Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

With regard to the governor limit of 100 SOQL calls per transaction, does the below SOQL for loop consume one call per chunk or one call across all chunks combined?

Map<String, ID> campaignMap = new Map<String, ID>();
for (Campaign c : [SELECT CCID__c, Id FROM Campaign WHERE CCID__c IN :campaignCodes]) {
    campaignMap.put(c.CCID__c, c.Id);
}

I haven't had luck finding this explicitly stated in any Salesforce documentation.

share|improve this question
    
When you say chunks are you referring to the way triggers chunk into groups of 200 or the way a batch job chunks the data into chunks, or something else? –  drakored Feb 12 at 21:09
    
Are you really referring to: 'for (List<Campaign> campaigns: [SELECT Id FROM Campaign])'? –  dphil Feb 12 at 21:29
    
@dphil Regardless of whether the loop is processing a single record or a list of n records, the loop calls queryMore() to retrieve the next chunk if the total number of records returned from the query exceeds the chunk size. I'm interested to know if each call to queryMore() adds to the SOQL query limit for the transaction. –  Brian Feb 12 at 22:25
add comment

1 Answer

up vote 5 down vote accepted

If I am reading the question correctly what you are asking is if you ran this

for(Account a : [Select Id, Name From Account Limit 10]){
     //Your Code
}

If it was counted as 1 SOQL call or 10. If thats what your asking then its only counted as 1 SOQL query against the governor limits.

Use Apex anonymous and try this

for(Account a : [Select Id, Name From Account Limit 10]){
     system.debug('This is the account');
}

If you look at the debug logs for this you can see it only counted as a single SOQL query, not 10.

enter image description here

EDIT

You can increase the amount of records as well and as you can see it still only counts as a single call.

for(Account a : [Select Id, Name From Account Limit 5000]){
     //Your Code
}

Still only uses a single call. See the debug log for this call

enter image description here

share|improve this answer
    
Great – since this type of loop utilizes query() and queryMore(), do each of those API calls count toward a SOQL query or is only one still used regardless of how many times queryMore() is called? –  Brian Feb 12 at 22:19
    
I used Apex anonymous as suggested and confirmed it only counts as one SOQL call. If you update your answer to reflect this I'll accept! –  Brian Feb 12 at 22:36
    
Excellent. Update my answer to reflect what though? I stated in the answer that it only counts as one call. What would need to be updated? –  sfdc_ninja Feb 13 at 11:32
    
Thanks, just accepted as the answer. The only thing I meant to add was to show this on a number of records greater than the initial chunk size of query(), which is called as part of the for loop. That way I can see that calling queryMore() to get additional records doesn't bump the number of SOQL queries up to 2 or more. –  Brian Feb 14 at 20:27
    
Ahhh. I see. Just updated the answer to show that. –  sfdc_ninja Feb 14 at 20:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.