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
sumit dsumit d 

test class for trigger helper history tracking

Hi All,
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);
            
            
            for (String conField : ContactHistory()) {
                
                History_Tracking__c conHistory = createUpdateHistory(conField, oldCon, con);
                historiesToInsert.add(conHistory);
                
            }
            
        }
        
        
        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;
        }
    }
    
    
    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 write a test class for it?
Any suggestions?
Best Answer chosen by sumit d
Sampath SuranjiSampath Suranji
Hi,
Try below code
@isTest
public class GenericClassTest {

    public static testmethod void  GenericClassMethodTest(){
        try{
            Account objAcc= new  Account(name='test acc');
            insert objAcc;
            contact objCon = new contact(lastname='test con',AccountId=objAcc.Id);
            insert objCon;
            Map<Id, Contact> newMapContact = new Map<Id, Contact>();
    		Map<Id, Contact> oldMapContact = new Map<Id, Contact>();
            
            newMapContact.put(objCon.Id, objCon);
            oldMapContact.put(objCon.Id, objCon);
            
            FieldTracking__c objFT = new FieldTracking__c(selectedField__c='LastName',Selectedobject__c='Contact');
            insert objFT;
            GenericClass.GenericClassMethod(oldMapContact,newMapContact);
            
        }
        catch(Exception ex){}
    }
}
regards