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
WilmerWilmer 

How to detect dynamically which field has changed in an UPDATE trigger event?

Hi everybody,

 

I'm working on a new trigger in the UPDATE event and we need to identify specifically which fields of that object have changed its values. I know there is a comparison between trigger.new and trigger.old objects but as I can see, it is required to hardcode the field name to know what we are asking for.

 

Is it possible that catch that behavior dynamically? 

 

Regards,

 

Wilmer

Best Answer chosen by Admin (Salesforce Developers) 
WilmerWilmer

Well, finally thanks to some friends from Salesforce support team I got a solution and I'm gonna share it to everybody.

 

Here is the sample code:

 

trigger UpdateTriggerSample_tgr on Lead (after update) { Lead NewLead = trigger.new[0]; Lead OldLead = trigger.old[0]; Lead LeadObject = new Lead(); // This takes all available fields from the required object. Schema.SObjectType objType = LeadObject.getSObjectType(); Map<String, Schema.SObjectField> M = Schema.SObjectType.Lead.fields.getMap(); for (String str : M.keyset()) { try { System.debug('Field name: '+str +'. New value: ' + NewLead.get(str) +'. Old value: '+OldLead.get(str)); if(NewLead.get(str) != OldLead.get(str)){ system.debug('******The value has changed!!!! '); // here goes more code } } catch (Exception e) { System.debug('Error: ' + e); } } }

 

 Thanks,

 

 

Wilmer

 

 

All Answers

WilmerWilmer

Well, finally thanks to some friends from Salesforce support team I got a solution and I'm gonna share it to everybody.

 

Here is the sample code:

 

trigger UpdateTriggerSample_tgr on Lead (after update) { Lead NewLead = trigger.new[0]; Lead OldLead = trigger.old[0]; Lead LeadObject = new Lead(); // This takes all available fields from the required object. Schema.SObjectType objType = LeadObject.getSObjectType(); Map<String, Schema.SObjectField> M = Schema.SObjectType.Lead.fields.getMap(); for (String str : M.keyset()) { try { System.debug('Field name: '+str +'. New value: ' + NewLead.get(str) +'. Old value: '+OldLead.get(str)); if(NewLead.get(str) != OldLead.get(str)){ system.debug('******The value has changed!!!! '); // here goes more code } } catch (Exception e) { System.debug('Error: ' + e); } } }

 

 Thanks,

 

 

Wilmer

 

 

This was selected as the best answer
sebastiangnagnarellasebastiangnagnarella
That trigger needs to be bulkified
igoruigoru
// bulkified version :)
// P.S. Be aware - do NOT use such scripts on constant basic cos' getMap method using a lot of cpu time (for example)

if(Trigger.isUpdate)
    {
        Group_Participation_List__c gplObject = new Group_Participation_List__c(); // This takes all available fields from the required object. 
        Schema.SObjectType objType = gplObject.getSObjectType(); 
        Map<String, Schema.SObjectField> mapFields = Schema.SObjectType.Group_Participation_List__c.fields.getMap(); 
        
        for(Group_Participation_List__c gpl : trigger.new)
        {
            Group_Participation_List__c oldGPL = trigger.oldMap.get(gpl.Id);

            for (String str : mapFields.keyset()) 
            { 
                try 
                { 
                    if(gpl.get(str) != oldGPL.get(str))
                    { 
                        System.Debug('IGORU Field changed: ' + str + '. The value has changed from: ' + oldGPL.get(str) + ' to: ' + gpl.get(str)); 
                    } 
                } 
                catch (Exception e) 
                { 
                    System.Debug('Error: ' + e); 
                } 
            }
        }
    }