You need to sign in to do that
Don't have an account?
sumit d
how to get the object name in helper class
Hi All,
i have a helper class which i am calling from Opportunity trigger.
i want to made it generic so that i can call it from any trigger and it track the field history. right now i am not able to get the object in which the trigger is running
my trigger is given below:-
trigger Trigger_Opportunity on Opportunity (before insert, before update, before delete,
after insert, after update, after delete) {
OpportunityTriggerHelper.newOpportunity = trigger.new;
OpportunityTriggerHelper.oldOpportunity = trigger.old;
OpportunityTriggerHelper.newMapOpportunity = trigger.newMap;
OpportunityTriggerHelper.oldMapOpportunity = trigger.oldMap;
if( Trigger.isAfter ) {
if(Trigger.isUpdate ){
GenericTriggerClass.GenericClassMethod(trigger.oldMap, trigger.newMap);
}
}
}
my helper is given below:-
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);
System.assert(FALSE,MapToFieldHistory);
}
FieldTracking__c ft = new FieldTracking__c();
List<String> ListFieldTracking = new List<String>();
String fieldValues = String.valueOf(MapToFieldHistory.get(ft.Selectedobject__c).selectedField__c);
//System.assert(FALSE,ft.Selectedobject__c);
ListFieldTracking.addAll(fieldValues.split(','));
return ListFieldTracking;
}
}
here at bold line i am not getting the particular object in which i am calling the trigger here like it should be opportunity
how to get the object?
Any suggestion?/
i have a helper class which i am calling from Opportunity trigger.
i want to made it generic so that i can call it from any trigger and it track the field history. right now i am not able to get the object in which the trigger is running
my trigger is given below:-
trigger Trigger_Opportunity on Opportunity (before insert, before update, before delete,
after insert, after update, after delete) {
OpportunityTriggerHelper.newOpportunity = trigger.new;
OpportunityTriggerHelper.oldOpportunity = trigger.old;
OpportunityTriggerHelper.newMapOpportunity = trigger.newMap;
OpportunityTriggerHelper.oldMapOpportunity = trigger.oldMap;
if( Trigger.isAfter ) {
if(Trigger.isUpdate ){
GenericTriggerClass.GenericClassMethod(trigger.oldMap, trigger.newMap);
}
}
}
my helper is given below:-
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);
System.assert(FALSE,MapToFieldHistory);
}
FieldTracking__c ft = new FieldTracking__c();
List<String> ListFieldTracking = new List<String>();
String fieldValues = String.valueOf(MapToFieldHistory.get(ft.Selectedobject__c).selectedField__c);
//System.assert(FALSE,ft.Selectedobject__c);
ListFieldTracking.addAll(fieldValues.split(','));
return ListFieldTracking;
}
}
here at bold line i am not getting the particular object in which i am calling the trigger here like it should be opportunity
how to get the object?
Any suggestion?/
public string objecttype;
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()){
objecttype=string.valueof(sObj.getsobjecttype());
}
//Use anywhere like
a=objecttype;
If helpful, not forget to mark as solution/best answer.
All Answers
You can get sobject type on the top by Using getsobjecttype() like this
String objecttype=sObj.getsobjecttype();
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved.
Thanks and Regards
Pavit Siddhu
its not working GetSObjectType() is used for Schema.SObject not for string .
suggest me something else
Any suggestions?
public string objecttype;
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()){
objecttype=string.valueof(sObj.getsobjecttype());
}
//Use anywhere like
a=objecttype;
If helpful, not forget to mark as solution/best answer.
can you explain it in my class?