2

I have a fundamental question about the how number of SOQL queries calulated in Salesforce.

I have inserted 50 Sample Accounts (Sample0, Sample2 ...Sample49) and trying to perform SOQL query:

List<Account> accs = [SELECT Id FROM Account WHERE Name LIKE 'Sam%'];

logs show the below limits:

Number of SOQL queries: 1 out of 100 Number of query rows: 50 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 0 out of 150 Number of DML rows: 0 out of 10000

Now, when i try to query and delete accounts:

List<Account> accs = [SELECT Id FROM Account WHERE Name LIKE 'Sam%']; DELETE accs;

The logs show:

Number of SOQL queries: 2 out of 100 Number of query rows: 50 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 1 out of 150 Number of DML rows: 50 out of 10000

UPDATE: I am executing the code in Developer console with Execute Anonymous Window. Below is the code:

/*list<Account> iacc = new List<Account>(); for(integer i=0; i<50; i++) { iacc.add(new Account(Name = 'Sample' + i)); } insert iacc; */

List<Account> accs = [SELECT Id FROM Account WHERE Name LIKE 'Sam%']; Delete accs;

I would uncomment 'for loop' and insert statement for re-inserting the accounts later (i would comment SOQL and Delete statment that time)

I was expecting Number of SOQL queries to be still 1, but the limits show it as 2. I guess i am missing something fundamental about the behaviour of SOQL or DELETE. Can you please help me in understanding this?

Thanks in Advance

  • What code did you use to do this? – Dan Jones Nov 24 '16 at 11:31
  • Are you doing this in one transaction (in which case 2 queries is expected) or two transactions? Please share the code you used. – Girbot Nov 24 '16 at 11:37
  • Dan, Girbot - I have updated the code in the question. – Arun Nov 24 '16 at 11:54
  • Do you have a trigger on Account that runs either before delete or after delete? – Derek F Nov 24 '16 at 14:17
  • @DerekF, yes i had a before delete trigger on Account and additional SOQL query was being counted from that. Thanks. – Arun Nov 26 '16 at 7:10
2

I did copy-paste your code snippet on my home dev org and it showed only 1 SOQL for me. I would suggest you to check for:

  • Triggers for this object
  • Processes in process builder for this object (since process builder could run apex)
  • Workflows since they could cause trigger invokations.
  • not entirely sure but...flows?

I would suggest you to create a new dev org and try your code snippet there. It should show you only 1 query.

Edited: the author said that the additional SOQL query was because of before delete trigger(written in comments).

  • 1
    also rollup summaries – kurunve Nov 24 '16 at 14:07
  • @kurunve do roll-up summaries count against query limits? O_o – EvAzi Nov 24 '16 at 14:34
  • no, but they may trigger one more update, which can eat query limit) – kurunve Nov 24 '16 at 14:43
  • 1
  • 1
    Thanks @EvAzi, i tried in new dev org. it showed me only one SOQL query. additional query was showing up because of a before delete trigger i had on Account. Thanks for pointing out. – Arun Nov 26 '16 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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