Find your content:

Search form

You are here

Ajax call not refreshing form data

 
Share

I am trying to use ajax - apex:actionfunction to update related account when opportunity is selected.

<apex:form id="myForm1">
<apex:pageBlock id="myPageBlock1">
 <tr>
      <td width = '25%'><span class ='grayNormal'><b>Related Account</b></span></td>
      <td align="left"><apex:inputField id="RelatedAccount" value="{!Comp__c.Related_Account__c}" style="width: 150px;" /> </td>
    </tr>
    <tr>
      <td width = '25%'><span class ='grayNormal'><b>Related Opportunity</b></span></td>
      <td align="left"><apex:inputField id="RelatedOppy" value="{!Comp__c.Related_Opportunity__c}" onchange="populateAccByOppy()"/></td>
    </tr>
 </apex:form>
 </apex:pageBlock>

javascript:

function populateAccByOppy() {
      var nameOfRelatedOppy = '{!$Component.myForm1.myPageBlock1}';
      var selctedOppy = document.getElementById(nameOfRelatedOppy+':RelatedOppy_lkid').value;
      if(selctedOppy != '')
          getAccNameByOppyId(selctedOppy);
  }

class:

public PageReference getAccNameByOppyId(String ids) {
       Comp__c comp =(Comp__c) stdCtrl.getRecord(); 
       system.assert(selectedOppyId != null);
       system.debug('selectedOppyId :: '+ selectedOppyId );
       Opportunity oppyAc = [Select Id, account.name from opportunity where id =:selectedOppyId ];
       comp .Related_Account__c = oppyAc.account.Id;
       return null;
   }

but it is not refreshing related account field


Attribution to: Bharat

Possible Suggestion/Solution #1

Ah, a common mistake --

public PageReference getAccNameByOppyId(String ids) { ...}

You can't directly call controller methods with arguments in Visualforce.

Your Javascript function populateAccByOppy() .. needs to invoke an actionFunction such as:

    <apex:actionFunction action="{!getAccNameByOppyId}" name="getAccNameByOppIdActionFn" 
                         reRender="myPageBlock1" > 
        <apex:param name="oppoId" value="" assignTo="{!selectedOppoId}"/>
    </apex:actionFunction> 

e.g. ..

 function populateAccByOppy() {
   var nameOfRelatedOppy = '{!$Component.myForm1.myPageBlock1}';
   var selctedOppy = document.getElementById(nameOfRelatedOppy+':RelatedOppy_lkid').value;
   if(selctedOppy != '')
      getAccNameByOppIdActionFn(selctedOppy);
 }

and you'll need to add to your controller a property

public ID selectedOppoId  {get; set;}

and rewrite the controller method to be a proper actionMethod:

public PageReference getAccNameByOppyId() {
   Opportunity oppyAc = [Select Id, account.name from opportunity where id =:selectedOppoId ];
   comp .Related_Account__c = oppyAc.account.Id;
   return null;
}

actionFunction components are Javascript functions with the added benefit of being able to invoke a controller actionMethod and passing parameters to controller properties.

A useful reference is http://bobbuzzard.blogspot.com/2011/07/passing-parameters-to-apex-method-from.html


Attribution to: cropredy
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/32911

My Block Status

My Block Content