Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have 30+ fields in a field set, and in the update part of my trigger, I want to see if any of those fields have been changed. Can I access them using field sets? Anyway to do it, short of writing out every field in my trigger?

I'm doing something similar to this:

share|improve this question
up vote 9 down vote accepted

I am not sure if you could use the field set directly to compare but you can use the sObject's get method to do the job

set<String> fieldSet = new Set<String>();
//dynamically get the fields from the field set and then use the same for comparison in the trigger. 
for(Schema.FieldSetMember fields :Schema.SObjectType.Account.fieldSets.getMap().get('yourFieldSetName').getFields()){
for(account a:{
    set<String> changedFieldSet  = new Set<String>();
    for(string s: fieldSet){
        if(a.get(s) != trigger.oldMap.get(a.Id).get(s)){
            changedFieldSet.add(s);//adding fields whose value changed
        //do something
share|improve this answer
updated my answer. Hope it helps! – Anamadeya Jun 10 '14 at 15:57

SObjects have map-like methods available including get that takes a String or SObjectField argument.

So you can write a trigger of this form:

trigger MyTrigger on MyObject__c (before update) {
    String[] fieldNames = new String[] {'Field__c', 'Field2__c', ...};
    for (MyObject__c mo : {
        MyObject__c old = Trigger.oldMap.get(mo.Id);
        for (String f : fieldNames) {
            if (mo.get(f) != old.get(f)) {
                // Do whatever you want here
share|improve this answer
in your case here what if the values of the fields are case sensitive? Will I be able to use equals string method? – Dec 10 '15 at 5:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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