0

I have created a custom button "Create Case" on case for community user portal, where user when click on that button it will redirect to vf page. On that vf page i am having a field Contact name, where when i select a contact.. related fields should get autopopulated from related contact (i.e. AccountId,Requestor_First_Name__c,Requestor_Last_Name__c,Requester_Email__c,Requester_Contact_Number__c).

I have written the code but not sure why it is not autopopulating related fields value on vf page.

VF page :

<apex:page standardController="Case" extensions="AutoPopulateExample">

    <apex:form>
        <apex:pageBlock>

            <apex:pageBlockButtons >
              <apex:commandButton value="Save" action="{!Save}"/>
              <apex:commandButton value="Cancel" action="{!Cancel}"/>
            </apex:pageBlockButtons> 

           <apex:pageBlockSection title="Basic Details" columns="2">
               <apex:actionSupport event ="onchange" action="{!autoCal}" reRender="AccountId,Requestor_First_Name__c,Requestor_Last_Name__c,Requester_Email__c,Requester_Contact_Number__c"/><br></br>
               <apex:inputField label="Contact Name" value='{!Case.ContactId}' />
               <apex:inputField label="Requester First Name" value='{!Case.Requestor_First_Name__c}'/>
               <apex:inputField label="Account Name" value='{!Case.AccountId}'/>
               <apex:inputField label="Requester Last Name" value='{!Case.Requestor_Last_Name__c}'/>
               <apex:inputField label="Status" value='{!Case.Status}'/>
               <apex:inputField label="Requester Email" value='{!Case.Requester_Email__c}'/>
               <apex:inputField label="Subject" value='{!Case.Subject}'/>
               <apex:inputField label="Requester Contact Number" value='{!Case.Requester_Contact_Number__c}'/>
               <apex:inputField label="Product" value='{!Case.Products__c}'/>
               <apex:inputField label="Business Impact" value='{!Case.Business_Impact__c}'/>
               <apex:inputField label="Components" value='{!Case.Componet__c}'/>
               <apex:inputField label="Environment" value='{!Case.Environment__c}'/>
               <apex:inputField label="On Behalf of" value='{!Case.On_Behalf_of__c}'/>
               <apex:inputField label="Urgency" value='{!Case.Urgency__c}'/>
               <apex:inputField label="Description" value='{!Case.Description__c}'/>
           </apex:pageBlockSection>

        </apex:pageBlock> 
   </apex:form>

</apex:page>

controller :

public class AutoPopulateExample
{
public Contact con{get;set;}
public Case caseObject{get;set;}
public ApexPages.StandardController stdCntrlr {get; set;}

public AutoPopulateExample(ApexPages.StandardController controller)
{
    stdCntrlr = controller;
    con = new Contact();
    caseObject = new case();
}

//function is called from actionsupport event

public PageReference autoCal()
{

Id conid = caseObject.ContactId;     // collecting contact id from visualforce page

List<Contact> conLst = [select id,AccountId,FirstName,LastName,Email,Phone from contact where id=:conid];

caseObject.Requestor_First_Name__c = conLst[0].FirstName;      
caseObject.AccountId = conLst[0].AccountId; 
caseObject.Requestor_Last_Name__c = conLst[0].LastName;
caseObject.Requester_Email__c = conLst[0].Email; 
caseObject.Requester_Contact_Number__c = conLst[0].Phone; 

    return null;
}

}
0

apex:actionSupport needs to be inside the field to add an event listener to. Also, reRender must be linked to something that has an Id, just not just the list of fields to reRender.

<apex:form id="form">
...

<apex:inputField label="Contact Name" value="{!Case.ContactId}">
  <apex:actionSupport event ="onchange" action="{!autoCal}" reRender="form"/>
</apex:inputField>

Side Note:

I always recommend changing an action method's return type to void if you do not intend to redirect. This provides an easy visual cue that the function does not attempt to redirect:

public void autoCal() {

  // return null; is not necessary now
}

Edit: You also need to assign the case from the standard controller to the variable, or you'll never see the input:

caseObject = (Case)controller.getRecord();

Also, check if the list is empty first before trying to use conLst[0], as the user might not have selected a record.

if(conLst.isEmpty()) {
  return;
}
| improve this answer | |
  • Hi i have done the changes mentioned above but when i add contact name on vf page it is showing me following error : Error: Error occurred while loading a Visualforce page. – Admin Jan 14 '19 at 9:17
  • @SnehalGaware The other answer reminded me that you have yet another bug or two. See revisions. – sfdcfox Jan 14 '19 at 9:22
0

You need to bind caseObject variable to fields used in visualforce page. Also your actionsupport should be associate to a field. reRender the pageBlock or form element. Update your visualforce code as below and check.

<apex:page standardController="Case" extensions="AutoPopulateExample">
    <apex:form>
        <apex:pageBlock id="caseBlock">

            <apex:pageBlockButtons >
              <apex:commandButton value="Save" action="{!Save}"/>
              <apex:commandButton value="Cancel" action="{!Cancel}"/>
            </apex:pageBlockButtons> 

            <apex:pageBlockSection title="Basic Details" columns="2">

               <apex:inputField label="Contact Name" value='{!caseObject.ContactId}' >
                   <apex:actionSupport event ="onchange" action="{!autoCal}" reRender="caseBlock"/>
               </apex:inputField>

               <apex:inputField label="Account Name" value='{!caseObject.AccountId}'/>
               <apex:inputField label="Requester First Name" value='{!caseObject.Requestor_First_Name__c}'/>
               <apex:inputField label="Requester Last Name" value='{!caseObject.Requestor_Last_Name__c}'/>
               <apex:inputField label="Status" value='{!caseObject.Status}'/>
               <apex:inputField label="Requester Email" value='{!caseObject.Requester_Email__c}'/>
               <apex:inputField label="Subject" value='{!caseObject.Subject}'/>
               <apex:inputField label="Requester Contact Number" value='{!caseObject.Requester_Contact_Number__c}'/>
               <apex:inputField label="Product" value='{!caseObject.Products__c}'/>
               <apex:inputField label="Business Impact" value='{!caseObject.Business_Impact__c}'/>
               <apex:inputField label="Components" value='{!caseObject.Componet__c}'/>
               <apex:inputField label="Environment" value='{!caseObject.Environment__c}'/>
               <apex:inputField label="On Behalf of" value='{!caseObject.On_Behalf_of__c}'/>
               <apex:inputField label="Urgency" value='{!caseObject.Urgency__c}'/>
               <apex:inputField label="Description" value='{!caseObject.Description__c}'/>
           </apex:pageBlockSection>

        </apex:pageBlock> 
   </apex:form>
</apex:page>
| improve this answer | |
  • An easier fix would be to assign the record from the standard controller to caseObject, actually. – sfdcfox Jan 14 '19 at 9:20
  • Hi Reshma, I have done changes suggested by you as well but it is showing me same error after selecting value in contact lookup field. – Admin Jan 14 '19 at 9:20
  • @Reshma thanks to you as well for looking into issue. – Admin Jan 14 '19 at 9:31

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.