You need to sign in to do that
Don't have an account?

Test Class Code Coverage 0%
I have written a test class to create orderline product once the order is created.

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

@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;
}
}
}
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();
}
}
Orderitem will be created automatically when an order is inserted.