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
Data_PumpData_Pump 

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

I am trying to insert some data into SalesForce and I am getting this error when running it. It seems that the OwnerId is null if anyone with more expertise in apex programming could give a clue on how to solve this or print the value that is on Owner ID line 33, do a check or something.

 

Error Message:

 

UpdateUserQuota: execution of BeforeInsert

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

Trigger.UpdateUserQuota: line 33, column 1";s:10:"statusCode";s:36:"CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY";}s:2:"id";N;s:7:"success";b:0;}}'

 

 

 

Trigger.UpdateUserQuota :

 

 

trigger UpdateUserQuota on Opportunity (before update, before insert) {

    if(OpportunityTriggerController.testTrigger != null && OpportunityTriggerController.testTrigger != 'updateUserQuota')
        return;
        
    //if(OpportunityTriggerController.testTrigger == null)
        //return;
    QuotaManager quotaManager = new QuotaManager();
    
    
    List<Id> ownerIds = new List<Id>();
    for(Opportunity opp: trigger.new)
    {
        ownerIds.add(opp.OwnerId);
    }
    
    Map<Id, User> owners = new Map<Id, User>([SELECT Id, Name, Enable_Targets__c FROM User WHERE Id IN :ownerIds]);
    
    List<User_Quota__c> allExistingQuotas = [
            SELECT 
                Id,
                Year__c,
                Quarter__c,
                User__c
            FROM 
                User_Quota__c
    ];
    
    //List<Opportunity> oppsToUpdate = new List<Opportunity>();
    
    for(Opportunity opp: trigger.new)
    {
        if(!owners.get(opp.OwnerId).Enable_Targets__c)   /* <- Line 33 */ 
            continue;
            
        integer minMonth = 1;
        integer todayMonth = Date.today().month();
        if(todayMonth >= 1 && todayMonth <= 3)
            minMonth = 1;
        else if(todayMonth >= 4 && todayMonth <= 6)
            minMonth = 4;
        else if(todayMonth >= 7 && todayMonth <= 9)
            minMonth = 7;
        else if(todayMonth >= 10 && todayMonth <= 12)
            minMonth = 9;
        
        Date minDate = Date.newInstance(Date.today().year(), minMonth, 1);
        
        if(opp.CloseDate <= minDate && opp.User_Quota__c == null)
        {
            continue;
        }
                
        User_Quota__c quota = quotaManager.getQuota(opp.CloseDate, opp.OwnerId, opp.Owner.Name, allExistingQuotas);
        
        if(quota != null)
        { 
            if(opp.User_Quota__c == null || opp.User_Quota__c != quota.Id)
            {
                opp.User_Quota__c = quota.Id; 
                //oppsToUpdate.add(opp);
            }
        }
        else
        {
            opp.User_Quota__c = null;
        }
    }
    //update oppsToUpdate;
}

 

 

 

I am new to Apex and SalesForce so I am struggling to solve this, could be a stupid thing but I don't see to solve you guys from the community could help me I would be glad !  Thank you.

 

Best Answer chosen by Admin (Salesforce Developers) 
digamber.prasaddigamber.prasad

Hi,

 

My mistake, sorry about that.

 

I have modified code as below. Please use it.

if(owners.containsKey(opp.OwnerId)) 
	if(!owners.get(opp.OwnerId).Enable_Targets__c)

 

Let me know if you still see any problem.

 

Happy to help you!

 

 

All Answers

digamber.prasaddigamber.prasad

Hi,

 

Could you please replace your line#33 code with following 2 lines of code:-

 

if(owners.get(opp.OwnerId)) 
	if(!owners.get(opp.OwnerId).Enable_Targets__c)

 Let me know if you still see this problem.

 

Happy to help you!

 

Data_PumpData_Pump
I am getting an error saving the trigger when I changed the line#33 with these 2 line as:

Error: Compile Error: Condition expression must be of type Boolean at line 33 column 7

Thank you digamberlucky !
digamber.prasaddigamber.prasad

Hi,

 

My mistake, sorry about that.

 

I have modified code as below. Please use it.

if(owners.containsKey(opp.OwnerId)) 
	if(!owners.get(opp.OwnerId).Enable_Targets__c)

 

Let me know if you still see any problem.

 

Happy to help you!

 

 

This was selected as the best answer
Data_PumpData_Pump

Hi now I am getting another error:

 

UpdateUserQuota: execution of BeforeInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: 005D0000003QtlG: 

Class.QuotaManager.createQuota: line 65, column 1
Class.QuotaManager.getQuota: line 49, column 1
Trigger.UpdateUserQuota: line 55, column 1";s:10:"statusCode";s:36:"CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY";}s:2:"id";N;s:7:"success";b:0;}i:1;O:8:"stdClass":3:{s:6:"errors";O:8:"stdClass":2:{s:7:"message";s:387:"UpdateUserQuota: execution of BeforeInsert

 

