I have the below piece of code.
------------------Apex class----------------
public static String retrieveLayoutMetadata ( String strLayoutConfig ) {
System.debug(' strLayoutConfig : ' + strLayoutConfig);
Map<String, Object> mapLayoutResult = new Map<String, Object>();
List<LayoutWrapper.LayoutSection> lstLayoutSections = new List<LayoutWrapper.LayoutSection>();
setLayoutFieldApi = new Set<String>();
LayoutConfigWrapper layoutConfig = (LayoutConfigWrapper) JSON.deserialize(strLayoutConfig, LayoutConfigWrapper.class);
if(layoutConfig != null) {
// Get the object schema and all its field map --
descResult = Schema.describeSObjects( new String[]{ layoutConfig.objectApi } );
mapFields = descResult[0].fields.getMap();
isAllReadOnlyFields = true;
// List fullnames of components we want to retrieve
List<String> lstComponentName = new List<String>{ layoutConfig.objectApi + '-' + layoutConfig.layout };
// Retrieve component "Error Block"
List<Metadata.Metadata> lstComponents =
Metadata.Operations.retrieve( Metadata.MetadataType.Layout, lstComponentName);
Metadata.Layout layoutMeta = (Metadata.Layout) lstComponents.get(0);
// Prepare layout fields of retrieved component
for(Metadata.LayoutSection layoutSec : layoutMeta.layoutSections) {
List<LayoutWrapper.LayoutField> lstLayoutFields = new List<LayoutWrapper.LayoutField>();
for(Metadata.LayoutColumn layoutCol : layoutSec.layoutColumns) {
if(layoutCol.layoutItems != null) {
lstLayoutFields.addAll( prepareColumn(layoutCol, layoutConfig) );
}
}
if(!lstLayoutFields.isEmpty()) {
lstLayoutSections.add( new LayoutWrapper.LayoutSection(layoutSec.label, lstLayoutFields, true));
}}
if(!lstLayoutSections.isEmpty()) {
LayoutWrapper layoutWrap = new LayoutWrapper(lstLayoutSections, isAllReadOnlyFields);
mapLayoutResult.put('layoutWrapper', layoutWrap);
}}
mapLayoutResult.put('layoutFields', setLayoutFieldApi);
System.debug(' mapLayoutResult : ' + mapLayoutResult);
return JSON.serialize(mapLayoutResult);
}
And this is test class :
static void testRetrieveLayoutMetadata() {
Test.startTest();
Map<String, Object> mapResult;
String strLayoutConfig = '{ "objectApi": "Applicant__c", "layout": "Sales Data Entry KYC Details", "hiddenRequiredFields": ["Name"] }';
System.debug('strLayoutConfig'+strLayoutConfig);
string strResult = SalesDataEntryController.retrieveLayoutMetadata(strLayoutConfig);
System.debug('strResult'+strResult);
mapResult = (Map<String, Object>) JSON.deserializeUntyped(strResult);
System.debug('strResult'+strResult);
System.assert(mapResult != null && mapResult.get('layoutFields') != null, 'Error - Something went wrong.');
Test.stopTest();
}
Can somebody help you with this error "List index out of bound" Thank you
NullPointerException
) that you can solve by looking at your code and the data it is processing. You're currently trying to access an empty list.