0

I have to pull the Metadata.Layout of an object from apex and basically re-create page layouts in aura. My plan was to use the Metadata.Layout and just iterate over the sections/columns/fields.

In Apex, I have debug statements that show me and the correct Layout is not null and is being properly queried/assigned. However, in aura, I can't seem to iterate over the sections, and I have some test statements set up, and it looks like the Layout section list is coming back as empty. Not sure why this is happening since it is being set correctly, and clearly isn't null on the back end. If anyone has any suggestions, I would greatly appreciate it.

.cmp file

    
    <aura:attribute name="lineLayout" type="Metadata.Layout"/>
    <aura:attribute name="lineLayoutSections" type="Object[]"/>
    <aura:attribute name="recordTypeId" type="String"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    
    <lightning:recordEditForm aura:id="recordEditForm"
                           objectApiName="Line__c">
        <lightning:messages />
        <div>{!v.lineLayout.layoutSections}</div>
        <div>{!empty(v.lineLayout.layoutSections)}</div>
        <aura:iteration items="{!v.lineLayout.layoutSections}" var="section">
            
            <div class="slds-section slds-is-open" aura:id="{!section.label}">
                <!--section header-->
                <h3 class="slds-section__title">
                    <button aria-controls="fruitsList" class="slds-button slds-section__title-action">
                        <span onclick="{!c.toggleSection}" data-auraId="fruitsSection">
                            <lightning:icon iconName="utility:switch"
                                            size="x-small"
                                            class="slds-section__title-action-icon slds-button__icon_left"
                                            alternativeText="button icon" 
                                            />
                        </span>
                        <span class="slds-truncate" title="{!section.label}">{!section.label}</span>
                    </button>
                </h3>
                <!--section body-->
                <div class="slds-section__content" id="fruitsList">
                    <aura:iteration items="{!section.layoutColumns}" var="column">
                        <aura:iteration items="{!column.layoutItems}" var="item">
                        <lightning:inputField fieldName="{!item.field}" />
                        </aura:iteration>
                    </aura:iteration>
                </div>
            </div>
            
        </aura:iteration>
    
        <lightning:button class="slds-m-top_small" type="submit" label="Save" />
    </lightning:recordEditForm>
    
    
    
    
    
    
</aura:component> 

helper.js

        var action = component.get("c.getLayout");
        action.setParams({ recordTypeId : component.get("v.recordTypeId")});
        $A.enqueueAction(action);
        action.setCallback(this, function(response) {
            var result = response.getReturnValue();
            component.set('v.lineLayout',result);
        })
    }

Apex

public class CustomLineFormController {

    
    //public Line__c line {get; set;}                 
    //public Metadata.Layout lineLayout {get; private set;} 
    
    @AuraEnabled
    public static Metadata.Layout getLayout(String recordTypeId) {
        Metadata.Layout lineLayout;
        try{
            List<Metadata.Metadata> components = Metadata.Operations.retrieve(
                Metadata.MetadataType.Layout, 
                new String[]{'Line__c-Non-Referencing AP-Non-Fed: ARC Internal'}
            );
            lineLayout = (Metadata.Layout) components.get(0);
        }catch(Exception e){
            System.debug('Exception: ' + e.getMessage());
        }
        System.debug('test:' + lineLayout.layoutSections);
        return lineLayout;
    }


}
2
  • Have you evaluated using the UI API (likely in an LWC)? That's what it is intended to do and is more suited to the purpose than Apex Metadata API.
    – David Reed
    Dec 4, 2020 at 3:31
  • Hi David, that's something I can consider, thanks. I had to make a quick turn around on this and I decided to use aura since I was more familiar with aura. I also didn't quite see why the above wouldn't work... Dec 4, 2020 at 14:13

0

You must log in to answer this question.

Browse other questions tagged .