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
Elie WahnounElie Wahnoun 

Pardot/SFDC - Lead Distribution Issue - Lead Trigger

Hi all, 

I am experiencing issues with the “Lead Trigger” that has been developed for us. Our leads, coming in through Pardot, are being stuck in the que. I reached out to the SFDC supprt team and the told me the issue arises from the Lead Trigger. .

This is the error we are getting from Pardot: 

LeadTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.LeadTrigger: line 27, column 1 | CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY

Any direction as how to fix this would be truly helpful.
 
For your reference,  here is the code of the LeadTrigger:
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
trigger LeadTrigger on Lead (before insert, before update) {
   Set<String> codeSet = NEW Set<String>();
   Map<String,String> regionMap = new Map<String,String> ();
   if(Trigger.isInsert){
       for(Lead LD :Trigger.new){
           if(LD.Phone <> NULL){
               String numericPhone = LD.Phone.replaceAll('[^0-9]', '');
               String areaCode = numericPhone.substring(0,3);
               codeSet.add(areaCode);
           }
       }
   }
   if(Trigger.isUpdate){
       for(Lead LD :Trigger.new){
           if(LD.Phone != Null && LD.Phone <> Trigger.oldMap.get(LD.id).Phone){
               String numericPhone = LD.Phone.replaceAll('[^0-9]', '');
               String areaCode = numericPhone.substring(0,3);
               codeSet.add(areaCode);
           }
       }
   }
   IF(!codeSet.isEmpty()){
       for(Region_Lookup__c ch:[select Name,Area_code__C from Region_Lookup__c where Area_code__C IN:codeSet]){
           regionMap.put(ch.Area_code__C,ch.Name);
       }
       for(Lead LD :Trigger.new){
           String numericPhone = LD.Phone.replaceAll('[^0-9]', '');
           String areaCode = numericPhone.substring(0,3);
           if(regionMap.containsKey(areaCode)){
               LD.Lead_Region__c = regionMap.get(areaCode);
               
           }
       }  
       
   }
}
 

Thank you in advance for your time.
 
Best Answer chosen by Elie Wahnoun
James LoghryJames Loghry
You need to find and fix the error where the null value is being referenced. It looks like this may be the culprit, according to the line number(s):
 
String numericPhone = LD.Phone.replaceAll('[^0-9]', '');

Try replacing the for trigger with the following (I added a check for null and empty values around the Phone field):
 
trigger LeadTrigger on Lead (before insert, before update) {
   Set<String> codeSet = NEW Set<String>();
   Map<String,String> regionMap = new Map<String,String> ();
   if(Trigger.isInsert){
       for(Lead LD :Trigger.new){
           if(LD.Phone <> NULL){
               String numericPhone = LD.Phone.replaceAll('[^0-9]', '');
               String areaCode = numericPhone.substring(0,3);
               codeSet.add(areaCode);
           }
       }
   }
   if(Trigger.isUpdate){
       for(Lead LD :Trigger.new){
           if(LD.Phone != Null && LD.Phone <> Trigger.oldMap.get(LD.id).Phone){
               String numericPhone = LD.Phone.replaceAll('[^0-9]', '');
               String areaCode = numericPhone.substring(0,3);
               codeSet.add(areaCode);
           }
       }
   }
   IF(!codeSet.isEmpty()){
       for(Region_Lookup__c ch:[select Name,Area_code__C from Region_Lookup__c where Area_code__C IN:codeSet]){
           regionMap.put(ch.Area_code__C,ch.Name);
       }
       for(Lead LD :Trigger.new){
           if(!String.isEmpty(LD.Phone)){
               String numericPhone = LD.Phone.replaceAll('[^0-9]', '');
               String areaCode = numericPhone.substring(0,3);
               if(regionMap.containsKey(areaCode)){
                   LD.Lead_Region__c = regionMap.get(areaCode);    
               }
           }
       }  
       
   }
}

Also, when you post code on here, please use the code format button (< >).  It helps make the code a bit more readable and allows repliers to copy and paste if necessary.  Thanks

All Answers

James LoghryJames Loghry
You need to find and fix the error where the null value is being referenced. It looks like this may be the culprit, according to the line number(s):
 
String numericPhone = LD.Phone.replaceAll('[^0-9]', '');

Try replacing the for trigger with the following (I added a check for null and empty values around the Phone field):
 
trigger LeadTrigger on Lead (before insert, before update) {
   Set<String> codeSet = NEW Set<String>();
   Map<String,String> regionMap = new Map<String,String> ();
   if(Trigger.isInsert){
       for(Lead LD :Trigger.new){
           if(LD.Phone <> NULL){
               String numericPhone = LD.Phone.replaceAll('[^0-9]', '');
               String areaCode = numericPhone.substring(0,3);
               codeSet.add(areaCode);
           }
       }
   }
   if(Trigger.isUpdate){
       for(Lead LD :Trigger.new){
           if(LD.Phone != Null && LD.Phone <> Trigger.oldMap.get(LD.id).Phone){
               String numericPhone = LD.Phone.replaceAll('[^0-9]', '');
               String areaCode = numericPhone.substring(0,3);
               codeSet.add(areaCode);
           }
       }
   }
   IF(!codeSet.isEmpty()){
       for(Region_Lookup__c ch:[select Name,Area_code__C from Region_Lookup__c where Area_code__C IN:codeSet]){
           regionMap.put(ch.Area_code__C,ch.Name);
       }
       for(Lead LD :Trigger.new){
           if(!String.isEmpty(LD.Phone)){
               String numericPhone = LD.Phone.replaceAll('[^0-9]', '');
               String areaCode = numericPhone.substring(0,3);
               if(regionMap.containsKey(areaCode)){
                   LD.Lead_Region__c = regionMap.get(areaCode);    
               }
           }
       }  
       
   }
}

Also, when you post code on here, please use the code format button (< >).  It helps make the code a bit more readable and allows repliers to copy and paste if necessary.  Thanks
This was selected as the best answer
Abhijeet Anand 6Abhijeet Anand 6
Use the following check before operating on the Phone field:
 
String numericPhone = LD.Phone;

if(numericPhone != '' && numericPhone != null){

     
}

 
Elie WahnounElie Wahnoun
@James Loghry
Thank you so much for your answer. 

It think it will work. Although, not being a develloper, I am unable to actualyl edit it because the LeadTrigger is in production. 
Any help on that end? 

Browsed quickly online, and it seems this cannot be edited because it is an active org. 

Thoughts? 
Thanks.