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
SFDC ROCKSFDC ROCK 

How to set up batch apex logic with trigger logic

I have a trigger with below logic with that logic I want to create batch apex  and schedule it daily.
Can we do it with below logic or need to write complete different code ?


public class handlerclass{
public static void methodname(List<sbject> newlist,Map<Id,sobject> oldmaplist){

    set<Id> oppIds = new  set<Id>();
    Map<Id,osbject1> oppupdate = new Map<Id,osbject1 >();
    for(sobject__c so: newlist){
        
        if(so.sobject1Id__c!=null)
           oppIds.add(so.sobect1Id__c);

    }

        if(!oppIds.isEmpty()){
            for(sobeject1 oppty : [Select name,id,address__c from osbject1 where uid='frame']){
                    
                    if(condition){
                        
                        for(loop){
                            if(condition){
                                
                                if(condition);
                                   //assigment logic';
                                   
                                    //update logic
                                    break;
                                }
                                if(condition){
                                    //assigment logic';
                                   
                                    //update logic;
                                    break;
                                }
                                if(condition){
                                    //assigment logic';
                                   
                                    //update logic;
                                   
                               }
                            }
                        }
                    }
Ajay K DubediAjay K Dubedi
Hi 
Subodh,
You can implement your logic by using following code according to your requirement

Batch Class:
global class MyBatchClass implements Database.Batchable<sObject> {
    global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {
        // collect the batches of records or objects to be passed to execute
    }
    global void execute(Database.BatchableContext bc, List<P> records){
        // process each batch of records
    }    
    global void finish(Database.BatchableContext bc){
        // execute any post-processing operations
    }    
}

Schedule Class:

global class BatchScheduleUpdate implements Schedulable
{
    global void execute(SchedulableContext sc)
    {
        // Implement any logic to be scheduled
       
        // We now call the batch class to be scheduled
        MyBatchClass b = new MyBatchClass ();
       
        //Parameters of ExecuteBatch(context,BatchSize)
        database.executebatch(b,200);
    }
   
}


Schedule from Developer console:
BatchScheduleUpdate batchSch=new BatchScheduleUpdate();
//Daily Schedule on 10AM
String sch='0 0 22 * * ? *';
System.schedule('Batch Schedule', sch , batchSch);
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com