• Data_Pump
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies

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.

 

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.