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
hybin joseph 2hybin joseph 2 

Getting the Following Error : System.NullPointerException: Attempt to de-reference a null object Trigger.CreateActivityOnLeadConverted: line 34, column 1

I am getting the following error :
Error: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateActivityOnLeadConverted: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.CreateActivityOnLeadConverted: line 34, column 1: [] Class.leadconvert.BulkLeadConvert.handleOpportunityInserts: line 740, column 1 Class.leadconvert.BulkLeadConvert.convertLead: line 104, column 1

Below is the trigger I think is the problem but not sure Please help.
 
trigger CreateActivityOnLeadConverted on Opportunity (after insert) {
    
    List<Task> tasksToInsert = new List<Task>();
    List<User> usersToUpdate = new List<User>();
    List<Lead_Quota_Audit__c> lstNewAudits = new List<Lead_Quota_Audit__c>();
    
    Id wholsaleID = null;
    List <RecordType> wId = [Select Id From RecordType where sobjecttype = 'Opportunity' and name = 'Wholesale' limit 1];
    if ((wId != null) && (wId.size() == 1)) wholsaleID = wId[0].Id;
    
    Set<Id> userIds = new Set<Id>();
    for(Opportunity opp : Trigger.new){
        userIds.add(opp.OwnerId);
    }
    
    Map<Id, User> usersById = new Map<Id, User>([SELECT Id, Name, Leads_Assigned_Current_Month__c, Leads_Assigned_Current_Week__c FROM User  WHERE Id IN :userIds]);
    
    for(Opportunity opp : Trigger.new){
        if (opp.RecordTypeId != wholsaleID) {    
            if (opp.ConvertedLeadID__c != null) {
                Task task = new Task(
                    Type = 'Call',
                    Subject = 'Lead Status Changed to Converted',
                    ActivityDate = Date.Today(),
                    WhatId = opp.Id,
                    OwnerId = UserInfo.getUserId(),
                    Status = 'Completed', 
                    Call_Screener_Transfered__c = opp.CS_Transferred__c 
                );
                tasksToInsert.add(task);
                if(opp.Lead_Status__c == 'Open'){
                    User u = usersById.get(opp.OwnerId);
                    if(u != null){
                        u.Leads_Assigned_Current_Month__c++;
                        u.Leads_Assigned_Current_Week__c++;
                        usersToUpdate.add(u);
                        Lead_Quota_Audit__c audit = new Lead_Quota_Audit__c(
                            Lead__c = opp.ConvertedLeadID__c,
                            User__c = u.Id,
                            Old_Owner__c = u.Name
                        );
                        lstNewAudits.add(audit);
                        
                    }
                }
            }
        }
    }
    
    insert tasksToInsert;
    update usersToUpdate;
    if(lstNewAudits.size() > 0) {
        insert lstNewAudits;
    }

}

 
Best Answer chosen by hybin joseph 2
MythreyeeSSMythreyeeSS
Hi -

The user record should be having null value for the field "Leads_Assigned_Current_Month__c". In Line number 34, when we try to increment the null value, "Attempt to de-reference a null object " error is received. To fix this, initialize the variable if it is null
if(u.Leads_Assigned_Current_Month__c == null) { // If it is a text variable, use string.isBlank(u.Leads_Assigned_Current_Month__c)
u.Leads_Assigned_Current_Month__c == 1;
}
else {
u.Leads_Assigned_Current_Month__c++;
}

All Answers

MythreyeeSSMythreyeeSS
Hi -

The user record should be having null value for the field "Leads_Assigned_Current_Month__c". In Line number 34, when we try to increment the null value, "Attempt to de-reference a null object " error is received. To fix this, initialize the variable if it is null
if(u.Leads_Assigned_Current_Month__c == null) { // If it is a text variable, use string.isBlank(u.Leads_Assigned_Current_Month__c)
u.Leads_Assigned_Current_Month__c == 1;
}
else {
u.Leads_Assigned_Current_Month__c++;
}
This was selected as the best answer
hybin joseph 2hybin joseph 2
Thanks for replying and explaining will try that out and let u know how that went 
hybin joseph 2hybin joseph 2
Thanks @MythreyeeSS That worked just fine Thanyou again for ur help Much Appreciated!!!!