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
gaganSFDCgaganSFDC 

Create Generic method for 2 different object having same fields api

Hi, i want to merge these methods and create one generic method, so that i will have to pass the type of object only as both the object having same fields.

//update Lead
private static void updateLead(Map<String,String> dataMap){      
        Lead obj = new lead();
        obj.Id= dataMap.get('Id');            
        obj.Current_Assets_Type_of_Charge__c = 'abcd';
        obj.Single_state_Remarks__c = 'xyz';
        obj.More_than_3_months__c =  'pqr';
        obj.More_than_3_months_Remarks__c ='asdf';
        update obj;
    }

//update opportunity
private static void updateOpportunity(Map<String,String> dataMap){     
        Opportunity obj = new Opportunity();
        obj.Id= dataMap.get('Id');            
        obj.Current_Assets_Type_of_Charge__c = 'abcd';
        obj.Single_state_Remarks__c = 'xyz';
        obj.More_than_3_months__c =  'pqr';
        obj.More_than_3_months_Remarks__c ='asdf';
        update obj;
    }
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try as below,
public class updateObject123 {
private static void updateObject(Map<String, String> dataMap) {
    For(String objectname:dataMap.keyset() ){
        
        if(objectname=='Lead'){
            
            Lead ls= new Lead();
            ls.id=dataMap.get(objectname);
             
            ls.Current_Assets_Type_of_Charge__c = 'abcd';
        ls.Single_state_Remarks__c = 'xyz';
        ls.More_than_3_months__c =  'pqr';
        ls.More_than_3_months_Remarks__c ='asdf';
        update ls;
        }
                if(objectname=='Opportunity '){
            
            Opportunity  opp= new Opportunity ();
            opp.id=dataMap.get(objectname);
             opp.Current_Assets_Type_of_Charge__c = 'abcd';
        opp.Single_state_Remarks__c = 'xyz';
        opp.More_than_3_months__c =  'pqr';
        opp.More_than_3_months_Remarks__c ='asdf';
        update opp;
        }
        
    }
}

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
gaganSFDCgaganSFDC
Hi, Sai Thanks for your reply but i am not looking for if/ else condition instead a common code.