[SalesForce] How to fetch a layout from metadata API in apex under a name prefix

Following my previous question: Updating page layout related list using metadata api

Yes, after three days' headache, I am still investigating this issue. I have created a new dev org and copied MetadataService.cls from Andy's github: https://github.com/financialforcedev/apex-mdapi. And applied the following code:

MetadataService.MetadataPort service = Util.createService();

MetadataService.Layout layout =
    (MetadataService.Layout) service.readMetadata('Layout',
                                                  new String[] { 'Contact-Contact Layout' }).getRecords()[0];

System.debug('layout is:' + layout);

And it works. The code for Util.createService() is:

public static MetadataService.MetadataPort createService()
{
    MetadataService.MetadataPort service = new MetadataService.MetadataPort();
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = UserInfo.getSessionId();
    return service;
}

However, the same code doesn't work for my current dev org which is developing a managed package with the namespace V6. So pretty much everything is under the name prefix: V6.

I am setting up everything for both orgs so the settings and code are pretty much the same except that my dev org is under a name prefix. So my question is, how should I be using metadata api in a namespace circumstance.

I have tried 'V6__Contact-Contact Layout' and 'V6.Contact-Contact Layout' and apparently they don't work.

Any suggestions would be highly appreciated.

Best Answer

The pagelayout follow below naming conventions for managed package

for a packaged layout over a standard object you need to retrieve

<Object Name>-<namespace>__<Layout Name>

and for a packaged layout over a packaged object

<namespace>__<Object Name>-<namespace>__<Layout Name>

Hence try with

Contact-V6__Contact Layout

Related Topic