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
admintrmpadmintrmp 

Get a lookup id via JS

I need to get a lookup ID from an inputField component via JavaScript but the field with the lookup ID looks different and I can't rely on setting a static component ID to get it.

 

Is there anyway I can do this? I'm developing a managed app, so I need to be able to make sure I can get the ID of the field dynamically.

Best Answer chosen by Admin (Salesforce Developers) 
admintrmpadmintrmp

I made a Javascript function to solve this problem just in case anyone wants it:

 

 

function getLookUpId(str) {
return document.getElementById(str+'_lkid').value;
}

 

For those using jQuery:

 

 

function getLookUpId(str) {

str = str+'_lkid';

str = str.replace(/:/g, '\\:');

return jQuery('#'+str).val();
}

 

All Answers

admintrmpadmintrmp

I made a Javascript function to solve this problem just in case anyone wants it:

 

 

function getLookUpId(str) {
return document.getElementById(str+'_lkid').value;
}

 

For those using jQuery:

 

 

function getLookUpId(str) {

str = str+'_lkid';

str = str.replace(/:/g, '\\:');

return jQuery('#'+str).val();
}

 

This was selected as the best answer
InnovationInnovation

I'm not sure what you're using the data for or where it goes beyond the context of this snippet, but if you're passing anything back to VF from JS this appears to create a XSS vulnerability based on the fact that the value retrieved by JS can be manipulated in the browser.  Just a heads up if this is a commercial app that you plan to submit for an AppExchange security review, we've seen similar code get flagged by the security scan.

 

admintrmpadmintrmp

I suppose your right.

 

Perhaps the following snippet would be a better idea?

 

 

function cloneId(str){ return str; } function getLookUpId(str) { var recordId = cloneId(document.getElementById(str+'_lkid').value); return recordId; }

 

 

 

Andy.xuAndy.xu

param  str‘s  meaning?