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
Sandile matheSandile mathe 

FieldSet Query

Hi, I have a fieldSet on Contact Object, So On Apex Class I want to check that if One of the Fields (in Fieldset) is updated then Call my methodXXX, How do I do this in Apex Class, please help
AnudeepAnudeep (Salesforce Developers) 
You can use Schema.FieldSetMember class to get details about fields contained within a fieldset and write your logic based on that
 
List<Schema.FieldSetMember> fields = Schema.SObjectType.Account.fieldSets.getMap().get('field_set_name').getFields();
Here is a sample code
trigger checkIfFieldIsUpdated on Contact(after update) {
  for(Contact con: Trigger.new) {
        Contact oldContact = Trigger.oldMap.get(con.ID);
        for(Schema.FieldSetMember fld :SObjectType.Account.FieldSets.accountFieldSet.getFields()) {        
        if(fld.getLabel()==oldContact.'your field name') {
            //call your method here        
        }        
     }
  

I don't do a bunch of Apex coding, so forgive me if there's an error.  Hopefully, this will give you the idea though

Anudeep
Agustin BAgustin B
Hi sandile, check this out:https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_fieldsets_describe.htm

The idea would be to get all the field sets and if I understand properly use a trigger like this(Example with Account):
Map<String, Schema.FieldSet> FsMap =    Schema.SObjectType.Account.fieldSets.getMap();

 for(Integer i = 0; i < newAccountsList.size(); i++){
                Account newAcc = newAccountsList.get(i);
                Account oldAcc = oldAccountsList.get(i);
//iterate the map to see if a value changed from newAcc to oldAcc
}

If it helps please like and mark as correct, it may help others facing the same issue.