function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jim LamberthJim Lamberth 

Why does this code work?

This code (not written by me) references the Id of a record in a before insert trigger. I was always under the impression you couldn't do that, as there is no Id before the record is committed to the database.

But it works... the Campaign__c field is correctly set every time. So naturally my question is how does it work? Because of the Id reference, or in spite of it? Is this some well known feature of Apex that I should be aware of, or a flawed trigger that happens to do the right thing?

trigger RegionTargetSetCampaign on Region_Targets__c (before insert) {

    Map<Id,Id> regionTargetToCallingGuide = new Map<Id,Id>();
    
    for(integer i=0; i<Trigger.size; i++) {
        Region_Targets__c regionTarget = Trigger.new[i];
        regionTargetToCallingGuide.put(regionTarget.id, regionTarget.Campaign_Delivery__c);
    }
    
    Map<Id,Campaign_Delivery__c> callingGuideMap = new Map<Id,Campaign_Delivery__c>([SELECT id, Campaign__c FROM Campaign_Delivery__c WHERE id IN :regionTargetToCallingGuide.values()]);
    
    for(Region_Targets__c regionTarget : Trigger.new) {
        regionTarget.Campaign__c = callingGuideMap.get(regionTargetToCallingGuide.get(regionTarget.id)).Campaign__c;
    }
}
Best Answer chosen by Jim Lamberth
Abhishek BansalAbhishek Bansal
Hi Jim,

This trigger is working on the basis of null vallues stored in the id field. This will work for only one record and if there are multiple records inserted at the same time then it will fail and set the same value in Campaign__c field of all the records. 
I have added the comments in the code to make you understand how it is working:
trigger RegionTargetSetCampaign on Region_Targets__c (before insert) {

    Map<Id,Id> regionTargetToCallingGuide = new Map<Id,Id>();
    
    for(integer i=0; i<Trigger.size; i++) {
        Region_Targets__c regionTarget = Trigger.new[i];
        regionTargetToCallingGuide.put(regionTarget.id, regionTarget.Campaign_Delivery__c);
    }
    
    Map<Id,Campaign_Delivery__c> callingGuideMap = new Map<Id,Campaign_Delivery__c>([SELECT id, Campaign__c FROM Campaign_Delivery__c WHERE id IN :regionTargetToCallingGuide.values()]);
    
    for(Region_Targets__c regionTarget : Trigger.new) {
        regionTarget.Campaign__c = callingGuideMap.get(regionTargetToCallingGuide.get(regionTarget.id)).Campaign__c;
    }
}

//Suppose trigger is working for one record
//regionTargetToCallingGuide at line 7 will hold values as [null; a60nskjhkjsh(campaign Id)]
//At line number 10 the query will fetch records matching the Id a60nskjhkjsh stored in the map
//At Line number 13 the Campaign is set with the value fetched from query as follows:
//callingGuideMap.get(regionTargetToCallingGuide.get(regionTarget.id)).Campaign__c where regionTargetToCallingGuide.get(regionTarget.id) = null then callingGuideMap.get(regionTargetToCallingGuide.get(null)) = a60nskjhkjsh so at last callingGuideMap.get(a60nskjhkjsh) = record fetched from query.


//If trigger is working for more than one record
//regionTargetToCallingGuide at line 7 will always get replaced with the last value 
//After first record map will be [Null, record1CampaignId]
//Afetr second record map will be [Null, record2CampaignId]
//After last record map will be [Null, lastrecordCampaigId]
//At line number 10 the query will fetch records matching the Id of campaign field for last record
//At line number 13 all the records will be set with the same campaign id as they all contain null in the id field.
May be I am not good in exaplaining the things here, but the trigger is workig on the null values only and in case of bulk records it will definitely fail. Let me know if you need any further information on this.

Thanks,
Abhishek Bansal.

All Answers

Abhishek BansalAbhishek Bansal
Hi Jim,

This trigger is working on the basis of null vallues stored in the id field. This will work for only one record and if there are multiple records inserted at the same time then it will fail and set the same value in Campaign__c field of all the records. 
I have added the comments in the code to make you understand how it is working:
trigger RegionTargetSetCampaign on Region_Targets__c (before insert) {

    Map<Id,Id> regionTargetToCallingGuide = new Map<Id,Id>();
    
    for(integer i=0; i<Trigger.size; i++) {
        Region_Targets__c regionTarget = Trigger.new[i];
        regionTargetToCallingGuide.put(regionTarget.id, regionTarget.Campaign_Delivery__c);
    }
    
    Map<Id,Campaign_Delivery__c> callingGuideMap = new Map<Id,Campaign_Delivery__c>([SELECT id, Campaign__c FROM Campaign_Delivery__c WHERE id IN :regionTargetToCallingGuide.values()]);
    
    for(Region_Targets__c regionTarget : Trigger.new) {
        regionTarget.Campaign__c = callingGuideMap.get(regionTargetToCallingGuide.get(regionTarget.id)).Campaign__c;
    }
}

//Suppose trigger is working for one record
//regionTargetToCallingGuide at line 7 will hold values as [null; a60nskjhkjsh(campaign Id)]
//At line number 10 the query will fetch records matching the Id a60nskjhkjsh stored in the map
//At Line number 13 the Campaign is set with the value fetched from query as follows:
//callingGuideMap.get(regionTargetToCallingGuide.get(regionTarget.id)).Campaign__c where regionTargetToCallingGuide.get(regionTarget.id) = null then callingGuideMap.get(regionTargetToCallingGuide.get(null)) = a60nskjhkjsh so at last callingGuideMap.get(a60nskjhkjsh) = record fetched from query.


//If trigger is working for more than one record
//regionTargetToCallingGuide at line 7 will always get replaced with the last value 
//After first record map will be [Null, record1CampaignId]
//Afetr second record map will be [Null, record2CampaignId]
//After last record map will be [Null, lastrecordCampaigId]
//At line number 10 the query will fetch records matching the Id of campaign field for last record
//At line number 13 all the records will be set with the same campaign id as they all contain null in the id field.
May be I am not good in exaplaining the things here, but the trigger is workig on the null values only and in case of bulk records it will definitely fail. Let me know if you need any further information on this.

Thanks,
Abhishek Bansal.
This was selected as the best answer
Jim LamberthJim Lamberth
Hi Abhishek,

Yes that clears things up perfectly, and explains why it's screwing with my test class. Attempting to insert multiple records confirms it does break. I suppose I should try and clean it up...

Thanks for your response.