You need to sign in to do that
Don't have an account?
Dave The Rave
apex trigger issue
Hi all,
The code below involve 2 objects Registration_Layer__c & Meeting__c. It is a master-detail relationship. Each contact has a upto 3 records on the Registration_Layer__c, when you create a record on Meeting__c and populate fields datemeeting__c and points__c, the fields obligatorypointsobtainedYR1,YR2 etc should be populate with the value of points__c according to the year in datemeeting__c.
However, I get an error when trying to save a new record on the meeting__c object.
"Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger TrgOnMeeting caused an unexpected exception, contact your administrator: TrgOnMeeting: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.TrgOnMeeting: line 20, column 1"
What did I do wrong?
Thanks, Dave
The code below involve 2 objects Registration_Layer__c & Meeting__c. It is a master-detail relationship. Each contact has a upto 3 records on the Registration_Layer__c, when you create a record on Meeting__c and populate fields datemeeting__c and points__c, the fields obligatorypointsobtainedYR1,YR2 etc should be populate with the value of points__c according to the year in datemeeting__c.
However, I get an error when trying to save a new record on the meeting__c object.
"Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger TrgOnMeeting caused an unexpected exception, contact your administrator: TrgOnMeeting: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.TrgOnMeeting: line 20, column 1"
What did I do wrong?
Thanks, Dave
Trigger TrgOnMeeting on Meeting__c (After insert, After Update){ Set<Id> regIds = new Set<Id>(); if(Trigger.isInsert || Trigger.isUpdate) { for(Meeting__c meeting : Trigger.new) { if(meeting.Registration_Layer__c != null) regIds.add(meeting.Registration_Layer__c); } } Map<Id, Registration_Layer__c> mapReg = new Map<Id, Registration_Layer__c>([SELECT Id, CertRegFrom__c, ObligatoryPointsObtainedYr1__c, ObligatoryPointsObtainedYr2__c, ObligatoryPointsObtainedYr3__c, ObligatoryPointsObtainedYr4__c, ObligatoryPointsObtainedYr5__c, ObligatoryPointsObtainedYr6__c FROM Registration_Layer__c WHERE Id IN: regIds]); for(Meeting__c meeting : Trigger.new) { if(meeting.datemeeting__c.Year() == (mapReg.get(meeting.Registration_Layer__c).CertRegFrom__c.Year())) { mapReg.get(meeting.Registration_Layer__c).ObligatoryPointsObtainedYr1__c += meeting.Points__c; } else if(meeting.datemeeting__c.Year() == (mapReg.get(meeting.Registration_Layer__c).CertRegFrom__c.Year() + 1)) { mapReg.get(meeting.Registration_Layer__c).ObligatoryPointsObtainedYr2__c += meeting.Points__c; } else if(meeting.datemeeting__c.Year() == (mapReg.get(meeting.Registration_Layer__c).CertRegFrom__c.Year() + 2)) { mapReg.get(meeting.Registration_Layer__c).ObligatoryPointsObtainedYr3__c += meeting.Points__c; } else if(meeting.datemeeting__c.Year() == (mapReg.get(meeting.Registration_Layer__c).CertRegFrom__c.Year() + 3)) { mapReg.get(meeting.Registration_Layer__c).ObligatoryPointsObtainedYr4__c += meeting.Points__c; } else if(meeting.datemeeting__c.Year() == (mapReg.get(meeting.Registration_Layer__c).CertRegFrom__c.Year() + 4)) { mapReg.get(meeting.Registration_Layer__c).ObligatoryPointsObtainedYr5__c += meeting.Points__c; } else if(meeting.datemeeting__c.Year() == (mapReg.get(meeting.Registration_Layer__c).CertRegFrom__c.Year() + 5)) { mapReg.get(meeting.Registration_Layer__c).ObligatoryPointsObtainedYr6__c += meeting.Points__c; } } update mapReg.values(); }
Check the Null conditions and containsKey condition while trying to use value in Map :
Please try this.
Thanks.
Whenever you perform calculations remember 2 things.
- Always check for null or empty values.
- If you are using a map then first check if it is containing the key or not using the containsKey method.
I have updated the code, Please check it. Let me know if it helps you out. And mark it as best.Thanks
Naresh Y.
The code is putting the values in correct place. However, there is an error, the value of points__c on the meeting__c object always doubles. If I create a record on the meeting__c object where points__c = 3 AND (YEAR(datemeeting)__c = (YEAR(CertRegFrom__c THEN ObligatoryPointsObtainedYr1__c (should) = 3 . But is = 6 instead. ObligatoryPointsObtainedYr1__c = 0 before I create a record.
Then error also occurs if the YEAR is 2019, ObligatoryPointsObtainedYr2__c also doubles from 3 to 6.
Why is the code doubling the value of points__c when you only create 1 record on meeting__c?
Dave.
I have checked the code and found that there is no code statement which doubles the value.
Please check if you have any trigger on Registration_Layer__c or any field update.
Let me know.
Dave