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.

2
  • I don't think your accountDetail variable is of type Account. I suppose its type is String? Commented Jul 31, 2017 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); Commented Jul 31, 2017 at 12:54

1 Answer 1

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .