0

Below is the sample code i have written working fine, However I am search for option using userinterface api in controller class to get layout assignment on the basis of recordtype and loggedin user and having the same fields to display for editing page.

<apex:page tabStyle="Account" standardController="Account" extensions="Ctlr">
     <apex:form >
     <apex:pageMessages />
        <apex:sectionHeader title="Edit" subtitle="{!Account.Name}" />
        <apex:pageBlock >
          <apex:pageMessages />
          <apex:pageBlockButtons >
            <apex:commandButton value="Cancel" action="{!cancel}" />
            <apex:commandButton value="Save" action="{!Save}" />
          </apex:pageBlockButtons>
          <apex:outputPanel rendered="{!AccountLayout !=null }">
            <apex:repeat value="{!AccountLayout .layoutSections}" var="section">
              <apex:pageBlockSection title="{!section.label}" collapsible="true" >
                <apex:repeat value="{!section.layoutColumns}" var="col">
                  <apex:repeat value="{!col.layoutItems}" var="item" rendered="{!account.IsPersonAccount}">
                <apex:inputField rendered="{!item.behavior = EditEnum && item.field != 'Name'}" 
                       value="{!accData[item.field]}" 
                    />

                  </apex:repeat>
                </apex:repeat>
              </apex:pageBlockSection>
            </apex:repeat>
          </apex:outputPanel>


        <apex:pageBlock >

        </apex:pageBlock>
        </apex:pageBlock>
      </apex:form>


     <script>

     </script>

    </apex:page>

Controller:

public with sharing class Ctlr {
    public Account accData {get; private set;}                 //editable Account
    public Metadata.Layout AccountLayout {get; private set;} //layout
    public Metadata.UiBehavior editEnum {
        get{ return Metadata.UiBehavior.Edit; }
    }

    public Ctlr (ApexPages.StandardController controller) {
        Id editAcc= ((Account)controller.getRecord()).Id;
        try{
            //get Account layout
            List<Metadata.Metadata> components = Metadata.Operations.retrieve(
                Metadata.MetadataType.Layout, 
                new String[]{'Account-Account Layout'}
            );
            AccountLayout = (Metadata.Layout) components.get(0);
            accData =[select all fields from account where id =: editAcc and IsPersonAccount = true];
            system.debug(accData);
        }catch(Exception e){
            System.debug('The following exception has occurred: ' + e.getMessage());
             ApexPages.Message ErrorMsg = new ApexPages.Message(ApexPages.Severity.FATAL,e.getmessage());
             ApexPages.addMessage(ErrorMsg );
        }
    }
}
0

You can call out to the metadata REST API to get the list of layouts for an object, that'll return them alongside their record type IDs and will indicate which is the default for the current user. I've not found a way to do the same using the User Interface API (though stand to be corrected), so think this is what you'll have to do.

There seems to be many ways of getting layout information and they all seem to work slightly differently. The biggest I had with the Apex metadata API itself is that I've never seen a way to get the layout names it wants, e.g. 'Account-Account Layout' programmatically.

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.