3

How to determine a list of Opportunity fields by API name that are on the page layout?

For example for the Opportunity Object:

  • I have created a custom field whose API Name is "My_Custom_Field__c"
  • I have created a page layout that is named "My_Page_Layout"

What I am trying to do is create a SOQL query but only include the fields that are currently on "My_Page_Layout".

I ruled out the "Edit Page Layout" screen as a viable option because of the number of custom fields and the number of fields included on the page layout.

I need something more dynamic that can leverage Salesforce metadata about the fields that are on the page layout currently and return a list of API field names.

Your insights are appreciated.

5

You can call the REST API to determine the fields on a layout, although some parsing is required on your part. First, call:

/services/data/v36.0/sobjects/Account/describe/layouts/

(Replace Account with your object). If you don't use record types, you'll get a page layout. Otherwise, you'll get a record type list:

{"layouts":null,
 "recordTypeMappings": 
     [{"available":false,
       "defaultRecordTypeMapping":false,
        "layoutId":"..............",
        "master":false,
        "name":"............",
        "picklistsForRecordType":[],
        "recordTypeId":"...............",
        "urls":         
        {"layout":"/services/data/v36.0/sobjects/Account/describe/layouts/........."}},
      ...],
      "recordTypeSelectorRequired":[true]}

Once you know what record type you want, use the layout URL for that record type:

{"buttonsLayoutSection": ... describes buttons ...,
 "detailLayoutSections": ... describes page layout sections...
     { "layoutRows": ... describes each row ...
         [ { "layoutItems": ... describes each item ...
             [ { "layoutComponents": ... describes each component (may have multiple parts, like Name on Lead/Contact
                 [ { "value": "FieldName" ... and other data

Honestly, there's a ton of data in this JSON, and it did take me a couple of days to get all of this mapped out in a structure I could parse into a Visualforce page when I did this originally a couple of years ago.

You'll want to play with the API to see what information you get back, especially being wary of composite fields, because they have an interesting behavior (composite fields being those with more than one sub-component, like some Name fields, address fields, geolocation fields, etc).

Once you get all this data, you can construct SOQL statements from the results, render a page, etc.

This describe includes everything you could want to know about the layout, including the read-only/editable status of the field, if it can be created/updated (some fields are only valid on create, for example), the data type, precision, scale, and so on of the layout being described.

  • Your answer is much appreciated and obviously very smart @sfdcfox. Here is my stupid follow-up question: What tool do you use to "call the REST API"? Thanks – Jon Jaussi Apr 28 '16 at 17:42
  • @santiago_jon It's a little more than I can post in a comment, but you can start here to read about making callouts. You might also ask an additional question if you need more help. – sfdcfox Apr 28 '16 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.