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
nksfnksf 

Test Class for Trigger Getting Error

Hi Guys,
Can you please help me with a Test Class? I am not a developer and trying to write a Test class for a Trigger I have but getting error. 
Trigger:

trigger ServiceLineBillingFrequency on kugo2p__SalesOrder__c (after update) {

Set<Id> OrderID = new Set<Id>();

 for(kugo2p__SalesOrder__c so : Trigger.new){
 OrderID.add(so.id);
}

List<kugo2p__SalesOrderServiceLine__c> SLUpdate = [select id, kugo2p__SalesOrder__c from kugo2p__SalesOrderServiceLine__c where kugo2p__SalesOrder__c in :OrderID];
 for(kugo2p__SalesOrder__c so : trigger.new){
 for (kugo2p__SalesOrderServiceLine__c osl : SLUpdate) {
 If(so.Order_Billing_Frequency__c != Null){

        osl.Billing_Frequency__c = so.Order_Billing_Frequency__c;
 }
}
}
update SLUpdate;
  }

Test Class:

@isTest
 public class BillingFrequency {
 static testMethod void InsertOrder(){
 
 Account acc = new Account();
 acc.Name = 'ABC';
 insert acc;
 
 kugo2p__SalesOrder__c SO = new kugo2p__SalesOrder__c();
 Account AccID = [Select id from Account where Name = 'ABC'];
 SO.kugo2p__Account__c = AccID.id;
 SO.CurrencyIsoCode = 'USD';
 SO.Order_Form_Type__c = 'New Order';
 SO.Renewal_Term_Exceptions__c = 'None';
 SO.Purchase_Order_Required__c = 'NO';
 SO.Renewal_Term_Notification__c = '30';
 SO.Order_Billing_Frequency__c = 'Monthly';
 insert SO;
 
 kugo2p__SalesOrderServiceLine__c SL = new kugo2p__SalesOrderServiceLine__c();
 SO =[Select id from kugo2p__SalesOrder__c where id = :SO.id];
 Sl.kugo2p__SalesOrder__c = SO.id;
 SL.kugo2p__Service__c = 'a2B40000000Lk85';
 SL.kugo2p__Quantity__c = 2;
 SL.CurrencyIsoCode = 'USD';
 date mydate = date.parse('08/03/2015');
 SL.kugo2p__DateServiceStart__c = mydate;
 insert SL;
 
 SO.kugo2p__RecordStatus__c = 'approved';
 update SO;
 }
 }

Error Message:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, kugo2p.SalesOrderBeforeInsert: execution of BeforeInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
(kugo2p)
Stack Trace Class.BillingFrequency.InsertOrder: line 18, column 1
Himanshu ParasharHimanshu Parashar
Update your trigger with following code
 
trigger ServiceLineBillingFrequency on kugo2p__SalesOrder__c (after update) {

Set<Id> OrderID = new Set<Id>();

 for(kugo2p__SalesOrder__c so : Trigger.new){
 OrderID.add(so.id);
}

List<kugo2p__SalesOrderServiceLine__c> SLUpdate = [select id, kugo2p__SalesOrder__c from kugo2p__SalesOrderServiceLine__c where kugo2p__SalesOrder__c in :OrderID];
 for(kugo2p__SalesOrder__c so : trigger.new){
 for (kugo2p__SalesOrderServiceLine__c osl : SLUpdate) {
 If(so.Order_Billing_Frequency__c != Null){

        osl.Billing_Frequency__c = so.Order_Billing_Frequency__c;
 }
}
}
if(SLUpdate.size()>0)
update SLUpdate;
  }

 
Vivek DeshmaneVivek Deshmane
Hi,
Update your trigger code as below and let me know if it works.
trigger ServiceLineBillingFrequency on kugo2p__SalesOrder__c (after update) 
{

		Set<Id> OrderID = new Set<Id>();

		 for(kugo2p__SalesOrder__c so : Trigger.new){
		 OrderID.add(so.id);
		}

		List<kugo2p__SalesOrderServiceLine__c> SLUpdate = [select id, kugo2p__SalesOrder__c from kugo2p__SalesOrderServiceLine__c where kugo2p__SalesOrder__c in :OrderID];
		if(SLUpdate !=null &&  !SLUpdate.isEmpty())
		{
			for(kugo2p__SalesOrder__c so : trigger.new)
			{
			 
			 for (kugo2p__SalesOrderServiceLine__c osl : SLUpdate) 
			 {
			  if(so.Order_Billing_Frequency__c != Null)
			  {

					osl.Billing_Frequency__c = so.Order_Billing_Frequency__c;
			 }
			}
		  }
		update SLUpdate;
		}
  }

Best Regards.
-Vivek
 
nksfnksf
Hi Himanshu and Vivek,
I have tried to update my Trigger but getting same error message when I run the Test Class. May be the issue is with the Test Class?

