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
Deepak Singh 116Deepak Singh 116 

How I can use generic object to update records in object

Hi Experts,
Below is my code snippet. I am Passing Account as object when calliing this below batch . I want to optimize it becuase the update DML is repeated vey time for different objcet. Is there any way to just get the record as per objects passed and updated uing only one DML .
public with sharing class BatchApex implements Database.Batchable<SObject>,Database.stateful {
    String ObjectName=null;
    public BatchApex(String ObjectName) {
        this.ObjectName=ObjectName;    
    }
    public Database.QueryLocator start(Database.BatchableContext BC){
        String Query=null;
        if(ObjectName!=null && ObjectName=='Account'){
            Query='select Id,Name,abc__c from '+ObjectName+' Where abc__c=null';
        }else If(ObjectName!=null && ObjectName=='Contact'){
            Query='select Id,FirstName,LastName,abc__c,abcLn__c from '+ObjectName+' Where  ((abc__c=null and FirstName!=null) or (abcLn__c=null and LastName!=null))';  
        }else If(ObjectName!=null && ObjectName=='Lead'){
            Query='select Id,FirstName,LastName,abc__c,abcLn__c from '+ObjectName+' Where ((abc__c=null and FirstName!=null) or (abcLn__c=null and LastName!=null))';  
        }
        else If(ObjectName!=null && ObjectName=='opportunity'){
            Query='select id,Account.name,Name,abc__c,abcAn__c from '+ObjectName+' Where (abcAn__c=null and Accountid!=null) or abc__c=null';  
        }
        return Database.getQueryLocator(Query);
    }
    
    public void execute(Database.BatchableContext BC,List<SObject> Scope){
        if(ObjectName!=null && ObjectName=='Account'){
            List<Account> AccountList=(List<Account>)Scope; 
            List<Account> AccounttoUpdate= New List<Account>();
            for(Account acc:AccountList){
                if(acc.Name!=null && acc.abc__c==null){
                    acc.abc__c=generatorClass.getvalue(acc.Name);
                    AccounttoUpdate.add(acc);
                }
            }
            if(AccounttoUpdate.size()>0){
                Database.SaveResult[] results = Database.Update(AccounttoUpdate, false);
                for (database.SaveResult SR:results){
                    if(SR.isSuccess()){
                        
                        System.debug(sr.getId());
                        
                    }else {
                        for(Database.Error err:SR.errors){
                            System.debug(sr.getId());
                            System.debug(err.getFields());
                            System.debug(err.getMessage());
                            
                        }
                    } 
                }
            }
        }else if(ObjectName!=null && ObjectName=='Contact'){
            List<Contact> ContactList=(List<Contact>)Scope; 
            List<Contact> ContacttoUpdate= New List<Contact>();
            for(Contact con:ContactList){
                if(con.FirstName!=null && con.abc__c==null){
                    con.abc__c=generatorClass.getvalue(con.FirstName);
                }
                if(con.LastName!=null &&  con.abcLn__c==null){
                    con.abcLn__c=generatorClass.getvalue(con.LastName);
                }   
                ContacttoUpdate.add(con);
            }
            system.debug(ContacttoUpdate.size());
            if(ContacttoUpdate.size()>0){
                Database.SaveResult[] results = Database.Update(ContacttoUpdate, false);
                for (database.SaveResult SR:results){
                    if(SR.isSuccess()){
                        
                        System.debug(sr.getId());
                        
                    }else {
                        for(Database.Error err:SR.errors){
                            System.debug(sr.getId());
                            System.debug(err.getFields());
                            System.debug(err.getMessage());
                            
                        }
                    }
                } 
            }
        } else if(ObjectName!=null && ObjectName=='Lead'){
            List<Lead> LeadList=(List<Lead>)Scope; 
            List<Lead> LeadtoUpdate= New List<Lead>();
            for(Lead lead:LeadList){
                if(lead.FirstName!=null && lead.abc__c==null){
                    lead.abc__c=generatorClass.getvalue(lead.FirstName);
                }
                if(lead.LastName!=null && lead.abcLn__c==null){
                    lead.abcLn__c=generatorClass.getvalue(lead.LastName);
                }
                LeadtoUpdate.add(lead);
            }
            if(LeadtoUpdate.size()>0){
                Database.SaveResult[] results = Database.Update(LeadtoUpdate, false);
                for (database.SaveResult SR:results){
                    if(SR.isSuccess()){
                        
                        System.debug(sr.getId());
                        
                    }else {
                        for(Database.Error err:SR.errors){
                            System.debug(sr.getId());
                            System.debug(err.getFields());
                            System.debug(err.getMessage());
                        }
                    }
                    
                } 
            }
        }else if(ObjectName!=null && ObjectName=='opportunity'){
            List<opportunity> opportunityList=(List<opportunity>)Scope; 
            List<opportunity> opportunitytoUpdate= New List<opportunity>();
            for(opportunity op:opportunityList){
                if(op.Name!=null && op.abc__c==null){
                    op.abc__c=generatorClass.getvalue(op.Name);  
                }
                if(op.AccountId!=null && op.abcAn__c==null){
                    op.abcAn__c=generatorClass.getvalue(op.Account.name);
                }
                opportunitytoUpdate.add(op);
            }
            if(opportunitytoUpdate.size()>0){
                Database.SaveResult[] results = Database.Update(opportunitytoUpdate, false);
                for (database.SaveResult SR:results){
                    if(SR.isSuccess()){
                        
                        System.debug(sr.getId());
                        
                    }else {
                        for(Database.Error err:SR.errors){
                            System.debug(sr.getId());
                            System.debug(err.getFields());
                            System.debug(err.getMessage());
                            
                        }
                    }
                } 
            }
        }
        
    }
    public void finish(Database.BatchableContext BC){
        if(ObjectName!=null && ObjectName=='Account'){
            BatchApex SBA=new BatchApex('Contact');
            database.executeBatch(SBA,1500);
        }
        else if(ObjectName!=null && ObjectName=='Contact'){
            BatchApex SBA=new BatchApex('Lead');
            database.executeBatch(SBA,1500);
        }
        else if(ObjectName!=null && ObjectName=='Lead'){
            BatchApex SBA=new BatchApex('opportunity');
            database.executeBatch(SBA,1500);
        } else{
            system.debug('Values Account,COntact,Opportunity,Lead has been updated');
        }
        
    }
}
AbhishekAbhishek (Salesforce Developers) 
Try methods as mentioned in the below developer discussions,

https://salesforce.stackexchange.com/questions/4193/update-a-records-using-generic-fields

https://stackoverflow.com/questions/58180175/error-while-update-object-in-generic-method

https://www.forcetalks.com/salesforce-topic/using-generic-sobject-to-change-fields-of-different-object-types/


I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.