I wanna get fields from a record page. So tried to use metadata query in apex, but it did not look working well.

Below is the code that I tried. What should I write in the code to get fields metadata in apex?

    List<Metadata.Metadata> components = Metadata.Operations.retrieve(
        new List<String>{'Account-Account Layout'}
    layout = (Metadata.Layout)components.get(0);
}catch(Exception e){

In the e.getMessage, the debug List index out of bounds: 0 was shown.

Or should I use xml code?

Thank you in advance.

  • 1
    To cover the obvious, have you double-checked that that layout name exists? Is the layout you're trying to receive part of a managed package?
    – David Reed
    Jul 7 '19 at 14:02
  • Yes, might be layout name issue. This code is working fine and getting layout information. Jul 7 '19 at 14:06
  • @DavidReed Could you tell me how can I know the layout name? Should I add some additional code to get layout name? or is it possible to check layout name from Salesforce setting?
    – Shin
    Jul 7 '19 at 14:13

This code appears to be correct, so I believe the problem is simply that your org does not contain a Page Layout called "Account-Account Layout".

If you navigate to the Account sObject in Setup->Object Manager, and click Layouts, you'll see a list of layout names:

Layout list

The metadata component name for each layout is of the form "<sObject API name>-<layout name>", where the layout name is what is shown here. Note however that if either the sObject or the layout is managed, namespaces will be present on the managed entity:

<sObject package namespace>__<sObject name>-<layout package namespace>__<layout name>

If in doubt, you can use a Tooling API query to get the FullName property:

SELECT FullName FROM Layout WHERE Id = '<an id>'

The Id is in the URL for each layout's detail page.


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.