0

I still don't know if queries in execute batch are made for each record or batch execution. For instance:

global void execute(Database.BatchableContext BC, List<xR1_order__c> scope){
 
    SELECT....
 
    SELECT...
 
    SELECT...
 
    for(xR1_order__c order:scope){
    ...
    }

}

Let's assume that batch limit is 200 records. Will those queries be made 200 times even if they are not in the (for...scope) or just once for the 200 records?

Thanks so much

3
  • 3
    Does this answer your question? Behaviour of Batch Apex jobs when records modified during execution
    – Phil W
    Sep 30, 2021 at 18:44
  • While the questions are not the same, the answer links to a blog posting that should give you the insight you are looking for.
    – Phil W
    Sep 30, 2021 at 18:45
  • Yeah that's true Phil, but yoy have to dig into the link to find the answer. I'm not sure about duplicate question rules, and i'll be alright with any decision, but i think is easier to find the answer with my title and sfdcfox response Sep 30, 2021 at 20:07

1 Answer 1

2

Apex is Apex. It does exactly what you tell it to do, as do most programming languages. If you write your code to handle a bulk scope, each query should execute only once per execute method. Note that if you have a scope of 200, and you are processing 500 records, there will be three execute calls, and thus each query will be called three times--but not in the same transaction. Governor limits reset at the start of each execute call, so you don't have to worry about running into limits, as long as your code is optimized and each execute call does not individually exceed any limits.

2
  • Yeah, that's absolutely right. I don't how i didn't realize before but i really appreciate it @sfdcfox Sep 30, 2021 at 20:00
  • 1
    Ah, OK, so I misunderstood the question; I was assuming it was discussing how Salesforce queries the data in the "scope" list passed to the execute method, rather than queries performed in the execute method (I did wonder why there were "SELECT ..." lines in the pseudo code!).
    – Phil W
    Sep 30, 2021 at 20:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .