Activity Forums Salesforce® Discussions How do you identify what field values changed for a record in APEX Triggers?

  • Yogesh

    Member
    November 26, 2019 at 9:22 am

    Hello,

    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,

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos