Last active
April 19, 2021 13:40
-
-
Save vspruyt-sol/028e0a6a7ced39e23de310e8efac7761 to your computer and use it in GitHub Desktop.
Importing a fieldset in LWC
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 class FieldSetField | |
{ | |
@AuraEnabled | |
public Boolean dbRequired; | |
@AuraEnabled | |
public String apiName; | |
@AuraEnabled | |
public String label; | |
@AuraEnabled | |
public Boolean required; | |
@AuraEnabled | |
public String type; | |
public FieldSetField(Schema.FieldSetMember fieldSetMember) | |
{ | |
this.dbRequired = fieldSetMember.dbRequired; | |
this.apiName = fieldSetMember.fieldPath; | |
this.label = fieldSetMember.label; | |
this.required = fieldSetMember.required; | |
this.type = String.valueOf(fieldSetMember.getType()); | |
} | |
} |
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
import { LightningElement, api, track, wire } from 'lwc'; | |
import getFieldSetFieldsByFieldSetName from '@salesforce/apex/Utilities.getFieldSetFieldsByFieldSetName'; | |
export default class objectClone extends LightningElement { | |
@api objectApiName; | |
@api fieldsToFetchFieldSet; | |
@wire(getFieldSetFieldsByFieldSetName, {objectApiName: '$objectApiName', fieldSetName: '$fieldsToFetchFieldSet'}) | |
wiredFieldsToFetchFieldSet; | |
} |
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 Utilities | |
{ | |
@AuraEnabled(cacheable = true) | |
public static List<FieldSetField> getFieldSetFieldsByFieldSetName(String objectApiName, String fieldSetName) | |
{ | |
List<Schema.FieldSetMember> fieldSetMembers = ((SObject)Type.forName(objectApiName).newInstance()).getSObjectType().getDescribe().FieldSets.getMap().get(fieldSetName).getFields(); | |
//Equal to: | |
//List<Schema.FieldSetMember> fieldSetMembers = Schema.getGlobalDescribe().get(objectName).getDescribe().FieldSets.getMap().get(fieldSetName).getFields(); | |
List<FieldSetField> fields = new List<FieldSetField>(); | |
for (Schema.FieldSetMember fieldSetMember : fieldSetMembers) | |
{ | |
FieldSetField fieldSetField = new FieldSetField(fieldSetMember); | |
fields.add(fieldSetField); | |
} | |
return fields; | |
} | |
} |
You haven't included the definition of the custom class FieldSetField.
Why not just using Schema.FieldSetMember?
When attempting to return Schema.FieldSetMember or even Schema.FieldSet through an @AuraEnabled method you get the following errors:
main/default/classes/fieldSetTestsController.cls AuraEnabled methods do not support return type of List<Schema.FieldSetMember> (3:40)
main/default/classes/fieldSetTestsController.cls AuraEnabled methods do not support return type of Schema.FieldSet (12:28)
FieldSetField is just a wrapper of a Schema.FieldSetMember. I'll add it for demonstration purposes.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You haven't included the definition of the custom class FieldSetField.
Why not just using Schema.FieldSetMember?