function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Denise CrosbyDenise Crosby 

after inserting contact, need to refresh a selectList in a different pageBlock on same page

Hello. I am still new to Salesforce development and have a problem that seems over my head. I have to create an event where the contact is associated to the account, so I created my own VF page with custom controller. If the contact doesn't exist, the user can click the "New Contact" button that renders a pageBlock below to add a contact. The problem is after saving the contact, I need the contact selectlist to refresh with new contact as the selected contact. Seems easy, but I am stuck on this. Thank you for spending your valuable time looking at this.
<apex:page controller="NewEventController1" showHeader="true" sidebar="true" standardStylesheets="true">
<apex:form >
<apex:slds >
        
<div class="slds-scope"> 
<apex:pageBlock title="New Meeting">
<apex:pageMessages />
<apex:pageBlockSection columns="1" id="eventInformation">
    <apex:inputfield value="{!ev.subject}"/> 
    <apex:inputfield value="{!ev.startdatetime}" />     
    <apex:inputfield value="{!ev.description}"/> 
    <apex:inputfield label="{!accountLabel}" value="{!dummyContact.AccountId}" >
        <apex:actionSupport action="{!changeAccount}" event="onchange" rerender="eventInformation" />
    </apex:inputField>
    <apex:selectList label="{!contactLabel}" value="{!contactId}" size="1" rendered="{!showContactOptions}" id="contactselectlist">
        <apex:selectOptions value="{!contactOptions}" />
        <apex:actionSupport action="{!changeContact}" event="onchange" rerender="eventInformation" />
    </apex:selectList>
    <apex:pageBlockSectionItem rendered="{!!showContactOptions}"/>
</apex:pageBlockSection>
        
<apex:pageBlockbuttons >
   <apex:commandbutton value="Save" action="{!save}" immediate="true"/>  
   <apex:commandbutton value="Cancel" action="{!cancel}"/>  
   <apex:commandbutton value="New Contact" action="{!newcontact}"/>      
<!--  <apex:commandButton value="New Contact" action="{!URLFOR($Action.Contact.NewContact)}" /> -->
</apex:pageBlockbuttons>    
    
</apex:pageBlock>
<apex:pageBlock title="Contact Info" rendered="{!RenderContact}" id="contactinformation" >
      <apex:pageBlockButtons >
          <apex:commandButton action="{!insertContact}" value="Insert" reRender="eventinformation"/>
      </apex:pageBlockButtons>
      <apex:pageBlockSection showHeader="false" columns="1">
        <apex:inputField value="{!Newcontact.firstName}" />
        <apex:inputField value="{!Newcontact.lastName}" />
        <apex:inputField value="{!Newcontact.email}" />
      </apex:pageBlockSection>
    </apex:pageBlock>
    
</div>
</apex:slds>       
</apex:form>
</apex:page>



public with sharing class NewEventController1 {

public event ev { get; set; }

public Contact Newcontact {
        get {
        if (Newcontact == null)
        Newcontact = new Contact();
        return Newcontact;
        }
        set; }

public boolean RenderContact { get; set; }
    
public NewEventController1 (){
    RenderContact = false;
    ev = new event();
    ev.CurrencyIsoCode = UserInfo.getDefaultCurrency();
    ev.OwnerId = UserInfo.getUserId();
    ev.StartDateTime = Datetime.now().addMinutes(-Datetime.now().minute());
    ev.EndDateTime = Datetime.now().addMinutes(60-Datetime.now().minute());  
    ev.Subject = 'New Meeting';
    
    if ( ev.WhatId != null && ev.WhatId.getSObjectType() == Account.sObjectType )
        {
            dummyContact.AccountId = ev.WhatId;
        }
}

public Contact dummyContact
    {
        get
        {
            if ( dummyContact == null ) dummyContact = new Contact();
            return dummyContact;
        }
        private set;
    }

    public List<SelectOption> contactOptions
    {
        get
        {
            if ( contactOptions == null && dummyContact.AccountId != null )
            {
                contactOptions = new List<SelectOption>();
                contactOptions.add( new SelectOption( 'null', '--None--' ) );
                for ( Contact contact :
                    [   SELECT  Id, Name
                        FROM    Contact
                        WHERE   AccountId = :dummyContact.AccountId
                    ]
                    )
                {
                    contactOptions.add( new SelectOption( contact.Id, contact.Name ) );
                }
            }
            return contactOptions;
        }
        private set;
    }

    public Boolean showContactOptions
    {
        get { return contactOptions != null && contactOptions.size() > 1; }
    }

    public String contactId { get; set; }
    
	public String accountLabel
    {
        get { return Account.sObjectType.getDescribe().getLabel(); }
    }

    public String contactLabel
    {
        get { return Contact.sObjectType.getDescribe().getLabel(); }
    }
    
	public void changeAccount()
    {
        ev.WhatId = dummyContact.AccountId;
        ev.WhoId = null;
        contactOptions = null;
    }

    public void changeContact()
    {
                
        ev.WhoId =
        (   ( contactId != null && contactId != 'null' )
        ?   Id.valueOf( contactId )
       :   null
        ); 
    }    
    
public PageReference save() {
    
    return new ApexPages.StandardController(ev).save();
  }  
    
public PageReference newcontact() {
      RenderContact = true;
      return null;
  } 
    
public PageReference insertContact() {
      if (dummyContact.AccountId != null)
      	{ NewContact.AccountId = dummyContact.AccountId; }
      insert NewContact;
      ev.WhoId = NewContact.Id;
      return null;
  }      
    
public PageReference cancel() {
      return new ApexPages.StandardController(ev).cancel();
  }     
}



 
Best Answer chosen by Denise Crosby
Denise CrosbyDenise Crosby
Closing this question, as I changed the code to use a single PageBlock.