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
PlatFormCloudPlatFormCloud 

execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object:

Hi, 
I saved the trigger sucessully, however, when I am saving  the BLC_Lead__c record from UI, then I am getting the error in the bold font written line and the error is  Salesforce - Unlimited EditionSystem.NullPointerException: Attempt to de-reference a null object: Trigger.EnableCurfewedOnLeads: line 23, column 1 :


trigger EnableCurfewedOnLeads on BLC_Lead__c (before update) {

//Iterate over all the Leads to retrieve the Curfew ID
   Set<Id> setCurfewIds = new Set<Id>();
     for(BLC_Lead__c blcLead : Trigger.new){
     
       setCurfewIds.add(blcLead.Call_Curfew_Details__r.id);      
       
        }
        

//  Retrieve all Curfews and create a map of Curfews

  Map<Id,Call_Curfew_Details__c>  mapCurfews = new Map<Id,Call_Curfew_Details__c>([SELECT Id, IsCurfew__c FROM Call_Curfew_Details__c where Id IN:setCurfewIds]);
  
    
    
//Iterate all over the Leads and Populate the flag isCurfew = TRUE
    for(BLC_Lead__c leds: Trigger.new){
    
if(leds != null && leds != null){        
         Call_Curfew_Details__c relatedCurfews =  mapCurfews.get(leds.Call_Curfew_Details__r.id);          leds.IsCurfew__c =  relatedCurfews.IsCurfew__c;        
          }        
       }    

    }
Ray C. KaoRay C. Kao
Hi PlatFormCloud

  Try the code as below:
trigger EnableCurfewedOnLeads on BLC_Lead__c (before update) {

//Iterate over all the Leads to retrieve the Curfew ID
   Set<Id> setCurfewIds = new Set<Id>();
     for(BLC_Lead__c blcLead : Trigger.new){
     
       setCurfewIds.add(blcLead.Call_Curfew_Details__r.id);      
       
        }
        

//  Retrieve all Curfews and create a map of Curfews

  Map<Id,Call_Curfew_Details__c>  mapCurfews = new Map<Id,Call_Curfew_Details__c>([SELECT Id, IsCurfew__c FROM Call_Curfew_Details__c where Id IN:setCurfewIds]);
  
    
    
//Iterate all over the Leads and Populate the flag isCurfew = TRUE
    for(BLC_Lead__c leds: Trigger.new){
         Call_Curfew_Details__c relatedCurfews =  mapCurfews.get(leds.Call_Curfew_Details__r.id);
         if(relatedCurfews != null ) leds.IsCurfew__c =  relatedCurfews.IsCurfew__c;
         else leds.addError('No related Curf fews);
    }    

}

 
Dilip_VDilip_V

 

Hi,
 
Its better to use containskey before geeting data from map.
try this code 
 
trigger EnableCurfewedOnLeads on BLC_Lead__c (before update) {

//Iterate over all the Leads to retrieve the Curfew ID
   Set<Id> setCurfewIds = new Set<Id>();
     for(BLC_Lead__c blcLead : Trigger.new){
     
       setCurfewIds.add(blcLead.Call_Curfew_Details__r.id);      
       
        }
        

//  Retrieve all Curfews and create a map of Curfews

  Map<Id,Call_Curfew_Details__c>  mapCurfews = new Map<Id,Call_Curfew_Details__c>([SELECT Id, IsCurfew__c FROM Call_Curfew_Details__c where Id IN:setCurfewIds]);
  
    
    
//Iterate all over the Leads and Populate the flag isCurfew = TRUE
    for(BLC_Lead__c leds: Trigger.new){
    
if(leds != null && mapCurfews.containskey(leds.Call_Curfew_Details__r.id)){        
         Call_Curfew_Details__c relatedCurfews =  mapCurfews.get(leds.Call_Curfew_Details__r.id);          leds.IsCurfew__c =  relatedCurfews.IsCurfew__c;        
          }        
       }    

    }

Thanks.