+ Start a Discussion
sumit dsumit d 

Change method into generic

Hi All,
i have a helper class which is working fine on contact to track history now i want to change it into generic class
which should work for every object not only contact 
my class is given below:-
public class GenericClass {
    public static List<Contact> newContact = new List<Contact>();
    public static List<Contact> oldContact = new List<Contact>();
    public static Map<Id, Contact> newMapContact = new Map<Id, Contact>();
    public static Map<Id, Contact> oldMapContact = new Map<Id, Contact>();
    
    public static void GenericClassMethod(Map<Id, Contact> oldMapContact, Map<Id, Contact> newMapContact){
        List<History_Tracking__c> historiesToInsert = new List<History_Tracking__c>();
        for(Contact con : newMapContact.values()){
            Contact OldCon = oldMapContact.get(con.Id);
            System.debug('con'+con);
            System.debug('oldCon'+OldCon);
            for (String conField : ContactHistory()) {
                
                History_Tracking__c conHistory = createUpdateHistory(conField, oldCon, con);
                historiesToInsert.add(conHistory);
                System.debug('ConHistory'+conHistory);
                 System.debug('histories'+historiesToInsert);
            }
            
        }
        
        
        if(!historiesToInsert.isEmpty()) {
            List<History_Tracking__c> historiesToInsertWithoutDuplicates = new List<History_Tracking__c>();
            Set<History_Tracking__c> historiesSet = new Set<History_Tracking__c>();
            historiesSet.addAll(historiesToInsert);
            historiesToInsertWithoutDuplicates.addAll(historiesSet);
            
            insert historiesToInsertWithoutDuplicates;
            System.debug('ConRecord'+historiesToInsertWithoutDuplicates);
        }
    }
     private static History_Tracking__c createUpdateHistory(String field, Contact oldCon, Contact newCon) {
        History_Tracking__c      conHistory = new History_Tracking__c();
        conHistory.FieldName__c = field;
        conHistory.ObjectName__c = 'Contact';
        conHistory.ObjectId__c = oldCon.Id;
        String oldValue = String.ValueOf(oldCon.get(field));
        String newValue = String.ValueOf(newCon.get(field));
        
        conHistory.OldValue__c = oldValue;
        conHistory.NewValue__c = newValue;
        if (conHistory.OldValue__c != null) conHistory.OldValue__c = conHistory.OldValue__c.abbreviate(255);
        if (conHistory.NewValue__c != null) conHistory.NewValue__c = conHistory.NewValue__c.abbreviate(255);
        return conHistory;
    }

    public static  List<String> ContactHistory(){
        Map<String,FieldTracking__c> MapToFieldHistory = new Map<String,FieldTracking__c>();
        for(FieldTracking__c ft: [Select  Id, selectedField__c,Selectedobject__c from FieldTracking__c]){
            MapToFieldHistory.put(ft.Selectedobject__c, ft);
        }
        List<String> ListFieldTracking = new List<String>();
        String fieldValues =  String.valueOf(MapToFieldHistory.get('Contact').selectedField__c);
        ListFieldTracking.addAll(fieldValues.split(','));
        return ListFieldTracking;
    }
}
how to do it?
Any suggestions?
Ankit SehgalAnkit Sehgal
Create a list public static List<sObject> newObject = new List<sObject>(); instead of contact
sumit dsumit d
Hi Ankit,
i did like this
public class GenericTriggerClass {
    public static List<SObject> newSObject = new List<SObject>();
    public static List<SObject> oldSObject = new List<SObject>();
    public static Map<Id, SObject> newMapSObject = new Map<Id, SObject>();
    public static Map<Id, SObject> oldMapSObject = new Map<Id, SObject>();
    
    public static void GenericClassMethod(Map<Id, SObject> oldMapSObject, Map<Id, SObject> newMapSObject){
        List<History_Tracking__c> historiesToInsert = new List<History_Tracking__c>();
        for(SObject sObj : newMapSObject.values()){
            SObject OldsObj = oldMapSObject.get(sObj.Id);
            System.debug('con'+sObj);
            System.debug('oldCon'+OldsObj);
            for (String sObjField : SObjectHistory()) {
                
                History_Tracking__c sObjHistory = createUpdateHistory(sObjField, oldsObj, sObj);
                historiesToInsert.add(sObjHistory);
                System.debug('ConHistory'+sObjHistory);
                 System.debug('histories'+historiesToInsert);
            }
            
        }
        
        
        if(!historiesToInsert.isEmpty()) {
            List<History_Tracking__c> historiesToInsertWithoutDuplicates = new List<History_Tracking__c>();
            Set<History_Tracking__c> historiesSet = new Set<History_Tracking__c>();
            historiesSet.addAll(historiesToInsert);
            historiesToInsertWithoutDuplicates.addAll(historiesSet);
            
            insert historiesToInsertWithoutDuplicates;
            System.debug('ConRecord'+historiesToInsertWithoutDuplicates);
        }
    }
     private static History_Tracking__c createUpdateHistory(String field, SObject oldsObj, SObject newsObj) {
        History_Tracking__c      sObjHistory = new History_Tracking__c();
        sObjHistory.FieldName__c = field;
        sObjHistory.ObjectName__c = oldsObj.Id.getSObjectType().getDescribe().getName();

        sObjHistory.ObjectId__c = oldsObj.Id;
        String oldValue = String.ValueOf(oldsObj.get(field));
        String newValue = String.ValueOf(newsObj.get(field));
        
        sObjHistory.OldValue__c = oldValue;
        sObjHistory.NewValue__c = newValue;
        if (sObjHistory.OldValue__c != null) sObjHistory.OldValue__c = sObjHistory.OldValue__c.abbreviate(255);
        if (sObjHistory.NewValue__c != null) sObjHistory.NewValue__c = sObjHistory.NewValue__c.abbreviate(255);
        return sObjHistory;
    }

    public static  List<String> SObjectHistory(){
        Map<String,FieldTracking__c> MapToFieldHistory = new Map<String,FieldTracking__c>();
        for(FieldTracking__c ft: [Select  Id, selectedField__c,Selectedobject__c from FieldTracking__c]){
            MapToFieldHistory.put(ft.Selectedobject__c, ft);
        }
        FieldTracking__c ft = new FieldTracking__c();
        List<String> ListFieldTracking = new List<String>();
          String fieldValues =  String.valueOf(MapToFieldHistory.get(ft.Selectedobject__c).selectedField__c);
        ListFieldTracking.addAll(fieldValues.split(','));
        return ListFieldTracking;
    }

but its not working when i call his class from Any trigger like opportunity
Any suggestion?