Saturday, 18 June 2016

Identify which fields are changed on the record

Hi to Salesforce ,

Here is the small snippet that can help to identify which field is changed on the record using sobject methods.

   list<string> lstAPIfields = new list<string>();
            Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
            Map <String, Schema.SObjectField> fieldMap = schemaMap.get('Lead').getDescribe().fields.getMap();
            for(Schema.SObjectField sfield : fieldMap.Values()){
                 schema.describefieldresult dfield = sfield.getDescribe();
                 if(dfield.isUpdateable()){
                    lstAPIfields.add(dfield.getname());
                 }
            }
            system.debug('****lstAPIfields'+lstAPIfields);
            for(lead objlead:lstNew){
                sobject sobjNew = objlead;
                sobject sobjOld = mpOld.get(objlead.id);
                for(string strfield:lstAPIfields){
                    if(sobjNew.get(strfield)!=sobjOld.get(strfield)){
                        system.debug('***********FIELD MODIFIED'+strfield);
                    }
                }
            }


Thank you.