Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Solution

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()]) {

        Trigger.oldMap.get(jobApp.Candidate__c).addError(
                   'Cannot delete candidate with a job application');

    }
}