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
Shruthi NarsiShruthi Narsi 

test Class To OrderLine Items

I have written the below code to add orderlineitems once quote is synced.
Once quote is synced the order gets created and the order procducts will get created. Can u help me with the code covergae as 100% its 0% now

trigger Createorderproduct on Order__c (after insert) {
    Set<Id> orderIdset= new Set<Id>();
    List<Order__c> orderList = new List<Order__c>();
    Map<Id,Id> opMap = new Map<Id,Id>();
    List<OrderItem__c> orderItemsForInsert = new List<OrderItem__c>();
    for(Order__c o : Trigger.new){
        if(o.Opportunity__c!= null){
            opMap.put(o.Opportunity__c ,o.Id);
        }
    }
    
    if(!opMap.isEmpty()){
        for(OpportunityLineItem__c oli: [Select id, name, OpportunityId__r.Stage__c, Product2Id__r.name, ListPrice__c, 
                                         OpportunityId__c, Sales_Price__c, Quantity__c 
                                         FROM OpportunityLineItem__c 
                                         WHERE OpportunityId__c IN: opMap.Keyset() 
                                         AND OpportunityId__r.Stage__c='Closed Won'])
        {
            OrderItem__c OrderItem = new OrderItem__c();
            OrderItem.OrderId__c = opMap.get(oli.OpportunityId__c);
            OrderItem.Name = oli.Product2Id__r.name;
            OrderItem.ListPrice__c = oli.ListPrice__c;
            OrderItem.Sales_Price__c = oli.Sales_Price__c;
            OrderItem.Quantity__c = oli.Quantity__c;
            orderItemsForInsert.add(OrderItem);                
        }
        if(orderItemsForInsert.size()>0)
        {
            insert orderItemsForInsert;
        } 
    }
}

Test Class

@isTest
public class orderproductTestClass {
    
    public static testMethod void testorderproduct(){
       Map<Id,Id> mapof = new Map<Id,Id>();
       OpportunityLineItem__c pd = new OpportunityLineItem__c();
       pd.OpportunityId__r.Stage__c='Closed Won' ;
        OrderItem__c orderItemsForInsert = new OrderItem__c();
        
        OrderItem__c oi = new OrderItem__c();
        oi.OrderId__c = mapof.get(pd.OpportunityId__c);
        oi.Name = pd.Product2Id__r.name;
        oi.ListPrice__c = pd.ListPrice__c;
        oi.Sales_Price__c = pd.Sales_Price__c;
        oi.Quantity__c = pd.Quantity__c;
        insert orderItemsForInsert;
        
    } 
}
 
AnudeepAnudeep (Salesforce Developers) 
Hi Shruthi, 

Let me know if this gives any coverage
@isTest

public class orderproductTestClass {
    
    public static testMethod void testorderproduct(){

    // Insert Account

    Account a = new Account();
    a.Name = 'Test Account';
    insert a;

    // Insert Product
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    

    Id pricebookId = Test.getStandardPricebookId();
    
    // Insert PricebookEntry

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = pricebookId;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
    
    // Insert Order
    
    Order o = new Order();
    o.Name = 'Test Order ';
    o.Status = 'Draft';
    o.EffectiveDate = system.today();
    o.EndDate = system.today() + 4;
    o.AccountId = a.id;
    o.Pricebook2Id =  pricebookId ;
    
    insert o;
    
    // Insert Order Item

    OrderItem i = new OrderItem();
    i.OrderId = o.id;
    i.Quantity = 24;
    i.UnitPrice = 240;
    i.Product2id = p.id;
    i.PricebookEntryId=standardPrice.id;
    insert i;

    // Insert second order Item

    Map<Id,Id> mapof = new Map<Id,Id>();
       OpportunityLineItem__c pd = new OpportunityLineItem__c();
       pd.OpportunityId__r.Stage__c='Closed Won' ;
        OrderItem__c orderItemsForInsert = new OrderItem__c();
        
        OrderItem__c oi = new OrderItem__c();
        oi.OrderId__c = mapof.get(pd.OpportunityId__c);
        oi.Name = pd.Product2Id__r.name;
        oi.ListPrice__c = pd.ListPrice__c;
        oi.Sales_Price__c = pd.Sales_Price__c;
        oi.Quantity__c = pd.Quantity__c;
        insert orderItemsForInsert;
        
    } 
}
Anudeep
Shruthi NarsiShruthi Narsi
its wrong its hould be someting like this. In this if u make changes it will help

@isTest
public class orderproductTestClass {
    
    public static testMethod void testorderproduct(){
       Map<Id,Id> mapof = new Map<Id,Id>();
       OpportunityLineItem__c pd = new OpportunityLineItem__c();
       pd.stage = 'Closed Won';
        OrderItem__c orderItemsForInsert = new OrderItem__c();
        
        OrderItem__c oi = new OrderItem__c();
        oi.OrderId__c = mapof.get(pd.OpportunityId__c);
        oi.Name = pd.Product2Id__r.name;
        oi.ListPrice__c = pd.ListPrice__c;
        oi.Sales_Price__c = pd.Sales_Price__c;
        oi.Quantity__c = pd.Quantity__c;
        insert orderItemsForInsert;
        
    } 
}
 
AnudeepAnudeep (Salesforce Developers) 
Hi Shruthi, 

Your trigger is on Order__c so please try inserting Order__c along with OrderItem__c in your test class

Anudeep
Shruthi NarsiShruthi Narsi
I dint understand can u write it in the code
Shruthi NarsiShruthi Narsi
@isTest
public class orderproductTestClass {
    
