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
rvkrvk 

Having issues with the trigger

hi all,

 

I have developed a before update trigger which has to fire when the lead status changes to reject then it should change the user to the manager of that user it is working perfectly in UI testing but getting errors when  testing with data loader.the error i am getting is "data changed by trigger for field Owner ID: owner cannot be blank". can any one help me to fix this error.

 

 

timainmantimainman

Looks to me like you might have nulled out the owner id somewhere in your trigger. If you post some of your code we could debug it for sure.

rvkrvk

below is my code.

 

trigger Leaduser on Lead (before update) {       
   
    List<string> IdList = new List<string>();
    map<id,user> Temp = new Map<id,user>();     
     for(lead ld : trigger.old){                       
        system.debug(ld.ownerId);                 
        if(ld.vendor__c == 'East')                                  
          IdList.add(ld.ownerId);   
     }
    if(OwnerIdList.size()>0)  { 
        Map<Id,user> manager = new Map<Id,user>([Select u.Id,u.ManagerId,u.Name From User u where u.id IN : IdList]);   
        Temp= manager;
    }
     for(lead ld : trigger.New){           
        
         user userId = Temp.get(ld.ownerId);             
         if((Temp.get(ld.ownerId)!= null) && (ld.status == 'Reject') ){
               system.debug('manager Id is :'+ Temp.get(ld.ownerId));                
               ld.ownerId = userId.ManagerId;
           }                                  
              
      }   
  }

timainmantimainman

Are you sure the ManagerId field is filled in on all your records? If that is null the the trigger would fail. 

rvkrvk

even though manager id field is filled i am getting that error message.some records dont have one but most of them have the manager id.can u please tell me why am i getting  error when the  manager id field is empty.

timainmantimainman

Well in this line of your code:

 

 ld.ownerId = userId.ManagerId;

 

You are setting the lead ownerId to whatever is in the user's managerId field. If that is null it results in you setting the ownerId to null...which cannot happen. Every record needs an owner.

rvkrvk

I am getting error message " System.NullPointerException: Attempt to de-reference a null object: Trigger.Leaduser : line 17, column 22". when i update the record in lead.

 

trigger Leaduser on Lead (before update) {       
   
    List<string> IdList = new List<string>();
    map<id,user> Temp = new Map<id,user>();     
     for(lead ld : trigger.old){                       
        system.debug(ld.ownerId);                 
        if(ld.vendor__c == 'East')                                  
          IdList.add(ld.ownerId);   
     }
    if(OwnerIdList.size()>0)  { 
        Map<Id,user> manager = new Map<Id,user>([Select u.Id,u.ManagerId  From User u where u.id IN : IdList]);   
        Temp= manager;
    }
     for(lead ld : trigger.New){           
        
         user userId = Temp.get(ld.ownerId);             
         if((Temp.get(ld.ownerId)!= null) && (ld.status == 'Reject') ){
               system.debug('manager Id is :'+ Temp.get(ld.ownerId));                
               ld.ownerId = userId.ManagerId;
           }                                  
              
      }   
  }

kbromerkbromer

Same issue, if ManagerID is null in this line:

ld.ownerId = userId.ManagerId;

 

You're attempting to use a null variable. You can check it at run time with a simple:

if (userID.ManagerID != null)

ld.ownerId = userId.ManagerId;