Salesforce | How to get Salesforce lookup field object id using javascript

| By Webner

Suppose we have the following lookup field on a VF page:

<apex:inputField id="owner" value="{!Event.Ownerid}">

To get value of above lookup field using field id in javascript, if we directly use document.getElementById(‘{!$Component.owner}’).value, it will return the text that is displayed within the textbox (usually the name of the selected record) after making selection. But what we need is to retrieve the Id of the record selected in the lookup.


In Salesforce for each lookup field on the VF page a hidden field is created corresponding to each lookup field on the page. This field contains the actual Id of the record selected in lookup. Id of this field is the id of actual lookup field suffixed with ‘_lkid’. So we will have to write the javascript as below:

<script type="text/javascript">
function grabOwnerId()
var ex =

Leave a Reply

Your email address will not be published. Required fields are marked *