4

How to access custom fields in javascript?

I have Account with both standard and custom fields. I use the default Page Layout to display and edit any acc. It is necessary to populate some of fields in javascript, so I need access to each of them.

Standart Account fields have a readable ids, such as acc1, acc15, acc16 (see attachments). They are the same for each account.

enter image description here

But custom fields have unique ids, so I cant identify them in DOM (attachments)

enter image description here

What is the best to have access to custom fields on standard Account Page Layout? Thanks a lot.

7
  • Do you have a Visualforce Page for this already?
    – highfive
    Commented Sep 18, 2014 at 6:12
  • Do you want to do this purely in javascript or do you have a controller that you can utilise? Commented Sep 18, 2014 at 6:22
  • I already have a controller and I could utilize this. Commented Sep 18, 2014 at 6:30
  • Can you please edit the question and post the code you already have?
    – highfive
    Commented Sep 18, 2014 at 6:32
  • You need this where? VF page, inline VF, standard layout? Commented Sep 18, 2014 at 7:59

3 Answers 3

3

In general there are a lot of solution for prepopulating fields on the standard layout page.

As you can see there are some solutions for this problem. You decide what you are taking.

enter image description here

3
  • Thank a lot! As I understand many of these approaches solve problem of pre-populating (on page load). But how to populate values (retrieved using ajax) into custom field ids on already loaded page? Good luck you too) Commented Sep 18, 2014 at 9:59
  • @AdelNazirov If you have got ID's you can set these values easily with Javascript using access to the field ID: document.getElementById('XXXXXX').value = XX for the input field for example. Commented Sep 18, 2014 at 10:05
  • please check this question salesforce.stackexchange.com/questions/50432/… Do you have any ideas? Commented Sep 25, 2014 at 8:47
1

Without your code its really hard to suggest anything...

Depending on your requirement:

  1. A simple Document.getElementByID may work
  2. ActionFunction can also be used to invoke controller methods from JavaScript
  3. You can also use @RemoteAction

For code example of these refer to my Blog post VISUALFORCE: PASSING VALUES FROM JAVASCRIPT TO CONTROLLER

1

The ID values rendered into Visualforce pages for custom fields are hard to get hold of and vary from org to org.

For purely client-side JavaScript I've resorted to basing the logic on the label text of a field. The data value is in a td of class dataCol and the label is in a td of class labelCol. So you can find the label and then find the data value input field in the peer td.

It is possible at some effort to get the field ID values using the tooling API (or some hacks) - see Querying Custom Object and Field IDs via Tooling API. So you could do this in your controller and then reference those field ID values in your Visualforce JavaScript logic.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .