function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anshuma YadavAnshuma Yadav 

How to create & update object record Using Field Set in aura Component ?

Best Answer chosen by Anshuma Yadav
Darshit Pathak 10Darshit Pathak 10

Based on your question, it seems you need to populate predefined set of fields everytime when record is inserted or updated.
So You can create Field set on that object and fetch fields in apex using Schema methods.
Schema.FieldSet fSet = Schema.getGlobalDescribe().get(ObjectName).getDescribe().FieldSets.getMap().get(fieldSetName);
 

Let say you want to do this activity for Account.
Basically either it's Insert or Update, goal is to populate set of fields before DML.
Creating new instance here.
Account accountRec = new Account();
for (Schema.FieldSetMember fsm : fSet.getFields()) { //iterate over all fields in fieldset
       accountRec.put(fsm.getFieldPath(), VALUE); //getFieldPath() will return API name of the field
}
insert accountRec;
Similar approach can be taken for update as well.

Please mark this as Best Asnwer if it helps!

Regards,
Darshit Pathak

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Anshuma,

Refer the below links have solution for similar kind of ask.
https://metillium.com/2017/08/lightning-field-set-form-component/
https://pantherschools.com/how-to-use-field-set-in-lightning-component/

If this information helps, Please mark it as best answer.

Thanks!!
Darshit Pathak 10Darshit Pathak 10

Based on your question, it seems you need to populate predefined set of fields everytime when record is inserted or updated.
So You can create Field set on that object and fetch fields in apex using Schema methods.
Schema.FieldSet fSet = Schema.getGlobalDescribe().get(ObjectName).getDescribe().FieldSets.getMap().get(fieldSetName);
 

Let say you want to do this activity for Account.
Basically either it's Insert or Update, goal is to populate set of fields before DML.
Creating new instance here.
Account accountRec = new Account();
for (Schema.FieldSetMember fsm : fSet.getFields()) { //iterate over all fields in fieldset
       accountRec.put(fsm.getFieldPath(), VALUE); //getFieldPath() will return API name of the field
}
insert accountRec;
Similar approach can be taken for update as well.

Please mark this as Best Asnwer if it helps!

Regards,
Darshit Pathak

This was selected as the best answer