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
J!GSJ!GS 

Help In System.NullPointerException: Attempt to de-reference a null object: Trigger

trigger CreateTaskWhenNewLeadGenerate on Lead (after insert) {
  Set<Id> LdIdSet = new Set<Id>();
 if(Trigger.isAfter && Trigger.isInsert) {            
        
   List<Lead> LdList = [ SELECT Id,Name,OwnerId,Company,Email, Phone,pi__comments__c,Leasing_agent__c,Create_Leasing_Call_Task__c,Lead_Property_Of_Interest__c FROM Lead WHERE Id IN : LdIdSet];  
   Map<Id,List<Lead>> LIdLdListMap = new Map<Id,List<Lead>>();
   List<Task> insTskList;
      
   Task newTsk ;
    
   
                   
        for ( BP_Property__c Prop:[Select Id,Name,IS_ACTIVE__c,Leasing_agent__c from BP_Property__c])
        {                                         
            
              //      LdList = LIdLdListMap.get(Prop) ;
                                                
      
              for ( Lead Ld : Trigger.New ){
                   LdIdSet.add ( Ld.Id );
                        
                if ( Trigger.NewMap.get ( Ld.Id ).Create_Leasing_Call_Task__c &&  ! Ld.Create_Leasing_Call_Task__c ){            
                //Ld.Create_Leasing_Call_Task__c  = true;
               LdList = LIdLdListMap.get( Ld.Id ) ;    
               
                if ( LdList == null || LdList.size() == 0 )
                    continue;                              
                  
                          if(Ld.Lead_Property_Of_Interest__c == null){
                              if(Ld.Leasing_Agent__c == null){
                                  
                              }
                                  
                              else{ 
                                       newTsk = new Task();
                                     newTsk.Subject = 'PC-Leasing Call';                                                               
                                     newTsk.OwnerId = Ld.Id;                                   
                                       newTsk.Leasing_Agent_ID__c = 'a0MG000000XkQFc';
                                     insTskList.add ( newTsk );
                                                                                                              
                              }
                }  
                              }else if(Ld.Lead_Property_Of_Interest__c != null && Ld.Leasing_Agent__c != null )
                              {        prop.IS_ACTIVE__c = true;
                                       prop.Leasing_Agent__r.IS_ACTIVE__c = true;
                              }
                                                                                             
                                  else{  
                                     newTsk = new Task();
                                        newTsk.Subject = 'LAInitial-Leasing call';                                                               
                                     newTsk.OwnerId = Ld.Owner_ID__c; 
                                     newTsk.WhoId =Ld.Id ; 
                                     NewTsk.Leasing_Agent__c = Ld.Leasing_Agent__c ;
                                        insTskList.add ( newTsk );
                              }
                              } 
                              
                         }
                                     
                                     insert newTsk;
     
              }            
        }
 
Best Answer chosen by J!GS
Marc D BehrMarc D Behr
What line is throwing the exception? Also, you really should not hard code a SF ID in the agent_ID__c in the task assignment. Look the user up outside the for loop.

Looking again, I think the error is comimg from this line
List<Lead> LdList = [ SELECT Id,Name,OwnerId,Company,Email, Phone,pi__comments__c,Leasing_agent__c,Create_Leasing_Call_Task__c,Lead_Property_Of_Interest__c FROM Lead WHERE Id IN : LdIdSet];
It looks the LdIdSet is just never set to a value, it is simply initialzed to LdIdSet = new Set<Id>();
 

All Answers

Marc D BehrMarc D Behr
What line is throwing the exception? Also, you really should not hard code a SF ID in the agent_ID__c in the task assignment. Look the user up outside the for loop.

Looking again, I think the error is comimg from this line
List<Lead> LdList = [ SELECT Id,Name,OwnerId,Company,Email, Phone,pi__comments__c,Leasing_agent__c,Create_Leasing_Call_Task__c,Lead_Property_Of_Interest__c FROM Lead WHERE Id IN : LdIdSet];
It looks the LdIdSet is just never set to a value, it is simply initialzed to LdIdSet = new Set<Id>();
 
This was selected as the best answer
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri

Hi, Please check this code I think you have to change code in 1st three lines

 

Set<Id> LdIdSet = new Set<Id>();

LdIdSet  = Trigger.newMap.keySet();

List<Lead> LdList = [ SELECT Id,Name,OwnerId,Company,Email, Phone,pi__comments__c,Leasing_agent__c,Create_Leasing_Call_Task__c,Lead_Property_Of_Interest__cFROM Lead WHERE Id IN : LdIdSet];