Hi
I am facing an issue in creating YAML file in my VS Code. The requirement is to fetch Page Layout of a custom object in apex and use the field API names and Lables in YAML file (the one we create in .view file in viewdefinitions folder) to create a Record Create Form.
I wrote the following code to return a List of key value pairs in apex.
public static List<FieldData> getHelpdeskLayout() {
List<FieldData> fieldsList = new List<FieldData>();
List<Metadata.Metadata> layouts = Metadata.Operations.retrieve(Metadata.MetadataType.Layout, new List<String> {'Helpdesk__c-Helpdesk Ticket Layout'});
Metadata.Layout layoutMd = (Metadata.Layout)layouts.get(0);
for (Metadata.LayoutSection section : layoutMd.layoutSections) {
for (Metadata.LayoutColumn column : section.layoutColumns) {
if (column.layoutItems != null) {
for (Metadata.LayoutItem item : column.layoutItems) {
System.debug(item);
Map<String, Schema.SObjectField> mfields = Schema.Helpdesk__c.SObjectType.getDescribe().fields.getMap();
Schema.DescribeFieldResult fieldResult = mfields.get(item.field).getDescribe();
System.debug('fieldResult.getLabel() ' +fieldResult.getLabel());
System.debug('item.field ' +item.field);
FieldData fieldData;
if(item.field =='Name')
{
fieldData = new FieldData(fieldResult.getLabel(), item.field,true,'textInput');
}
else{
fieldData = new FieldData(fieldResult.getLabel(), item.field,false,'textInput');
}
fieldsList.add(fieldData);
}
}
}
}
System.debug('fieldsList ' +fieldsList);
return fieldsList;
}
Note: FieldData is a wrapper class
global class FieldData {
global String label;
global Object value;
global boolean isRequired;
global String fieldType1;
global FieldData(String label, Object value,boolean isRequired,String fieldType1) {
this.label = label;
this.value = value;
this.isRequired = isRequired;
this.fieldType1 = fieldType1;
}
}
Following is the YAML file that I created
description: "This component displays record details in a message"
schema:
properties:
headerText:
type: string
defaultValue: "Record Details"
dataproviders:
record:
definition: "apex__DataProviderSObjectRecord.getHelpdeskLayout"
components:
- definition: modal
properties:
title: "Create Helpdesk Ticket"
submitLabel: "Create"
events:
onsubmit:
# definition maps to 'ActionDispatcherSubmitRecord' apex class implementing Slack.ActionDispatcher
definition: "apex__action__ActionDispatcherSubmitRecord"
properties:
objectApiName: "Helpdesk__c"
components:
- definition: divider
- definition: iteration
properties:
foreach: "{!record}"
foritem: "record"
components:
- definition: input
properties:
label: "{!record.label}:" # label is the field label
required: "{!record.isRequired}"
components:
- definition: "{!record.fieldType1}"
properties:
name: "{!record.value}" # name should be FieldApiName
I get the error in apex saying "No component was found with definition {!record.fieldType1}"
My goal is to dynamically provide definition (textInput, Numer, datepicker, etc) in the iteration. I can't figure out what I am doing wrong here.
If the issue is that “Currently dynamically deciding the type is not supported via variables” - perhaps coding a repeating block in YAML with one of each component type and dynamically making just the appropriate one visible for each field to display using visibility property. Just a wild thought.
