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
d1c3m4nd1c3m4n 

Using Batch Apex to perform operations on multiple large objects

Is it possible to utilise Batch Apex to perform operations on multiple large objects thereby going around governor limits?

Understand that the Query Locator allows up to 50M records to be returned, but what if the requirement is to:

- Query large Object A

- Create records on Object B based on the number of records in the first query

- Query large Object C to match newly-created Object B records and update Object B fields accordingly

 

Can one batchable class support this? Or does it require multiple batchable classes?

sfdcfoxsfdcfox

Assuming I understand the requirements correctly, it sounds like you're looking for the following:

 

1) Batchable class D queries large object A.

2) D creates B records based on query.

3) Trigger E on B records queries large object C and places values in B records on insert.

 

On the other hand, if you only want the logic that E would produce when running D, then you'd probably want to query large object C in the execute method of class D and place those values in B on insert.

 

You should note that it is entirely possibly to call a query in the execute function against C, so long as it is selective (i.e. you can use an indexed field with a high cardinality). Your query limits are effectively reset back to zero with each call to execute, so feel free to include queries in that function (just don't exceed the governor limits).

 

On the other hand, if you feel that the B records must already completely exist before querying C, which would probably only make sense if the query on C depended on some sort of aggregation involving B, then you would need a second batch class.

 

I believe that the part you're missing is simply the fact that you will need to perform a selective query on C if you hope to make it efficient. You can contact Technical Support if you believe that C would benefit from a custom index so that you can query it the same time as when you're creating B records, either via trigger or in the batch class.

d1c3m4nd1c3m4n

thanks for your reply.

 

not planning to utilise a trigger for step 3. and yes, we might be able to combine the query on C to populate fields on B during insert as part of step 2, but  i believe the volumes are still large (eg > 100K). my initial understanding that anything within the execute method of the batchable goes around governor limits since it's treated as a batch request, but you mentioned to try not to hit governor limits which implies that it's not the case.  if so, that means i still can't do a query on C in the execute without breaking the number of records limit. is that correct?

 

 

 

sfdcfoxsfdcfox

The start method gets to perform one large query and return a locator. Each execute method gets a set of governor limits for each iteration (every 200 records); they are reset for each new call to execute. Take a look at http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm. The main query has the 50,000,000 record limit, while each loop through execute will get 1,000,000 script statements, 200 queries, and so on. Therefore, you must still plan on how to avoid reaching limits.