1

I'm trying to do some actions like calling a class when an only separate set of fields are modified on an object.

What I did

I'm using trigger.old and trigger.new to compare values of old and new for each and every field to call the class. The challenger here is if I need to add one more field to it I need to repush the whole class for adding one field

Why I am not doing through point & click

I can use ISchanged from process and builder and perform the action but I need the old and new values to see history.

What I am trying to do

Create a custom metadatatype with Label and API name and somehow compare this in trigger context to make it scalable.

Where I am stuck

I have added the label and API name to a map and used Schema.SObjectType.Account.fields.getMap() to get Api name , label pair but I am unable to figure out how to compare these.

Any help is much appreciated.

1 Answer 1

2

There's actually a more appropriate choice here: Field Sets. Custom Metadata is a separate type of data, that you don't need to use in this case. You can reference them in code easily.

FieldSetMember[] fields = sObjectType.Account.fieldSets.getMap().get('fieldsetname').getFields();
for(Integer i = 0, s = Trigger.new.size(); i < s; i++) {
  for(FieldSetMember fieldMember: fields) {
    sObjectField field = fieldMember.getSObjectField();
    if(Trigger.old[i].get(field) != Trigger.new[i].get(field)) {
      // Do stuff here //
    }
  }
}
1
  • Thank you very much @sfdcfox . I've implemented my solution with fieldsets and it is working very smooth and ofcourse in scalable format. Really learned new things
    – Badduboy
    Aug 11, 2020 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.