Thursday, April 13, 2017

How to find out which fields are changed on the record

elwo is the code which can used to identify which field is changed on the record using sobject methods.

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

No comments:

Post a Comment