You need to sign in to do that
Don't have an account?
sumit 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?
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?
Try below code regards