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
affuaffu 

Trigger Code coverage

Hi all,

I have written a Trigger but i am not able to write testclass and tried different ways plz help me out...

 

This trigger works as roll-up summary 

trigger TotalQtyShippedInsaleorderitem on Dispatch_Detail_Item__c (after delete, after insert, after undelete, 
after update) {
  Map<Id,Sale_Order_Item__c> mapsoi = new Map<Id,Sale_Order_Item__c>();
    Set<Id> soiId = new Set<Id>();
    try {
        // If insert, update or undelete put the new values into soiId
        if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete){
            for(Dispatch_Detail_Item__c ddi : Trigger.new) {
                soiId.add(ddi.Sale_Order_Item__c);
            }
        }
        // If we are updating, some Sale_Order_Item__c values might change, so include that as well as deletes
        if(Trigger.isUpdate || Trigger.isDelete){
            for(Dispatch_Detail_Item__c ddi : Trigger.old) {
                soiId.add(ddi.Sale_Order_Item__c);
            }
        }
        
        soiId.remove(null);
        for(Id pId : soiId) {
            mapsoi.put(pId,new Sale_Order_Item__c(Id=pId,Total_Of_Dispatches_Shipped__c=0));
        }
        
        for(Dispatch_Detail_Item__c ddi : [select Total_Amt_Shipped__c,Sale_Order_Item__c from Dispatch_Detail_Item__c WHERE Sale_Order_Item__c IN: soiId]) {
            mapsoi.get(ddi.Sale_Order_Item__c).Total_Of_Dispatches_Shipped__c += ddi.Total_Amt_Shipped__c;
        }
        
        update mapsoi.values();
    }
    catch(Exception e) {
        System.debug('### Exception: ' + e.getMessage());
    }

}

 Here is the Testclass, There are two objects "Sale order item" and" Dispatch detail item" but the problem is"sale order item" is related to "sale order" object and i have some mandatory lookups and master detail fields like DEALER ACCOUNT master detail to ACCOUNT , CUSTOMER ACCOUNT lookup to ACCOUNT and OPPORTUNITY lookup . How can i call this id's.

@istest
private class TotalShippedTestClass {
    static testMethod void myUnitTest() { 
        Sale_Order_Item__c soi = new Sale_Order_Item__c(Sale_Order__c='a0KO000000096Wl');
        Dispatch_Detail_Item__c ddi = new Dispatch_Detail_Item__c(Sale_Order_Item__c=soi.id);
        insert soi;
        insert ddi;
        update soi;
        update ddi;
    }
}

 

Vishwanath AVishwanath A

Hi,

 

You need to insert all the parent object,

Navatar_DbSupNavatar_DbSup

Hi,


For those object like account you have to insert the account and similarly other record.

 

After insert you can use the id of that record inside. Your test method should be like below(Make changes accourdingly).
@istest
private class TotalShippedTestClass
{
static testMethod void myUnitTest()
{
Account ac=new account(name=’test’);
Insert ac;
Sale_Order__c s=new Sale_Order__c(name=’abc’,other mendatory fields);
Insert s;
Dispatch_Detail_Item__c ddi = new Dispatch_Detail_Item__c(Sale_Order_Item__c=s.id,account__c=ac.id,othermendatory fields);
Insert ddi;
Update ddi;
}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

affuaffu

Hi jain,

Thanks for the reply, but the trigger runs like this

 

1. First we will create sale order(name=auto number)  and i have got sale order item related list in sales order.

2. In sale order item (name=auto number)  i have dispatch detail item (name=auto number)  related list and we will create dispatch detail item and it takes the roll up of dispatch detail items in sales order item.

 

But refering to the first step in "SALE ORDER" object i have three mandatory fields 

 

Dealer account = master-detail to account

Customer Account = lookup to account.

Project = lookup to Opportunity (here in opportunity i have trigger it updates the customer account field).

 

The problem is if we refer this above three id's in sale order it is not taking any code coverage to this trigger.