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
RossGRossG 

trigger error:

The trigger below calls a class. The purpose of the whole thing is to create a new custom object record when an opp is created or edited to meet a few criteria.

 

The thing works in the ui on single records, and I also ran bulk inserts and updates and it all works.

 

Problem is I'm going to deploy to prod, and I'm getting an error telling me that it's "attempting to de-reference a null object".

 

Does anyone see how or where this error is in the trigger or class, and how it could be resolved?  I have spent like all day trying to get past this error and no matter what I do I keep getting it.

 

 

Trigger:

 

 

trigger OpportunityTrigger on Opportunity(after insert,after update){

if (Trigger.isAfter) { /* BEGIN Services Engagement Auto-create Processing for Opps */ ServicesEngagementManager newSEOnOppInsert = new ServicesEngagementManager(); if(Trigger.isInsert){ newSEOnOppInsert.createEngagementWhenNeededOnOppInsert(Trigger.new); } ServicesEngagementManager newSEOnOppUpdate = new ServicesEngagementManager(); if(Trigger.isUpdate){ newSEOnOppUpdate.createEngagementWhenNeededOnOppUpdate(Trigger.new, Trigger.oldMap); } /* END Services Engagement Auto-create Processing */

...

}

 

 

Class:

 

public class ServicesEngagementManager{

    public void createEngagementWhenNeededOnOppInsert(List<Opportunity> opportunities)
    {

        List<Opportunity> opps = [SELECT Id,Services_to_be_Delivered_by__c,Owner.Id,Probability,Threshold__c,Account.Id,Account.Name,Region__c,
                                                       CreatedById,Account.Region__r.OwnerId,Account.Region__r.RSA_Manager__r.Id,Professional_Services__c, 
                                                       Name From Opportunity WHERE id IN:Opportunities];
        List< SFL5_Projects__c> engagementsToCreate = new List<SFL5_Projects__c>();

        for(Opportunity opp: opps)
            if((opp.Probability>=75 && opp.Services_to_be_Delivered_by__c.contains('Training Team')) || (opp.Probability>=50 && 
                (opp.Services_to_be_Delivered_by__c.contains('Global Client Services')||opp.Services_to_be_Delivered_by__c.contains('Partner') ||  
                 opp.Services_to_be_Delivered_by__c.contains('Regional personnel')   )))   
        {
               SFL5_Projects__c engagementToAdd = new SFL5_Projects__c();

               if(opp.Account.Region__r.RSA_Manager__r.Id!=null &&  opp.Account.Region__c !=null){
               engagementToAdd.OwnerId = opp.Account.Region__r.RSA_Manager__r.Id;
                } else{
                   engagementToAdd.OwnerId = opp.Owner.Id;
                }
                engagementToAdd.Name = 'Services Engagement for: '+ opp.Account.Name;
                engagementToAdd.Opportunity__c = opp.Id;
                engagementToAdd.Client__c = opp.AccountId;
                engagementToAdd.Region__c = opp.Region__c;
                engagementToAdd.Project_Status__c = 'Active';
                engagementToAdd.Services_Amount__c = opp.Professional_Services__c;
                engagementToAdd.Services_to_be_Delivered_by__c = opp.Services_to_be_Delivered_by__c;
                engagementsToCreate.add(engagementToAdd);
         }
           insert engagementsToCreate;        
    }
   
    public void createEngagementWhenNeededOnOppUpdate(List<Opportunity> opportunities, Map<Id, Opportunity> oldMap)
    {
        List<Opportunity> opps = [SELECT Id, Threshold__c,Account.Id,Account.Name,Region__c,CreatedById,Account.Region__r.OwnerId,
                                                       Account.Region__r.RSA_Manager__r.Id,Professional_Services__c, Name From Opportunity WHERE id IN:Opportunities];
        List< SFL5_Projects__c > engagementsToCreate = new List< SFL5_Projects__c >();

        for(Opportunity opp: opportunities)
        {
            Opportunity beforeUpdate = oldMap.get(opp.Id);
            if(opp.Threshold__c=='1' && beforeUpdate.Threshold__c!='1')
           {
               SFL5_Projects__c engagementToAdd = new SFL5_Projects__c();
               
               if(opp.Account.Region__r.RSA_Manager__r.Id!=null)
               {
                  engagementToAdd.OwnerId = opp.Account.Region__r.RSA_Manager__r.Id;
                } else{
                   engagementToAdd.OwnerId = opp.OwnerId;
                }
                   engagementToAdd.Name = 'Services Engagement for: '+ opp.Account.Name;
                   engagementToAdd.Opportunity__c = opp.Id;
                   engagementToAdd.Client__c = opp.AccountId;
                   engagementToAdd.Region__c = opp.Region__c;
                   engagementToAdd.Project_Status__c = 'Active';
                   engagementToAdd.Services_Amount__c = opp.Professional_Services__c;
                   engagementsToCreate.add(engagementToAdd);
               }
         }

           insert engagementsToCreate; 
    }

}

 

kiranmutturukiranmutturu

some what more efficient way of writing the class 

 

public class ServicesEngagementManager{

    public void createEngagementWhenNeededOnOppInsert(List<Opportunity> opportunities)
    {

        /*List<Opportunity> opps = [SELECT Id,Services_to_be_Delivered_by__c,Owner.Id,Probability,Threshold__c,Account.Id,Account.Name,Region__c,
                                                       CreatedById,Account.Region__r.OwnerId,Account.Region__r.RSA_Manager__r.Id,Professional_Services__c, 
                                                       Name From Opportunity WHERE id IN:Opportunities];*/
        List< SFL5_Projects__c> engagementsToCreate = new List<SFL5_Projects__c>();

        for(Opportunity opp: [SELECT Id,Services_to_be_Delivered_by__c,Owner.Id,Probability,Threshold__c,Account.Id,Account.Name,Region__c,
                                                       CreatedById,Account.Region__r.OwnerId,Account.Region__r.RSA_Manager__r.Id,Professional_Services__c, 
                                                       Name From Opportunity WHERE id IN:Opportunities])
		{
				if((opp.Probability>=75 && opp.Services_to_be_Delivered_by__c.contains('Training Team')) || (opp.Probability>=50 && 
					(opp.Services_to_be_Delivered_by__c.contains('Global Client Services')||opp.Services_to_be_Delivered_by__c.contains('Partner') ||  
					 opp.Services_to_be_Delivered_by__c.contains('Regional personnel')   )))   
			{
				   SFL5_Projects__c engagementToAdd = new SFL5_Projects__c();

				   if(opp.Account.Region__r.RSA_Manager__r.Id!=null &&  opp.Account.Region__c !=null){
				   engagementToAdd.OwnerId = opp.Account.Region__r.RSA_Manager__r.Id;
					} else{
					   engagementToAdd.OwnerId = opp.Owner.Id;
					}
					engagementToAdd.Name = 'Services Engagement for: '+ opp.Account.Name;
					engagementToAdd.Opportunity__c = opp.Id;
					engagementToAdd.Client__c = opp.AccountId;
					engagementToAdd.Region__c = opp.Region__c;
					engagementToAdd.Project_Status__c = 'Active';
					engagementToAdd.Services_Amount__c = opp.Professional_Services__c;
					engagementToAdd.Services_to_be_Delivered_by__c = opp.Services_to_be_Delivered_by__c;
					engagementsToCreate.add(engagementToAdd);
			 }
		 }
           insert engagementsToCreate;        
    }
   
    public void createEngagementWhenNeededOnOppUpdate(List<Opportunity> opportunities, Map<Id, Opportunity> oldMap)
    {
        /*List<Opportunity> opps = [SELECT Id, Threshold__c,Account.Id,Account.Name,Region__c,CreatedById,Account.Region__r.OwnerId,
                                                       Account.Region__r.RSA_Manager__r.Id,Professional_Services__c, Name From Opportunity WHERE id IN:Opportunities];*/
        List< SFL5_Projects__c > engagementsToCreate = new List< SFL5_Projects__c >();

        for(Opportunity opp: [SELECT Id, Threshold__c,Account.Id,Account.Name,Region__c,CreatedById,Account.Region__r.OwnerId,
                                                       Account.Region__r.RSA_Manager__r.Id,Professional_Services__c, Name From Opportunity WHERE id IN:Opportunities])
        {
            Opportunity beforeUpdate = oldMap.get(opp.Id);
            if(opp.Threshold__c=='1' && beforeUpdate.Threshold__c!='1')
           {
               SFL5_Projects__c engagementToAdd = new SFL5_Projects__c();
               
               if(opp.Account.Region__r.RSA_Manager__r.Id!=null)
               {
                  engagementToAdd.OwnerId = opp.Account.Region__r.RSA_Manager__r.Id;
                } else{
                   engagementToAdd.OwnerId = opp.OwnerId;
                }
                   engagementToAdd.Name = 'Services Engagement for: '+ opp.Account.Name;
                   engagementToAdd.Opportunity__c = opp.Id;
                   engagementToAdd.Client__c = opp.AccountId;
                   engagementToAdd.Region__c = opp.Region__c;
                   engagementToAdd.Project_Status__c = 'Active';
                   engagementToAdd.Services_Amount__c = opp.Professional_Services__c;
                   engagementsToCreate.add(engagementToAdd);
               }
         }

           insert engagementsToCreate; 
    }

}

 

 

and coming to the issue check the test class whether you are setting all the data which is required to enter in to the if blocks before you are using the same field inside the code...

AKS018AKS018
This error is due to test coverage, i'e through test class. Pass all the required values to your test class method. Then deploy it