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
mikumiku 

How to add callback function for lookup Field change?

I want rerender my page when one lookup field changed? I add a js function for the event onchange of the lookup field.

<apex:inputField id="patient" value="{!claim.Patient_Name__c}" onchange="patientNameChanged();" required="true"                 styleClass="requiredClass"></apex:inputField> 

 

function patientNameChanged() {           if(document.getElementById('{!$Component.formId.pageBlk.pgBlockSec1.patient}').value == '') {                submit = false;           } else {                         document.getElementById('patientId').value =                document.getElementById('{!$Component.formId.pageBlk.pgBlockSec1.patient}' + '_lkid').value;               setSubmit();                patientSelected();         

  }                    }

 

but the document.getElementById('{!$Component.formId.pageBlk.pgBlockSec1.patient}' + '_lkid').value is the value before the lookup function.

 

I want to get the value that after the lookup execution.

 

any sugesstion would be appreciated. thanks

 

Shashikant SharmaShashikant Sharma

try this

Controller :

public class TestRerenderController {
    public Contact con{get;set;}
    public TestRerenderController()
    {
        con = new Contact();
    }
    
}

 VFP

 

<apex:page controller="TestRerenderController" showHeader="false">

   <apex:form >
  <apex:pageBlock>
      <apex:pageBlockSection>
           <apex:inputField value="{!con.AccountId}" onchange="onchangeOfLookup();" id="lookupField"/>
       </apex:pageBlockSection>
   </apex:pageBlock>
    
    <script>
    
    var oldLookupValue = '';
    function onchangeOfLookup()
    {
       
       var currentLookup = document.getElementById('j_id0:j_id1:j_id2:j_id3:lookupField_lkid');
       if(currentLookup.value == oldLookupValue)
       {
       alert('Same Lookup');
       }
       else
       {
       alert('New Lookup');
       }
       oldLookupValue = currentLookup.value;
    }
    
    </script>
    
  </apex:form>
</apex:page>

 You can use similarly in your page.