1

I'm not sure how to write this, but I know what the issue is. The following can be triggered by Insert or Update. Below, mapOfEnroll is Trigger.oldMap. I know the mapOfEnroll value is null on Insert and throwing the NullPointerException, I'm just not sure the right if statement to route it one way or another:

            FOR(Enrollment__c e :newEnroll){
                IF(mapOfEnroll.get(e.Id)!=null){
                    IF(enrlCode.containsKey(e.Lead__r.Enrollment_Code__c) && !enrlCode.containsKey(mapOfEnroll.get(e.Id).Lead__r.Enrollment_Code__c)){ //If Old Enrl Code wasn't PT but now is
                        enrlListPT.add(e);
                }
                    IF(!enrlCode.containsKey(e.Lead__r.Enrollment_Code__c) && enrlCode.containsKey(mapOfEnroll.get(e.Id).Lead__r.Enrollment_Code__c)){ //If Old Enrl Code was PT but now isn't
                        enrlListFT.add(e);
                    }
                } ELSE {
                    IF(enrlCode.containsKey(e.Lead__r.Enrollment_Code__c)){ //If Enrl Code is PT
                        enrlListPT.add(e);
                    }
                    IF(!enrlCode.containsKey(e.Lead__r.Enrollment_Code__c)){ //If Enrl Code Isn't PT
                        enrlListFT.add(e);
                    }
                }
            }

IF(mapOfEnroll.get(e.Id)!=null){

Throws a nullPointerException as well. I've tried removing it, but the next line references mapOfEnroll as well and throws nullPointerException on insert as well. I've also tried:

IF(Trigger.isUpdate){

but I found none of the code is triggered - not even the else statement. Any help on this? If update I want to see if the Enrollment_Code__c is changed from an value in the map to a value not in the map (and vise versa), and if insert I want to see if the value is in the map or not.

2 Answers 2

1

The mapOfEnroll variable should be null and the get() method throws this exception.

Replace this line :

 IF(mapOfEnroll.get(e.Id)!=null){

by :

 IF(mapOfEnroll != null && mapOfEnroll.get(e.Id)!=null){

Or ensure that mapOfEnroll is initialized before using it.

1
  • Should also just go ahead and replace mapOfEnroll.get(e.Id)!=null with mapOfEnroll.containsKey(e.Id).
    – Adrian Larson
    Commented Feb 5, 2015 at 17:32
0

Looks like I misspoke.

I debugged more and the Trigger.isUpdate is working as expected:

            FOR(Enrollment__c e :newEnroll){
                System.debug(loggingLevel.INFO,'## EnrollmentHelper_leadChange Trigger.isUpdate: '+Trigger.isUpdate+');
                IF(Trigger.isUpdate){
                    System.debug(loggingLevel.INFO,'## enrlCode.containsKey(e.Lead__r.Enrollment_Code__c): '+enrlCode.containsKey(e.Lead__r.Enrollment_Code__c)+'; enrlCode.containsKey(mapOfEnroll.get(e.Id).Lead__r.Enrollment_Code__c): '+enrlCode.containsKey(mapOfEnroll.get(e.Id).Lead__r.Enrollment_Code__c));

10:35:17:242 USER_DEBUG [12]|INFO|## EnrollmentHelper_leadChange Trigger.isUpdate: true 10:35:17:242 USER_DEBUG [14]|INFO|## enrlCode.containsKey(e.Lead__r.Enrollment_Code__c): false; enrlCode.containsKey(mapOfEnroll.get(e.Id).Lead__r.Enrollment_Code__c): false

The problem was in my nested formula - they are both returning false for some reason. i.e. Both the old record and new record do not reference a lead with an enrollment code in the enrlCode Map.

I'll need to continue debugging to figure out why that is, but Trigger.isUpdate will filter out any record inserted which will allow me to use the mapOfEnroll which was initialized with Trigger.oldMap.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .