0

This is my visualforce page which displays the selected contacts from the previous page and a list of contacts in a picklist.

<apex:form>
<apex:pageBlock title="Selected contacts" >
    <apex:pageBlockSection>
        <apex:pageBlockTable value="{!selectedContacts}" var="sc" >
            <apex:column value="{!sc.firstName}" />
            <apex:column value="{!sc.lastName}" />
            <apex:column value="{!sc.phone}" />
        </apex:pageBlockTable>
    </apex:pageBlockSection>
    <apex:pageBlockSection title="Choose account" >
         <apex:selectList value="{!accountDetail}" size="1">
            <apex:selectOptions value="{!options}"/>
         </apex:selectList>
    </apex:pageBlockSection>
    <apex:pageBlockButtons>
        <apex:commandButton value="Save" action="{!save}" />
    </apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>

This is a part my controller related to the save button in the above page.

public pageReference save(){
    system.debug('account'+accountDetail.Id);
    for(contact con : selectedContacts){
        con.accountId = accountDetail.Id;
        selectedContactWithAccount.add(con);             
    }
    if(selectedContactWithAccount.size() > 0){
        update selectedContactWithAccount;
    }

    pageReference page = new pageReference('/apex/finalStep31July');
    return page;
} 

When i click on the save button the above page just gets reloaded and neither of the contacts get saved in the account record selected from the picklist. Please tell me what am I doing wrong here.

  • I don't think your accountDetail variable is of type Account. I suppose its type is String? – Martin Lezer Jul 31 '17 at 12:29
  • It was of type account but now i changed it to string . But now I am getting null pointer exception on selectedContactWithAccount.add(con); – Mohit Joshi Jul 31 '17 at 12:54
1

accountDetail should be of String type and it should store the Id of the account which you are selecting from the picklist.

public pageReference save(){
    system.debug('account'+accountDetail.Id);
    for(contact con : selectedContacts){
        con.accountId = Id.valueof(accountDetail); //value selected from the picklist
        selectedContactWithAccount.add(con);             
    }
    if(selectedContactWithAccount.size() > 0){
        update selectedContactWithAccount;
    }

    pageReference page = new pageReference('/apex/finalStep31July');
    return page;
} 

Refer Apex selectList for understanding the type of attribute value.

  • That solved my problem but i am getting a null pointer exception in line selectedContactWithAccount.add(con); – Mohit Joshi Jul 31 '17 at 12:48
  • accountDetail is not empty – Mohit Joshi Jul 31 '17 at 12:52
  • id of the the selected account from the picklist – Mohit Joshi Jul 31 '17 at 12:55
  • 1
    @MohitJoshi Did you instantiate your list using selectedContactWithAccount = new List<Contact>(); ? – Martin Lezer Jul 31 '17 at 12:55
  • No I did not, that was the problem. Thanks :) – Mohit Joshi Jul 31 '17 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.