+ Start a Discussion
Vrushali Mali 4Vrushali Mali 4 

I have one trigger writing test class for the same but i am not able to cover it at least 1 % please help me in this, here is my code

trigger CreateOrder on Order__c (before insert,After Insert) {
        Set<Id> OppId = new Set<Id>();
        Set<Id> OffId = new Set<Id>();
        Map<Id,Offer__c> MapOff = new Map<Id,Offer__c>();
        List<Product_Detail_for_Order__c> ordforProd = new List<Product_Detail_for_Order__c>();
        Map<Id,List<Product_Detail_for_Offer__c>> offforProd = new Map<Id,List<Product_Detail_for_Offer__c>>();
    
    if(Trigger.isbefore){
        for(Order__c ord : Trigger.new){
            if(!String.isBlank(ord.Opportunity__c))
            {
                OppId.add(ord.Opportunity__c);
            }
            else if(!String.isBlank(ord.Offer__c))
            {
                OffId.add(ord.Offer__c);
            }
        }
        if(!OppId.isEmpty())
        {
            for(Offer__c off : [Select Id,Name,Opportunity__c,Partner__c from Offer__c where Opportunity__c =:OppId ORDER BY CreatedDate ASC NULLS FIRST])
            {
                MapOff.put(off.Opportunity__c, off);
            }
        }
        if(!OffId.isEmpty())
        {
            for(Offer__c ofo : [Select Id,Name,Opportunity__c,Partner__c from Offer__c where Id =: OffId] ){
                MapOff.put(ofo.Id,ofo);
            }
        }
        for(Order__c order : Trigger.new)
        {
            if(!String.isBlank(order.Opportunity__c))
            {
                Offer__c ofs = MapOff.get(order.Opportunity__c);
                order.Partner_Name__c = ofs.Partner__c;
                order.Opportunity__c = ofs.Opportunity__c;
                order.Id = ofs.Id;
             
            }
            else if(!String.isBlank(order.Offer__c))
            {
                Offer__c ofs = MapOff.get(order.Offer__c);
                order.Partner_Name__c = ofs.Partner__c;
                order.Opportunity__c = ofs.Opportunity__c;
                
            }
        }
    }
    
    if(Trigger.isAfter)
    {
        for(Order__c order : Trigger.new)
        {
            if(!String.isBlank(order.Offer__c)){
                OffId.add(order.Offer__c);
            }
        }
        for(Product_Detail_for_Offer__c ensp  : [Select Id,Currency__c,Customer_Drawing_Number__c,Customer_Item_No_Drawing_No1__c,Model_No__c,Enquiry_Product_Backend__c,Product_Description__c,Offer__c,Offer_Type__c,Opportunity__c,Partner_Drawing_Number__c,Partner_Item_Material_Code__c,Partner_Item_No_Drawing_No__c,Parter_Model_Code__c,Product__c,Product_code_Description__c,Quantity__c,Rate_Contract__c,Regret__c,Total_Amount__c,Unit_Price__c from Product_Detail_for_Offer__c where Id =:OffId])
        {
            List<Product_Detail_for_Offer__c> eis = new List<Product_Detail_for_Offer__c>();
            if(offforProd.get(ensp.Offer__c) != null)
                eis = offforProd.get(ensp.Offer__c);
                eis.add(ensp);
                offforProd.put(ensp.Offer__c, eis);
            
        }
            for(Order__c ord : Trigger.new){
                
                List<Product_Detail_for_Offer__c> eis = new List<Product_Detail_for_Offer__c>();
                eis = offforProd.get(ord.Offer__c);
                System.debug('******eis*****'+eis);
                for(Product_Detail_for_Offer__c off : eis){
                    
                    Product_Detail_for_Order__c ordp = new Product_Detail_for_Order__c();
                    ordp.Opportunity__c = off.Opportunity__c;
                    ordp.Customer_Item_Material_Code__c = off.Customer_Item_No_Drawing_No1__c;
                    ordp.Customer_Drawing_Number__c = off.Customer_Drawing_Number__c;
                    ordp.Product__c = off.Product__c;
                    ordp.Quantity__c = off.Quantity__c;
                    ordp.Unit_Price__c = off.Unit_Price__c;
                    ordp.Order__c = ord.Id;
                    
                    ordforProd.add(ordp);
                }
            }
            System.debug('******ordforProd******'+ordforProd);
            Insert ordforProd;
        }
    }
Balayesu ChilakalapudiBalayesu Chilakalapudi
Create objects and fields related to your trigger in the test methods like this,
 
@isTest
public class OrderTriggerTest{
   @isTest
   static void test1(){
        Opportunity opp=new Opportunity();
        insert opp;
        Order__c  od=new Order__c();
        od.opportunity__c=opp.Id;
         insert od;
        Product_Detail_for_Offer__c  pdf=new Product_Detail_for_Offer__c();
        insert pdf;
        Offer__c  off=new Offer__c();
        insert off;
   }
}