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

Field names/ids in a form


We need to be able to access the field names on a SalesForce page with javascript. For example when the user opens the Accounts page our javascript needs to be able to access the email field. When the user opens the Lead page our javascript needs to be able to access the email field on Leads.


Is there a way to detect what is the email's field id/name on Leads and Accounts and possibly the other SF pages.





Is this a visualforce page or a standard salesforce page, or some combination thereof?


If its a VF page, then you can specify your own ids when coding the page.


If its a standard salesforce page, I've found that standard fields have predictable ids which you can find by viewing the page source or inspecting elements in chrome.  


If its a combination then hopefully a combination of the above will do the trick.


Hi Bob,

Thank you for your answer.

It is a combination of both that I have + an additional one - users may create their own VF pages and not follow neither mine nor SF's convention for naming fields.


I am looking for a more predictable and robust way of doing this but if there is not such way I will follow the approach you described.


Nice blog by the way.





Ah - I see.  That's a bit of a tricky one.  I can't see a viable way of  doing this in javascript that would be 100% right.  For example, I've just looked at a page of mine that I use to create accounts and the name field is actually an input text rather than an input field (for server side reasons).  I haven't specified an id and the HTML input automatically produced is:



<input type="text" name="j_id0:j_id2:j_id3">



Which isn't going to be very helpful to javascript!


I think you stuck trying to enforce best practices unfortunately.  There isn't an awful lot to help you automate checking this either - the only thing I can think of is something like Selenium running on a server and checking that the javascript has the desired effect across all pages, but that would take some serious configuration.


When you are dealing with visualforce, the $Component variable in formula expressions is designed to give you the generated  dom-id for a component from the id attribute you specify in markup.


In terms of standard Salesforce pages, note that screen scraping to determine things like ids and names is NOT RECCOMENDED. We reserve the right to change these at any time, and if so your integration could break.





Thank you all for the answers.

Stephan I do understand that ids and names is NOT RECOMENDED but could you please let us know how else to achieve what we would like to do.

In particular we would like to do some processing after somebody has entered an email address on any page. We do require the users to indicate on which of the pages there is an email field and which field it is but we do not want to require for the users to indicate the field ids as well. We would like to be able to automatically detect those so that our javascript can hook up to those fields.


To recap the user tells us that Field: "EmailAddress__c" on sObject "MyObject__c"  is the email field.

Based on that information how can we find out id or name of the html input field that will represent the field "EmailAddress__c" so that our javascript can hook up some events to that?