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
Marry SteinMarry Stein 

Call List<SObject> inside Batch job

Hello Community,

i have created a class which handles customer activities. It checks all activities within the customer relationship. A separate object (Activity Template) defines which activities should be available. If an activity is missing, the class creates a new one. If there is a unneeded activity, the class marks it as "to be deleted".  The class pushes the records in a list and do an upsert call (create new records and update unneeded record).This class will be run once a week (scheduled).

To play it save, i want to create a batch job, but this one is different from my previous ones. Normally i query all records (usallly one object) in my start method and use them in my execute methode. (Call a separate class and use the scope as parameter).

This time i have the whole logic in my separate class, which queries different objects (Tasks, Events, Opportunities), do sth with it and finaly return a list of a list of SObjects (Task and Events).

Now i wonder, what is the best way to handle this? Just call the class in my start method and use the execute methode for the upsert?
public with sharing class BatchTaskCreation implements Database.Batchable<SObject>{

    public List<SObject> start(Database.BatchableContext bc) {

    	// call method to retrieve list of sObjects

        GetActivitiesPerCustomer gapc = new GetActivitiesPerCustomer();

        return gapc.getSObjectList();
    }
    public void execute(Database.BatchableContext bc, List<SObject> scope){

        List<SObject> objectsToUpdate = scope;
        recordsProcessed = scope.size();

        // just execute the upsert 
        upsert scope;

    }
    public void finish(Database.BatchableContext bc) {
        System.debug(recordsProcessed + ' records processed');
    }
}

To sum up, GetActivitiesPerCustomer is a really complex class which queries different objects (overall less than 50k), proccess them and finally returns a list of SObjects. Because the number of records in the list is really flexible, i want to start with a batch job, make some data analysis in the next few weeks/month and plan my next steps.
Best Answer chosen by Marry Stein
ShivankurShivankur (Salesforce Developers) 
Hi Marry,

Thanks for the acknowledgement.

I have found similar implementation ask where list of Sobjects from an apex class are retrieved in Start method and update or upsert is performed in Execute method.You could use it in similar way.

Hope this example could help you more to achieve similarly in your case.

Refer:
https://salesforce.stackexchange.com/questions/217212/passing-a-list-of-custom-objects-to-batch-apex

Thanks.

All Answers

ShivankurShivankur (Salesforce Developers) 
Hi Marry,

Please check out the standard documentation around Schedulable apex and Batchable Interface for your implementation:

Implementing the Schedulable Interface:
https://developer.salesforce.com/docs/atlas.en-us.232.0.apexcode.meta/apexcode/apex_scheduler.htm
Implementing the Database.Batchable Interface:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm#apex_batch_scheduleBatch_section

The code examples over these links will surely help you implementing specific to your requirement.Please try to fit in your logic within those code samples and you should be able to achieve it.

Hope above information helps. Please mark as Best Answer so that it can help others in future.

Thanks.
Marry SteinMarry Stein

Hi Shivankur,

thanks for your response, i know how to crate a scheduled job and a batch job, but the docs is not really helpful for my need. Unfortunately i can't do much with this example "Using an Iterable in Batch Apex to Define Scope" .
 

I want to know, what is the best way to handle complex logic inside a batch job? I have tried to describe my question in as much detail as possible, so I would appreciate feedback. If you need more information, please feel free to contact me.

ShivankurShivankur (Salesforce Developers) 
Hi Marry,

Thanks for the acknowledgement.

I have found similar implementation ask where list of Sobjects from an apex class are retrieved in Start method and update or upsert is performed in Execute method.You could use it in similar way.

Hope this example could help you more to achieve similarly in your case.

Refer:
https://salesforce.stackexchange.com/questions/217212/passing-a-list-of-custom-objects-to-batch-apex

Thanks.
This was selected as the best answer
Marry SteinMarry Stein
Hi Shivankur,

thank you for your effort! So if i understand the example correct, my first guess above was right? :)