trigger UpdateUserQuota on Opportunity (before update, before insert) {

    if(OpportunityTriggerController.testTrigger != null && OpportunityTriggerController.testTrigger != 'updateUserQuota')
        return;
        
    //if(OpportunityTriggerController.testTrigger == null)
        //return;
    QuotaManager quotaManager = new QuotaManager();
    
    
    List<Id> ownerIds = new List<Id>();
    for(Opportunity opp: trigger.new)
    {
        ownerIds.add(opp.OwnerId);
    }
    
    Map<Id, User> owners = new Map<Id, User>([SELECT Id, Name, Enable_Targets__c FROM User WHERE Id IN :ownerIds]);
    
    List<User_Quota__c> allExistingQuotas = [
            SELECT 
                Id,
                Year__c,
                Quarter__c,
                User__c
            FROM 
                User_Quota__c
    ];
    
    //List<Opportunity> oppsToUpdate = new List<Opportunity>();
    
    for(Opportunity opp: trigger.new)
    {
    if(owners.containsKey(opp.OwnerId)) 
    if(!owners.get(opp.OwnerId).Enable_Targets__c)
            continue;
            
        integer minMonth = 1;
        integer todayMonth = Date.today().month();
        if(todayMonth >= 1 && todayMonth <= 3)
            minMonth = 1;
        else if(todayMonth >= 4 && todayMonth <= 6)
            minMonth = 4;
        else if(todayMonth >= 7 && todayMonth <= 9)
            minMonth = 7;
        else if(todayMonth >= 10 && todayMonth <= 12)
            minMonth = 9;
        
        Date minDate = Date.newInstance(Date.today().year(), minMonth, 1);
        
        if(opp.CloseDate <= minDate && opp.User_Quota__c == null)
        {
            continue;
        }
                
        User_Quota__c quota = quotaManager.getQuota(opp.CloseDate, opp.OwnerId, opp.Owner.Name, allExistingQuotas);  // Line 55
        
        if(quota != null)
        { 
            if(opp.User_Quota__c == null || opp.User_Quota__c != quota.Id)
            {
                opp.User_Quota__c = quota.Id; 
                //oppsToUpdate.add(opp);
            }
        }
        else
        {
            opp.User_Quota__c = null;
        }
    }
    //update oppsToUpdate;
}

 

 

and 

 

public class QuotaManager {
    String used_currency;
    ID opportunity_id;
    
    public QuotaManager()
    {
    }
    
    public User_Quota__c getQuota(Date closeDate, string ownerId, string ownerName, List<User_Quota__c> allExistingQuotas)
    {
        integer month = closeDate.month();
        string year = string.valueOf(closeDate.year());
        
        
       
        
        string quarter = null;     
        if(month >= 1 && month <= 3)
        {
            quarter = 'Q1';
        }
        else if(month >= 4 && month <= 6)
        {
            quarter = 'Q2';
        }
        else if(month >= 7 && month <= 9)
        {
            quarter = 'Q3';
        }
        else if(month >= 10 && month <= 12)
        {
            quarter = 'Q4';
        }
        
        
        User_Quota__c quota = null;
        
        for(User_Quota__c existingQuota: allExistingQuotas)
        {
            if(existingQuota.Year__c == year && existingQuota.Quarter__c == quarter && existingQuota.User__c == ownerId)
            {
                quota = existingQuota;
                break;
            }
        }
        
        if(quota == null)
        {
            quota = this.createQuota(quarter, year, ownerId, ownerName);  // Line 49
            allExistingQuotas.add(quota);
        }

            
        return quota;
    }
    
    public User_Quota__c createQuota(string quarter, string year, string ownerId, string ownerName)
    {
        User_Quota__c quota = new User_Quota__c();
        quota.Year__c = year;
        quota.Quarter__c = quarter;
        quota.User__c = ownerId;
        quota.Target__c = 0;
        quota.Name = 'This text will be replaced';
        insert quota;           // Line 65
        
        return quota;
    }
    
}

 

 

 

If you know what is happening please give me a little help.

 

Thanks a lot !

digamber.prasaddigamber.prasad

Hi,

 

Looks like you can't assign this user to ownerId field, could you please hard code, say your UserId as owner of quote and see still gets same error.

Data_PumpData_Pump

 

 

The Owner Id was deleted I changed the owner id that was causing the error, so now it's inserting all opportunities.

 

Thank you very much for fixing the apex code and sorting out this for me, I really appreciate !!!