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 Code Coverage 0%

I have written a test class to create orderline product once the order is created.



User-added image


@isTest
public class orderproductTestClass {
    
    public static testMethod void testorderproduct(){
       Map<Id,Id> mapof = new Map<Id,Id>();
       OpportunityLineItem__c pd = new OpportunityLineItem__c();
       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;
        Test.StopTest();
    } 
}

Apex 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;
        } 
    }
}

User-added image
Shruthi NarsiShruthi Narsi
code cpovergae is 0% can u help me reach 100

below is the trigger

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();
       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;

       Test.StartTest();
        insert orderItemsForInsert;
        Test.StopTest();
    } 
}
Shruthi NarsiShruthi Narsi
its creation of orderline items 
chanchal_:)chanchal_:)
Try writing this and let me know the errors.
Orderitem will be created automatically when an order is inserted. 
@isTest
public class orderproductTestClass {
    @isTest
    public static testMethod void testorderproduct(){
    Opportunity__c opp = new opportunity__c();
    opp.stage = 'Closed Won';
    insert opp;

    product2 opr = new product2();
    opr.Name = 'test';
    insert opr;

	order or : new order();
	or.Opportunity__c = opp.Id
	insert or;

    OpportunityLineItem__c oli = new OpportunityLineItem__c();
    oli.name = 'test';
    oli.product2Id = opr.Id;
    oli.OpportunityId__c = opp.Id;
    oli.ListPrice__c = 1000;
    oli.Sales_Price__c = 1000;
    oli.Quantity__c = 2;
    insert oli
	
	
    }
}

 
Shruthi NarsiShruthi Narsi
ya we are customizing it so we need orderproduct to be created once quoyte is synced...for opportunityline item I have already created and finished it in another trigger...can u see the trigger and help with code cpvergare to atleast 90%
chanchal_:)chanchal_:)
I didn't check you have used public static testMethod. You can remove @isTest notation that is written above method. 
Shruthi NarsiShruthi Narsi
ok can u help me with the trigger
chanchal_:)chanchal_:)
yes whatever trigger you have pasted. use this test class which i just pasted. have you tried ?
Shruthi NarsiShruthi Narsi
its giving error since we have to add orderlineotems and not opportunity line items
Shruthi NarsiShruthi Narsi
sent an email