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
Chandu007Chandu007 

getting System.DmlException: Insert failed. in test class for trigger. how to resolve this?

Getting System.DMLException in test class for below trigger. Please help me to solve it.

Trigger:- 

trigger OpportunityHowell on Opportunity (after Insert,after Update) {
   
  List<Opportunity> listOpportunitiesToUpdate = new List<Opportunity>();
  Set<Id> allOids = new Set<Id>();
  for(Opportunity o: Trigger.new){
    if(o.Multiplier__c != null && o.Multiplier__c != trigger.oldMap.get(o.id).Multiplier__c){
      allOids.add(o.id);
    }
  }
  listOpportunitiesToUpdate = [select id, (SELECT Id,Dollar_Sales_Howell__c from OpportunityLineItems) from Opportunity where id in :allOids];
  for(Opportunity o:listOpportunitiesToUpdate){
    o.Sum_of_Dollar_Sales_Howell__c = 0;
    for(OpportunityLineItem oli: o.OpportunityLineItems)
    Try{
      o.Sum_of_Dollar_Sales_Howell__c += oli.Dollar_Sales_Howell__c;
      }
    Catch(Exception e)
    {
  }
  if(listOpportunitiesToUpdate.size()>0)   
    update listOpportunitiesToUpdate;    
    }
  }

Test class:-

@isTest
public class OpportunityHowellTest {

    public static testMethod void TestOppty(){
        
         Set<Id> allOids = new Set<Id>();
         MHC2__Project__c p = new MHC2__Project__c(Name='Test Project',MHC2__MHC_Project_Bid_Date__c=System.Today(),MHC2__MHC_Target_Bid_Date__c=System.Today());
        insert p;
        Account a = new Account(Name='Test Account');
        insert a;
        
        Opportunity op = new Opportunity();
        op.Name = 'My Test Opportunity';
        op.Multiplier__c = 2;
        op.Sum_of_Dollar_Sales_Howell__c = 0;
        op.Bid_Date_Time__c = System.Now().addDays(30);
        op.CloseDate = System.Now().addDays(45).date();
        op.StageName = 'Bidding';
        op.AccountId = a.Id;
        op.Bid_Type__c = 'Lump Sum';
        op.Project__c = p.Id;
        insert op;
        
        op.Multiplier__c = 5;
        
        update op;
     
        
       OpportunityLineItem oli = new OpportunityLineItem();
        oli.Quantity = 1.0;
        oli.OpportunityId = op.Id;
        oli.Invoiced__c = false;
        insert oli; 
  }
}

Error:-

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunityHowell: execution of AfterInsert

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

Trigger.OpportunityHowell: line 6, column 1: []
Best Answer chosen by Chandu007
MKRMKR
Hi,

If that is the case then remove the following line.
op.Multiplier__c = 2;
The trigger code will always produce the error if there is a value in the Multiplier__c field on insert.

Regards,
Mkr
 

All Answers

MKRMKR
Hi,

Trigger.OldMap is null in after insert trigger. It can only be used in update triggers. You can try following instead with Trigger.isInsert:
if(o.Multiplier__c != null && (Trigger.isInsert || o.Multiplier__c != trigger.oldMap.get(o.id).Multiplier__c)) {
    allOids.add(o.id);
}

Regards,
Mkr
Chandu007Chandu007
Hi Mkr,

Thanks for your suggestion. I have been asked to write test classes for triggers and i am not supposed to modify existing code in the triggers. thats the reason i have posted this in forum. Is there any other solution to cover the code coverage. Please confirm.

Thanks,
Chandra
Sunil RathoreSunil Rathore
Hi Chandu007,

Update Opportunity record after the insertion of OpportunityLineItem in your test class.

Let me know if it helps you.

Many Thanks,
Sunil Rathore
MKRMKR
Hi,

If that is the case then remove the following line.
op.Multiplier__c = 2;
The trigger code will always produce the error if there is a value in the Multiplier__c field on insert.

Regards,
Mkr
 
This was selected as the best answer
Chandu007Chandu007
Thanks Mkr for providing your valuble suggestion.