Created
February 24, 2018 14:58
-
-
Save lfreeland/a5a766446d71ddce4308cfec677526a3 to your computer and use it in GitHub Desktop.
Lightning Field Set Form V2 Apex Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public with sharing class FieldSetFormControllerV2 { | |
| @AuraEnabled | |
| public static FieldSetForm getForm(Id recordId, String objectName, String fieldSetName) { | |
| FieldSetForm form = new FieldSetForm(); | |
| form.Fields = getFields(recordId, objectName, fieldSetName); | |
| return form; | |
| } | |
| private static List<Field> getFields(Id recordId, String objectName, String fieldSetName) { | |
| Schema.SObjectType objectType = null; | |
| if (recordId != null) { | |
| objectType = recordId.getSobjectType(); | |
| } | |
| else if (String.isNotBlank(objectName)) { | |
| objectType = Schema.getGlobalDescribe().get(objectName); | |
| } | |
| Schema.DescribeSObjectResult objectDescribe = objectType.getDescribe(); | |
| Map<String, Schema.FieldSet> fieldSetMap = objectDescribe.fieldSets.getMap(); | |
| Schema.FieldSet fieldSet = fieldSetMap.get(fieldSetName); | |
| List<Schema.FieldSetMember> fieldSetMembers = fieldSet.getFields(); | |
| List<Field> fields = new List<Field>(); | |
| for (Schema.FieldSetMember fsm : fieldSetMembers) { | |
| Field f = new Field(fsm); | |
| fields.add(f); | |
| } | |
| return fields; | |
| } | |
| public class FieldSetForm { | |
| @AuraEnabled | |
| public List<Field> Fields { get; set; } | |
| public FieldSetForm() { | |
| Fields = new List<Field>(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment