Does setting a lookup field trigger a SOQL query?

Question:
When working with Apex, a common question that arises is whether creating a new record with a lookup field will trigger a SOQL query. For example, if you create a new Contact
and assign it to a lookup field of a Case
record, does it result in a SOQL query being executed? Here’s an example scenario to illustrate the question:
Case cs = new Case();
cs.Subject = 'Test external ID';
cs.Description = 'Description of the case';
cs.Contact = new Contact(External_Id__c = '12345');
insert cs;
In this example, the Case
record is being associated with a Contact
record, which uses an external ID. The question is: does this operation cause a SOQL query to be executed to look up the Contact
record, or is it just a reference assignment?
Answer:
No, this type of record association does not result in a SOQL query. When you create a new record and assign a value to a lookup field, such as in the code example, it does not automatically trigger a SOQL query. The assignment is simply linking the Contact
object to the Case
object via the lookup field.
Master Salesforce with expert-led training at CRS Info Solutions in Salesforce training in Hyderabad. Gain hands-on experience in Admin, Developer (Apex), and Integration—join a free demo today!!!
I tested this scenario by running the code in the Developer Console. The result shows that no SOQL query is executed when associating the Contact
record in this way:
Case cs = new Case();
cs.Subject = 'Test external ID';
cs.Description = 'Description of the case';
cs.Contact = new Contact(External_Id__c = '12345');
insert cs;
Explanation: In this code, a new Case
record is created with a subject and description. A Contact
record is then instantiated with an external ID and assigned to the Contact
lookup field on the Case
. Finally, the Case
record is inserted into the database, but no SOQL query is triggered during the assignment of the Contact
record.
The log confirms that no SOQL query was executed:
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
Explanation: The log above shows the usage limits for a transaction in Salesforce. It indicates that no SOQL or SOSL queries were executed (0 out of 100 and 0 out of 20, respectively), with only one DML statement (insert) used, affecting one row. The CPU time usage is minimal (54 out of 10,000), meaning the operation was efficient in terms of resource consumption.
Conclusion:
Creating a new record and setting a lookup field does not perform a SOQL query by default. If you’re experiencing SOQL limit issues, it’s important to review other areas of your code or automation that might be generating queries, as this type of assignment is not responsible for executing any queries.
Launch Your Salesforce Career in Hyderabad’s Booming Tech Hub
Hyderabad has emerged as a major hub for IT and cloud computing, with Salesforce leading the charge in digital transformation. As companies increasingly rely on Salesforce for CRM, AI, automation, and integrations, the demand for certified professionals is soaring. Top firms like Deloitte, Accenture, Infosys, TCS, and Wipro are actively seeking skilled Salesforce experts, making specialized training a powerful catalyst for career growth.
If you’re ready to build a thriving Salesforce career, the right training can make all the difference. CRS Info Solutions provides industry-focused Top Salesforce training Institutes in Hyderabad, covering Admin, Developer (Apex), and Integration modules. With expert guidance, hands-on projects, and real-world case studies, this program equips you with the skills to excel in Hyderabad’s competitive job market. Take the next step—enroll today and open the door to limitless opportunities in the Salesforce ecosystem!!!