Error Message: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, kugo2p.SalesOrderBeforeInsert: execution of BeforeInsert
caused by: System.NullPointerException: Attempt to de-reference a null object (kugo2p)

Test Class:
@isTest
 public class BillingFrequency {
 public static testMethod void InsertOrder(){
 
 Account acc = new Account();
 acc.Name = 'ABC';
 insert acc;
 
 kugo2p__SalesOrder__c SO = new kugo2p__SalesOrder__c();
 Account AccID = [Select id from Account where Name = 'ABC'];
 SO.kugo2p__Account__c = AccID.id;
 SO.CurrencyIsoCode = 'USD';
 SO.Order_Form_Type__c = 'New Order';
 SO.Renewal_Term_Exceptions__c = 'None';
 SO.Purchase_Order_Required__c = 'NO';
 SO.Renewal_Term_Notification__c = '30';
 SO.Order_Billing_Frequency__c = 'Monthly';
 insert SO;
 
 kugo2p__SalesOrderServiceLine__c SL = new kugo2p__SalesOrderServiceLine__c();
 SO =[Select id from kugo2p__SalesOrder__c where id = :SO.id];
 Sl.kugo2p__SalesOrder__c = SO.id;
 SL.kugo2p__Service__c = 'a2B40000000Lk85';
 SL.kugo2p__Quantity__c = 2;
 SL.CurrencyIsoCode = 'USD';
 date mydate = date.parse('08/03/2015');
 SL.kugo2p__DateServiceStart__c = mydate;
 insert SL;
 
 SO.kugo2p__RecordStatus__c = 'approved';
 update SO;
 }
 }
Vivek DeshmaneVivek Deshmane
Hi,

Message is clearly saying error in execution of BeforeInsert . do you any BeforeInsert  trigger 
as SalesOrderBeforeInsert on SalesOrder . please share it.

Best Regards,
-Vivek
 
nksfnksf
Hi Vivek,
I found this Trigger

trigger SalesOrderBeforeInsert on kugo2p__SalesOrder__c (before insert) {

  map<Id, Opportunity> mapOppty = new map<Id, Opportunity>();
  map<Id, kugo2p__SalesQuote__c> mapQuote = new map<Id, kugo2p__SalesQuote__c>();  

  for (kugo2p__SalesOrder__c so : trigger.new) {
    if (so.kugo2p__SalesQuote__c != null) {
      mapQuote.put(so.kugo2p__SalesQuote__c, null);
    } else if (so.kugo2p__Opportunity__c != null) {
      mapOppty.put(so.kugo2p__Opportunity__c, null);
    }
  }

  if (!mapQuote.isEmpty()) mapQuote = new map<Id, kugo2p__SalesQuote__c>([select Id, kugo2p__BillingFrequency__c, Renewal_Term_Exceptions__c, kugo2p__PaymentTerms__c, kugo2p__Opportunity__r.Opportunity_Type__c, kugo2p__Opportunity__r.OriginalContract__r.Order_Form_Type__c from kugo2p__SalesQuote__c where Id in : mapQuote.keySet()]);
  if (!mapOppty.isEmpty()) mapOppty = new map<Id, Opportunity>([select Id, Billing_Frequency__c, Renewal_Term_Exceptions__c, Payment_Terms__c, Opportunity_Type__c, OriginalContract__r.Order_Form_Type__c from Opportunity where Id in : mapOppty.keySet()]);

  if (!mapQuote.isEmpty() || !mapOppty.isEmpty()) {
    for (kugo2p__SalesOrder__c so : trigger.new) {
      if (so.kugo2p__SalesQuote__c != null) {
        so.kugo2p__PaymentTerms__c = mapQuote.get(so.kugo2p__SalesQuote__c).kugo2p__PaymentTerms__c;
        so.Renewal_Term_Exceptions__c = mapQuote.get(so.kugo2p__SalesQuote__c).Renewal_Term_Exceptions__c;
        if (mapQuote.get(so.kugo2p__SalesQuote__c).kugo2p__Opportunity__r.Opportunity_Type__c == 'Upsell') so.Order_Form_Type__c = mapQuote.get(so.kugo2p__SalesQuote__c).kugo2p__Opportunity__r.OriginalContract__r.Order_Form_Type__c;
      } else if (so.kugo2p__Opportunity__c != null) {
        so.kugo2p__PaymentTerms__c = mapOppty.get(so.kugo2p__Opportunity__c).Payment_Terms__c;
        so.Renewal_Term_Exceptions__c = mapOppty.get(so.kugo2p__Opportunity__c).Renewal_Term_Exceptions__c;
        if (mapOppty.get(so.kugo2p__Opportunity__c).Opportunity_Type__c == 'Upsell') so.Order_Form_Type__c = mapOppty.get(so.kugo2p__Opportunity__c).OriginalContract__r.Order_Form_Type__c;
      }
    }
  }
}
nksfnksf
Any idea Vivek?