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
V100V100 

Visualforce Contact List to update Lead Object

I have created a visualforce page on the lead object that is populated with the contacts for that account, this is to allow the user to simply select the contact they want

<apex:page standardController="Lead" extensions="LeadContactCtrl">

<apex:pageBlock >
<apex:form id="SelectConForm">
<apex:pageBlockTable value="{!Contacts}" var="c">
    <apex:column value="{!c.Name}"/>
    <apex:column value="{!c.Phone}"/>
    <apex:column value="{!c.Account_Manager_Contact_Flag__c}"/>
    <apex:column value="{!c.Key_Decision_Maker__c}"/>
    <apex:column value="{!c.Last_Updated_Date__c}"/> 
    <apex:column>  
        <apex:inputField id="Contact__c" value="{!c.Id}"/>  
    </apex:column>
    <apex:column>  
        <apex:commandButton value="Select" action="{!Save}"/>
    </apex:column>
</apex:pageBlockTable>
</apex:form>
</apex:pageBlock>
        

</apex:page>

 When the clickig save the controller tries to select that contact into the lead and save the lead:

public PageReference save()
{
    List<Lead> l = [SELECT Contact__c FROM Lead WHERE Id = :LeadId LIMIT 1];
    system.debug('JMM Lead: ');
    l[0].Contact__c= WHATSHOULDGOHERE;//'003P000000XjK71IAF';
    update l;
    
    return new PageReference('/apex/LeadContact?'+LeadId+'&refreshPage=true');
}

 I can get this work when i hard code a contact but cannot get to use the one selected in the save from the visualforce.

I have tried in the WHATSHOULDGOHERE i have tried eveything i can think off and but i'm sure i must be missing something simple.

Any help much appreciated.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

Add an ID to the Pageblock

 

<apex:pageBlock id="contactBlock">

 

Then use rerender="contactBlock" in the command button. You may also need immediate="true" (I did on my page)

 

<apex:commandButton value="Select" action="{!Save}" reRender="contactBlock" immediate="true">

 

the reRender is required fo the command Button to send the value of the param to the controller

 

Bob wrote a blog post regarding this: http://bobbuzzard.blogspot.com/2011/07/passing-parameters-to-apex-method-from.html

 

Also, you can put a temporary {!selectedID} next to the button and it should be the value of the var after rerender.

 

 

 

 

 

 

All Answers

Starz26Starz26

On the VF Page

 

<apex:column>  
        <apex:commandButton value="Select" action="{!Save}">
<apex:param value="{!c.id}" assignTo="{!selectedID}"/>
</apex:commandButton>
 </apex:column>

 

On the controller

 

ID selectedID {get;set;}

public PageReference save() { List<Lead> l = [SELECT Contact__c FROM Lead WHERE Id = :LeadId LIMIT 1]; system.debug('JMM Lead: '); l[0].Contact__c= selectedOD ;//'003P000000XjK71IAF'; update l; return new PageReference('/apex/LeadContact?'+LeadId+'&refreshPage=true'); }

 

 

V100V100

Hi

Thanks for the suggestion but i cant get it to work.

The debug statem shows selectedID as null and the contact is not saved against the lead.

Have i gone wrong somewhere, thanks again.

<apex:pageBlock >
<apex:form id="SelectConForm" >
<apex:pageBlockTable value="{!Contacts}" var="c">
    <apex:column value="{!c.Name}"/>
    <apex:column value="{!c.Phone}"/>
    <apex:column value="{!c.Account_Manager_Contact_Flag__c}"/>
    <apex:column value="{!c.Key_Decision_Maker__c}"/>
    <apex:column value="{!c.Last_Updated_Date__c}"/> 
    <apex:column >  
        <apex:commandButton value="Select" action="{!Save}">
        <apex:param value="{!c.id}" assignTo="{!selectedID}"/> 
        </apex:commandButton>
    </apex:column>
</apex:pageBlockTable>
</apex:form> 
</apex:pageBlock>

 Then the controller:

ID selectedID {get;set;} 

public PageReference save(){  
    List<Lead> l = [SELECT Contact__c FROM Lead WHERE Id = :LeadId LIMIT 1];
    system.debug('JMM Lead: ' + selectedID);
    l[0].Contact__c= selectedID;//'003P000000XjK71IAF';
    update l;
    refreshPage=true;
    
    return new PageReference('/apex/LeadContact?'+LeadId+'&refreshPage=true');
}

 

Starz26Starz26

try adding a rerender tag to the commandbutton

V100V100

Not too sure what this meant but tried

<apex:commandButton value="Select" action="{!Save}" reRender="{!selectedID}"> returned

Error: Unknown property 'LeadStandardController.selectedID'

then:

<apex:commandButton value="Select" action="{!Save}" reRender="{!Lead.Contact__c}">

still didnt update.

 

Not sure what this would do as the value selected is not getting passed to the controller at all.

Appreciate any more ideas as i am tearing my hair out with this one. Seems so simple but....


Starz26Starz26

Add an ID to the Pageblock

 

<apex:pageBlock id="contactBlock">

 

Then use rerender="contactBlock" in the command button. You may also need immediate="true" (I did on my page)

 

<apex:commandButton value="Select" action="{!Save}" reRender="contactBlock" immediate="true">

 

the reRender is required fo the command Button to send the value of the param to the controller

 

Bob wrote a blog post regarding this: http://bobbuzzard.blogspot.com/2011/07/passing-parameters-to-apex-method-from.html

 

Also, you can put a temporary {!selectedID} next to the button and it should be the value of the var after rerender.

 

 

 

 

 

 

This was selected as the best answer
V100V100

Nearly there :-)

The code below now sends the contact to the controller and updates the lead.

Only issue is that is no longer causes the full lead page to refresh so the user does not know that they have successfully picked that contact. Suspect this is to do with the rerender piece but not sure how to solve.

 

<apex:pageBlock id="contactBlock">
<apex:pageBlockTable value="{!Contacts}" var="c">
    <apex:column value="{!c.Name}"/>
    <apex:column value="{!c.Phone}"/>
    <apex:column value="{!c.Account_Manager_Contact_Flag__c}"/>
    <apex:column value="{!c.Key_Decision_Maker__c}"/>
    <apex:column value="{!c.Last_Updated_Date__c}"/> 
    <apex:column >  
        <apex:commandButton value="Select" action="{!Save}" reRender="all" immediate="true">
        <apex:param name="conIDParam" value="{!c.id}" assignTo="{!selectedID}"/>
        </apex:commandButton>
    </apex:column>
</apex:pageBlockTable>

 

V100V100

I've got it, used

<apex:outputPanel rendered="{!refreshPage}">
   <script>
      window.top.location='/{!Lead.id}';
   </script>
</apex:outputPanel>

 but had to move to inside the rerendered area to refresh the whole page.

Thanks for all your help