Skip to content

Instantly share code, notes, and snippets.

@vspruyt-sol
Last active April 19, 2021 13:40
Show Gist options
  • Save vspruyt-sol/028e0a6a7ced39e23de310e8efac7761 to your computer and use it in GitHub Desktop.
Save vspruyt-sol/028e0a6a7ced39e23de310e8efac7761 to your computer and use it in GitHub Desktop.
Importing a fieldset in LWC
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());
}
}
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;
}
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;
}
}
@vspruyt-sol
Copy link
Author

@ArielS1

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