0

I need to retrieve the compact layout via APEX in order to generate the query. I would like to do something like:

String entity = 'Lead';
String fields = retrieveCompactLayoutFields(entity);
String searchQuery = 'SELECT ' + fields + ' FROM ' + entity + ' WHERE Id = :id LIMIT 1';

The compact layout is documented in the Metadata API, but I have no clue in how to access to it. Is it accessible from an APEX class? Can you please share a sample code?

1

Refer to this library provided on GitHub Apex Wrapper Salesforce Metadata API. This provides a lot of features.

To retrieve a metadata component, you only need MetadataService class and its corresponding test class. And a remote site with your salesforce url so that api calls can be made.

Sample code to retrieve is present in MetadataServiceExamples class. Search with keyword "retrieve" in this class and you will find a lot of examples.

Example:

    // Retrieve Home Page Layout
    MetadataService.MetadataPort service = createService();
    MetadataService.HomePageLayout homePageLayout =
        (MetadataService.HomePageLayout) service.readMetadata('HomePageLayout',
            new String[] { 'DE Default' }).getRecords()[0];
|improve this answer|||||
  • Thank you very much for the input. Reading the provided info: How to call the Salesforce Metadata API from Apex. Salesforce have been promoting recently the Metadata REST API. While this is still not a native API to Apex, it would be a lot easier to call than the Web Service one, though you would have develop your own wrapper classes. Unfortunatly this API is still in pilot and I have been told by Salesforce its appearance as a GA API is still someway out, sadly. So, I want to do something that natively is still not supported... Thank you very much again! – Javi Jun 7 '16 at 14:48
  • @bombita - There's really no reason not to use this as the configuration is simple as it uses the current session for authorization. You get all the wrapper classes for free and the github is littered with examples of how to use it. I thought it was daunting at first but then realized it is straightforward – cropredy Jun 8 '16 at 6:56
  • 1
    Should this question get a new answer now that Metadata.Operations exists as an API accessible by Apex? I guess not since this still only supports Layout... :( – Phil W Dec 10 '19 at 15:22

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.