Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue retrieving layout metadata for object via Metadata API #302

Closed
pavankumar9849 opened this issue Mar 25, 2024 · 1 comment
Closed

Comments

@pavankumar9849
Copy link

I am encountering an issue while attempting to retrieve layout metadata for the Quote object in my Salesforce org using the Metadata API. Despite providing the correct layout name ('Quote-Quote Layout') the metadata retrieval returns null values for all properties of the layout. And also facing an NullPointerException while adding custom fields to the page-layout.

Below is the MetadataAPI call , how I configured

`public static void addFieldsToPageLayout( String layoutName, List fieldApiNames) {
MetadataService.MetadataPort service = createMetadataService();

    // Retrieve the existing layout metadata
    //String layoutFullName = 'Quote.' + layoutName;
    try{
    MetadataService.Layout layoutMetadata = (MetadataService.Layout) service.readMetadata('Layout',new String[] { 'Quote-Quote Layout' }).getRecords()[0];
    system.debug('layoutMetadata Begin: ' + layoutMetadata);     
    for (String fieldApiName : fieldApiNames) {
        MetadataService.LayoutItem customFieldItem = new MetadataService.LayoutItem();
        customFieldItem.field = fieldApiName;
        // customFieldItem.behavior = MetadataService.LayoutBehavior.Edit; 
        
        // Add the layout item to the layout
        layoutMetadata.layoutSections[0].layoutColumns[0].layoutItems.add(customFieldItem);
        system.debug('layoutMetadata after adding: ' + layoutMetadata);
    }
   
    List<MetadataService.UpsertResult> results = service.upsertMetadata(new MetadataService.Metadata[] { layoutMetadata });
    handleUpsertResults(results[0]);
    system.debug('results: ' + results );
    for (MetadataService.UpsertResult result : results) {
        if (!result.success) {
            for (MetadataService.Error error : result.errors) {
                System.debug('Error message: ' + error.message);
            }
        }
    }
    } catch (Exception e){
        system.debug('Exception at AddFieldMethod: ' + e);
    }

}`

Layout details from WorkBench:

createdById: 0055h00000AYC1fAAH
createdByName: Pavan Kumar Panduru
createdDate: 2024-01-02T10:29:04.000Z
fileName: layouts/Quote-Quote Layout.layout
fullName: Quote-Quote Layout
id: 00h5h00000FhseFAAR
lastModifiedById: 0055h00000AYC1fAAH
lastModifiedByName: Pavan Kumar Panduru
lastModifiedDate: 2024-03-20T07:20:43.000Z
manageableState: unmanaged
namespacePrefix: SentinelEMSDEV
type: Layout

Layout metadata Debug statement:

layoutMetadata Begin: Layout:[Metadata.fullName=null, apex_schema_type_info=(http://soap.sforce.com/2006/04/metadata, true, false), customButtons=null, customButtons_type_info=(customButtons, http://soap.sforce.com/2006/04/metadata, null, 0, -1, false), customConsoleComponents=null, customConsoleComponents_type_info=(customConsoleComponents, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), emailDefault=null, emailDefault_type_info=(emailDefault, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), excludeButtons=null, excludeButtons_type_info=(excludeButtons, http://soap.sforce.com/2006/04/metadata, null, 0, -1, false), feedLayout=null, feedLayout_type_info=(feedLayout, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), field_order_type_info=(fullName, customButtons, customConsoleComponents, emailDefault, excludeButtons, feedLayout, headers, layoutSections, miniLayout, multilineLayoutFields, ...), fullName=null, fullName_type_info=(fullName, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), headers=null, headers_type_info=(headers, http://soap.sforce.com/2006/04/metadata, null, 0, -1, false), layoutSections=null, layoutSections_type_info=(layoutSections, http://soap.sforce.com/2006/04/metadata, null, 0, -1, false), miniLayout=null, miniLayout_type_info=(miniLayout, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), multilineLayoutFields=null, multilineLayoutFields_type_info=(multilineLayoutFields, http://soap.sforce.com/2006/04/metadata, null, 0, -1, false), platformActionList=null, platformActionList_type_info=(platformActionList, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), quickActionList=null, quickActionList_type_info=(quickActionList, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), relatedContent=null, relatedContent_type_info=(relatedContent, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), relatedLists=null, relatedLists_type_info=(relatedLists, http://soap.sforce.com/2006/04/metadata, null, 0, -1, false), relatedObjects=null, relatedObjects_type_info=(relatedObjects, http://soap.sforce.com/2006/04/metadata, null, 0, -1, false), runAssignmentRulesDefault=null, runAssignmentRulesDefault_type_info=(runAssignmentRulesDefault, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), showEmailCheckbox=null, showEmailCheckbox_type_info=(showEmailCheckbox, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), showHighlightsPanel=null, showHighlightsPanel_type_info=(showHighlightsPanel, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), showInteractionLogPanel=null, showInteractionLogPanel_type_info=(showInteractionLogPanel, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), showKnowledgeComponent=null, showKnowledgeComponent_type_info=(showKnowledgeComponent, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), showRunAssignmentRulesCheckbox=null, showRunAssignmentRulesCheckbox_type_info=(showRunAssignmentRulesCheckbox, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), showSolutionSection=null, showSolutionSection_type_info=(showSolutionSection, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), showSubmitAndAttachButton=null, showSubmitAndAttachButton_type_info=(showSubmitAndAttachButton, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), summaryLayout=null, summaryLayout_type_info=(summaryLayout, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), type=Layout, type_att_info=(xsi:type)]

CallOut Request:

SESSION_ID_REMOVEDLayoutQuote-Quote Layout

15:09:15.617 (7627890828)
|readMetadata_element:[apex_schema_type_info=(http://soap.sforce.com/2006/04/metadata, true, false), field_order_type_info=(type_x, fullNames), fullNames=(Quote-Quote Layout), fullNames_type_info=(fullNames, http://soap.sforce.com/2006/04/metadata, null, 0, -1, false), type_x=Layout, type_x_type_info=(type, http://soap.sforce.com/2006/04/metadata, null, 1, 1, false)]::SOAPAction="" Accept=text/xml User-Agent=SFDC-Callout/60.0 SFDC_STACK_DEPTH=1 Content-Type=text/xml; charset=UTF-8
15:09:15.617 (7677640890)|

Getting Exception for my code:

e|Exception|true|false
"common.apex.runtime.impl.ExecutionException: Attempt to de-reference a null object"|

Expecting :

-> Able to read out the Metadata of the respective page layout.
-> Can able to add custom fields to that respective page layout.

@pavankumar9849
Copy link
Author

I am able to retrieve the layout metadata , As I added Namespace prefix to the layout Names , then it is working fine.
but now, I am not able to add a new layout section in that respective page layout. Here is my code

` MetadataService.Layout layoutMetadata =
(MetadataService.Layout) service.readMetadata('Layout', new String[] { layoutName }).getRecords()[0];
MetadataService.LayoutSection newSection = new MetadataService.LayoutSection();
newSection.customLabel = true;
newSection.editHeading = true;

        newSection.label = 'Sentinel Information';
        newSection.style = 'TwoColumnsTopToBottom'; 
        MetadataService.LayoutColumn newColumn = new MetadataService.LayoutColumn();
        for (String fieldApiName : fieldApiNames) {
            MetadataService.LayoutItem customFieldItem = new MetadataService.LayoutItem();
            customFieldItem.field = fieldApiName;            
            newColumn.layoutItems.add(customFieldItem);
        }
        newSection.layoutColumns.add(newColumn);
        layoutMetadata.layoutSections.add(newSection);`

Seeking for a reply and help over this ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant