1

I'm fairly new to Apex so please bear with me. I'm getting some error messages regarding reaching SOQL limits for a particular class and method that does a callout and loops through the results. I'm wondering now what exactly counts as a SOQL query, particularly, does setting a lookup field to a new record with an external ID as argument count as one (see code example below)? Note that the actual insertion (upsert) of the records is not done within the loop.

For example

sfticket.Contact__r = new Contact(TM_ID__c = tmOrder.customerId);

I found this documentation about Flow bulkification that got me to think about this. It says there "When fields on a record are looked up, the transaction performs a SOQL query."

Appreciate any insight in this!

1
  • 2
    This will almost certainly be caused by some automation that runs on record insert; this could be record triggered flow or apex triggers or some other (older type of) automation.
    – Phil W
    Commented Sep 9 at 11:18

1 Answer 1

2

No, this type of record association should not result in an SOQL query.

I've executed this simple code snippet in the dev console to demonstrate:

Case cs = new Case();
cs.Subject = 'test external id';
cs.Description = 'sfsdgdfgfdh';
cs.Contact = new Contact(External_Id__c = '12345');
insert cs;

and in the logs we get this

17:38:39.645 (4645087977)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of Publish Immediate DML: 0 out of 150
  Number of DML rows: 1 out of 10000
  Maximum CPU time: 54 out of 10000

So no SOQL query was executed (this is with all triggers and flows turned off). If you're hitting limits, most likely other automations are to blame.

Flows and apex do not necessarily correspond in behaviour, so it's better to rely on documentation specifically for apex (or - in case it's lacking - to give it a try in the dev console).

You must log in to answer this question.

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