You need to sign in to do that
Don't have an account?
Jim 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;
}
}
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;
}
}
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: 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
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: 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.
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.