Monday, February 22, 2016

Get Salesforce record ID from lookup field in Visualforce page

Hello Friends,

Recently I was working on a requirement where I have to validate some lookup field on visualforce page itself not in apex without submitting form to server. 

I did lots of research and after having a lots of dig down of salesforce source code I identified each lookup field is associated with one hidden field with a "_lkid" appended to the id this help me to identify the record uniquely 

<apex:page standardController="Contact">
   <apex:form>
      <apex:inputField value="{!Contact.AccountId}" id="accountId onchange="getAccountSFId();">
          <script>
                  var accountId = "{!$Component.accountId}";
          </script>
   </apex:form>
   <script>
           function getAccountSFId(){
              var accId =  accountId + '_lkid';  
              // This will give you the 18 digit SF id of the account and you can do what ever you want with this 
              console.log(document.getElementById(accId ).value);
           }
   </script> 
</apex:page>

Tips on passing Salesforce AI Associate Certification

  🌟 Motivation to Pursue the Salesforce AI Associate Certification 🌟 The world of technology is in a state of perpetual evolution, and on...