    public static testMethod void testorderproduct(){
       Map<Id,Id> mapof = new Map<Id,Id>();
  OpportunityLineItem__c pd = new OpportunityLineItem__c();
        pd.OpportunityId__r.Stage__c='Closed Won';
        pd.OpportunityId__r.Name ='Test';
        pd.ListPrice__c = 10 ;
              
     insert pd;
OrderItem__c orderItemsForInsert = new OrderItem__c();
        
        OrderItem__c oi = new OrderItem__c();
        oi.OrderId__c = mapof.get(pd.OpportunityId__c);
        oi.Name = pd.Product2Id__r.name;
        oi.ListPrice__c = pd.ListPrice__c;
        oi.Sales_Price__c = pd.Sales_Price__c;
        oi.Quantity__c = pd.Quantity__c;
        insert orderItemsForInsert;
        
    } 
}
        
        
        
        
      
        
 
Shruthi NarsiShruthi Narsi
its stills showing 0%
AnudeepAnudeep (Salesforce Developers) 
Hi Shruthi, 

Your test class should insert order__c like the example below (Please populate all required fields for opportunity__c and order__c before inserting them)

@isTest
public class orderproductTestClass {
    
    public static testMethod void testorderproduct(){
       Map<Id,Id> mapof = new Map<Id,Id>();
  OpportunityLineItem__c pd = new OpportunityLineItem__c();
        pd.OpportunityId__r.Stage__c='Closed Won';
        pd.OpportunityId__r.Name ='Test';
        pd.ListPrice__c = 10 ;
              
     insert pd;
OrderItem__c orderItemsForInsert = new OrderItem__c();
        
        OrderItem__c oi = new OrderItem__c();
        oi.OrderId__c = mapof.get(pd.OpportunityId__c);
        oi.Name = pd.Product2Id__r.name;
        oi.ListPrice__c = pd.ListPrice__c;
        oi.Sales_Price__c = pd.Sales_Price__c;
        oi.Quantity__c = pd.Quantity__c;
        insert orderItemsForInsert;

        //Insert opportunity 

        Opportunity__c opp = new Opportunity__c(); 
        opp.Name = 'Test'; 
        opp.Stage__c = 'Closed Won';
        insert opp;

       //Insert order

        Order__c ord = new Order__c(); 
        ord.Opportunity__c = opp.Id;
        insert ord;
        
    } 
}
        
Anudeep
Shruthi NarsiShruthi Narsi
The code coverage is 55%

User-added image

trigger Createorderproduct on Order__c (after insert) {
    Set<Id> orderIdset= new Set<Id>();
    List<Order__c> orderList = new List<Order__c>();
    Map<Id,Id> opMap = new Map<Id,Id>();
    List<OrderItem__c> orderItemsForInsert = new List<OrderItem__c>();
    for(Order__c o : Trigger.new){
        if(o.Opportunity__c!= null){
            opMap.put(o.Opportunity__c ,o.Id);
        }
    }
    
    if(!opMap.isEmpty()){
        for(OpportunityLineItem__c oli: [Select id, name, OpportunityId__r.Stage__c, Product2Id__r.name, ListPrice__c, 
                                         OpportunityId__c, Sales_Price__c, Quantity__c 
                                         FROM OpportunityLineItem__c 
                                         WHERE OpportunityId__c IN: opMap.Keyset() 
                                         AND OpportunityId__r.Stage__c='Closed Won'])
        {
            OrderItem__c OrderItem = new OrderItem__c();
            OrderItem.OrderId__c = opMap.get(oli.OpportunityId__c);
            OrderItem.Name = oli.Product2Id__r.name;
            OrderItem.ListPrice__c = oli.ListPrice__c;
            OrderItem.Sales_Price__c = oli.Sales_Price__c;
            OrderItem.Quantity__c = oli.Quantity__c;
            orderItemsForInsert.add(OrderItem);                
        }
        if(orderItemsForInsert.size()>0)
        {
            insert orderItemsForInsert;
        } 
    }
}

Test Class

trigger Createorderproduct on Order__c (after insert) {
    Set<Id> orderIdset= new Set<Id>();
    List<Order__c> orderList = new List<Order__c>();
    Map<Id,Id> opMap = new Map<Id,Id>();
    List<OrderItem__c> orderItemsForInsert = new List<OrderItem__c>();
    for(Order__c o : Trigger.new){
        if(o.Opportunity__c!= null){
            opMap.put(o.Opportunity__c ,o.Id);
        }
    }
    
    if(!opMap.isEmpty()){
        for(OpportunityLineItem__c oli: [Select id, name, OpportunityId__r.Stage__c, Product2Id__r.name, ListPrice__c, 
                                         OpportunityId__c, Sales_Price__c, Quantity__c 
                                         FROM OpportunityLineItem__c 
                                         WHERE OpportunityId__c IN: opMap.Keyset() 
                                         AND OpportunityId__r.Stage__c='Closed Won'])
        {
            OrderItem__c OrderItem = new OrderItem__c();
            OrderItem.OrderId__c = opMap.get(oli.OpportunityId__c);
            OrderItem.Name = oli.Product2Id__r.name;
            OrderItem.ListPrice__c = oli.ListPrice__c;
            OrderItem.Sales_Price__c = oli.Sales_Price__c;
            OrderItem.Quantity__c = oli.Quantity__c;
            orderItemsForInsert.add(OrderItem);                
        }
        if(orderItemsForInsert.size()>0)
        {
            insert orderItemsForInsert;
        } 
    }
}