2

I've been looking in to the ui-api but it doesn't cut it for me, what I need It's to return exclusively the fields in the Page Layout for an object and some additional records.

Something like Getting the Opportunity and getting its related records like the Contact records related to it as well as the OpportunityLineItem records related to it.

The ui-api does the job, but still, it's a little of a pain to extract the data from it, I'm trying to feed an external service with this, but there are just so few fields and I want to do it for many objects, that is why querying the Page Layout could be useful, may be not the data of the record but at least the involved fields, so I can do the SOQL query and get them.

My first attempt to solve this issue, was querying the full object, with 2 levels of depth, but It was too much, for the request.

  • This is really the designed use case for the UI API. Can you say a little more about why it is not working for you? I'm not sure any of the alternatives will be more comfortable. – David Reed Nov 28 '18 at 0:22
  • I have one service that will be listening to http requests made from salesforce and another couple of service and it has a well defined api, and I can't make any requests from this other service, so I need to find a way to make a call, from apex to this service with this data, and well, I can't find a way to dynamically get this data, for an arbirary sObject, unless I'm able to manipulate the ui-api responses, in apex, and the forward it to the other service, but making an api request from salesforce to salesforce sounds like I'm doing it wrong, and there could be another way. – ekiim Nov 28 '18 at 0:30
3

The other way besides the UI API to approach this in pure Apex (that is, without calling out to an API explicitly) is to use the Apex Metadata API, a thin wrapper over some of the basic functionality of the Metadata API. Apex Metadata supports pulling layout details. Cf. also Trailhead, which has an example of using this functionality for a different purpose.

An example:

List<Metadata.Metadata> layouts = 
    Metadata.Operations.retrieve(Metadata.MetadataType.Layout, 
                                 new List<String> {'Account-Account Layout'});
Metadata.Layout layoutMd = (Metadata.Layout) layouts.get(0);

for (Metadata.LayoutSection section : layoutMd.layoutSections) {
    for (Metadata.LayoutColumn column : section.layoutColumns) {
        if (column.layoutItems != null) {
            for (Metadata.LayoutItem item : column.layoutItems) {
                System.debug(item.field);
            }
        }
    }
}

This would output the API names of all of the fields on the layout "Account-Account Layout". But it doesn't do all the other things the UI API does for you, like managing record types, visibility/FLS, picklist entries, etc. You'd need to handle all of that yourself, including determining the correct layout to display for any given combination of profile and record type so that you can query it.

I don't want to opine too much about your system architecture because there's obviously complexity there that I wouldn't assume you can cover in a comment. I'd just urge considering a few points before you reach for a completely different solution:

  • UI API is explicitly built for creating UIs backed by Salesforce data.
  • UI API handles record types, picklist values, and FLS for you in a single endpoint. Implementing all this stuff yourself is a giant pain.
  • You can source more than one record's data in a single call.
  • You can transform the UI API response body within Salesforce if necessary. It doesn't sound like fun, but it can be done.
  • Wow, this is great, I did that trailhead, when I started training as a developer for salesforce, but at that moment, I didn't understand the real value of it Thanks. – ekiim Nov 28 '18 at 0:43
  • is there a way to get the Layout Page metadata for a specific Object ID, and profile? – ekiim Nov 29 '18 at 1:27
  • Well.... there's the UI API :). Seriously, though, you'd have to query the object record type, then source the data for page layout assignments out of the Profile (which I think you'd have to go to the Metadata API for) and put that all together. – David Reed Nov 29 '18 at 13:16

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.