You need to sign in to do that
Don't have an account?
manjunath vivek
What is the function of <strong> in the below code? and how does it work?
01 trigger populateFieldOnCase on Case (before insert){
02 Set<id>parentIds = new Set<id>();
03 for(Case acase:trigger.new){
04 parentIds.add(acase.project__c);
05 }
06 Map<id,parent__c> pro = new Map<id,parent__c>([select id,parentField__c from parent__c where idin :parentIds]);
07 for(case acase:trigger.new){
08 <strong>if(pro.get(acase.id) != null){</strong>
09 acase.childfield__c = pro.get(acase.id).parentField__c;
10 <strong>}</strong>
11 }
12 }
02 Set<id>parentIds = new Set<id>();
03 for(Case acase:trigger.new){
04 parentIds.add(acase.project__c);
05 }
06 Map<id,parent__c> pro = new Map<id,parent__c>([select id,parentField__c from parent__c where idin :parentIds]);
07 for(case acase:trigger.new){
08 <strong>if(pro.get(acase.id) != null){</strong>
09 acase.childfield__c = pro.get(acase.id).parentField__c;
10 <strong>}</strong>
11 }
12 }
All Answers
Line 8 checks to see if the Id exists in the map. If we did not do this and we tried to pull the data from the map and call parentField__c on it we would get a Null Pointer Exception (NPE).
I think your code should actually read:
In your code you were checking the map pro for acase.Id which is not what the map is of. The map is of case.Project__c to parent__c
Just a suggestion, but I'd use the containsKey method rather than checking if it != null. Does the same thing but one looks a bit cleaner than the other. Again, just my personal preference as an OCD developer :-)