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 

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?/
Best Answer chosen by sumit d
Pavit SiddhuPavit Siddhu
Declare on top of the class with public or private access modifier. if you find that in loop values may be different the hold value in map<id, string> otherwise hold as a string and use where u want in the class. like this

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

Pavit SiddhuPavit Siddhu
Hello, Sumit
You can get sobject type on the top by Using getsobjecttype() like this
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()){
String objecttype=sObj.getsobjecttype();
}

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
sumit dsumit d
Hi Pavit,
        its not working GetSObjectType() is used for Schema.SObject not for string .
suggest me something else
Pavit SiddhuPavit Siddhu
It's Working fine here you can test by execute anonymous.
list<sobject> lstobj= new list<sobject>();
Account acc= new Account();
acc.name='acc';
lstobj.add(acc);
for(sobject obj:lstobj){
    system.debug('======'+obj.getsobjecttype());
}

 
sumit dsumit d
its giving me this error:-Illegal assignment from Schema.SObjectType to String
sumit dsumit d
can you make this trigger generic and work for every object trigger
Pavit SiddhuPavit Siddhu
String objecttype=string.valueof(sObj.getsobjecttype());
sumit dsumit d
how to use it aat the bold line? these are in diffrent method.
Any suggestions?
 
Pavit SiddhuPavit Siddhu
Declare on top of the class with public or private access modifier. if you find that in loop values may be different the hold value in map<id, string> otherwise hold as a string and use where u want in the class. like this

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.
This was selected as the best answer
sumit dsumit d
its not working pavit,
can you explain it in my class?