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
Dave The RaveDave 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
 
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();
    }

 
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi

Check the Null conditions and containsKey condition while trying to use value in Map :
 
else if(meeting.datemeeting__c.Year() == (mapReg.get(meeting.Registration_Layer__c).CertRegFrom__c.Year() + 1))  {
if( mapReg.containsKey(meeting.Registration_Layer__c) && meeting.Points__c!=null)
{
 if(mapReg.get(meeting.Registration_Layer__c).ObligatoryPointsObtainedYr2__c !=null))
                mapReg.get(meeting.Registration_Layer__c).ObligatoryPointsObtainedYr2__c += meeting.Points__c;
else
 mapReg.get(meeting.Registration_Layer__c).ObligatoryPointsObtainedYr2__c = meeting.Points__c;
else

            }
}

Please try this.
Thanks.
Naresh YadavNaresh Yadav
Hi Dave,

Whenever you perform calculations remember 2 things.
  1. Always check for null or empty values.
  2. 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.
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 != null && mapReg.containsKey(meeting.Registration_Layer__c) && mapReg.get(meeting.Registration_Layer__c).CertRegFrom__c != null){
			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();
}
Let me know if it helps you out. And mark it as best.


Thanks

Naresh Y.
Dave The RaveDave The Rave
Hi Naresh, thanks very much for your help.

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.
Naresh YadavNaresh Yadav
Hi 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 The RaveDave The Rave
Yes, I totally beleive I could not find any errors either. Is there a way that I can create a log, so that when you create a new record you can see what processes are happening in the background? I have check my process in the process builder and cannot find a fault. I tried also to delete a field, and got the error that the field was using an inactive process, maybe I need to totally delete the inactive process?

Dave