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

Use only a Batch for multiple sObjects records

I want to create a schedule batch that will take records from multiple Objects and update a field. The objects for which the batch will run are specified in custom metadata.

I've considered using an Iterable batch, but using that approach would require looping over the custom metadata records and querying for the records of each object. However, running a query inside a loop would exceed governor limits.

Does using an Iterable batch limit the number of records compared to using a database.queryLocator? The database.queryLocator approach allows processing up to 50 million records. What are the limitations with the Iterable approach?

Do you have any other suggestion?

In your scenario, you want to create a scheduled batch job that updates a field for records from multiple objects. The objects for which the batch will run are specified in custom metadata. You are considering using an Iterable batch, but you're concerned about governor limits and the limitations of this approach compared to using a database.queryLocator.
When using an Iterable batch, you would need to loop over the custom metadata records and query for the records of each object individually. However, running a query inside a loop can potentially exceed governor limits, such as the total number of SOQL queries allowed or the number of records processed.
To address your concerns and improve performance, you can consider the following suggestions:
1) Query Optimization: Instead of querying for records of each object individually, you can try to optimize your queries to retrieve the necessary data in single or fewer queries. Consider using relationship queries or leveraging other query features to minimize the number of queries executed.
2) Bulkification: Ensure that your batch job processes record in bulk rather than one at a time. This means leveraging collections, such as lists or sets, to accumulate and process records in larger batches, reducing the overall number of DML operations performed.
3) Custom Metadata Usage: Custom metadata can help store configuration data, but you should be cautious when using it within a loop. Consider retrieving the relevant metadata records outside the loop and storing them in a collection for efficient access.
4) Asynchronous Processing: If your batch job requires updating a significant number of records, consider breaking it down into smaller chunks and processing them asynchronously using Queueable or future methods. This approach allows for better control over governor limits and can improve overall performance.
By following these suggestions, you can design a more efficient and scalable batch process that updates records from multiple objects while staying within the governor's limits.