Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (5) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Comparing Queries Against Trigger.old and

Comparing Queries Against Trigger.old and

Post by Developer Force  (2010-07-16)

Status: Certified
Level: novice


You're writing a before update or before delete trigger and need to issue a SOQL query to get related data for records in the and Trigger.old lists.


Correlate records and query results with the Trigger.newMap and Trigger.oldMap ID-to-SObject maps.

For example, the following trigger uses Trigger.oldMap to create a set of unique IDs (Trigger.oldMap.keySet()). The set is then used as part of a query to create a list of job applications associated with the candidates being processed by the trigger. For every job application returned by the query, the related candidate is retrieved from Trigger.oldMap and prevented from being deleted.

trigger candidateTrigger on Candidate__c (before delete) {
    for (Job_Application__c jobApp : [SELECT Candidate__c
                                      FROM Job_Application__c
                                      WHERE Candidate__c
                                      IN :Trigger.oldMap.keySet()]) {
                   'Cannot delete candidate with a job application');


It's a better practice to use Trigger.newMap and Trigger.oldMap because you can't assume that directly querying the and Trigger.old lists will return the same number of records in the same order. Even though these lists are sorted by ID, external operations might change the number of records that are returned and make parallel list processing dangerous.


Recipe Activity - Please Log in to write a comment

Great work

by Jaipalreddy Admin  (2012-04-09)

Interesting. What conditions would cause the and trigger.old lists to have different sizes for an update trigger? 

If code should be using trigger.oldMap to find a particular record instead of assuming it's in the same location in trigger.old, it will also need to handle the condition where it's not found in trigger.oldMap. For update triggers that may be tricky!

by Anil Rhemtulla  (2011-10-13)

Works great!!

voted as verified by a0930000008RAGB  (2011-03-14)


Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.