You need to sign in to do that
Don't have an account?
Balasubramani Dhanapal
System.DmlException: Insert failed. CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OrderTrigger: execution of AfterInsert caused by: System.DmlException: Insert failed. first error: FIELD_INTEGRITY_EXCEPTION, Price Book Not Set on Order: []
Hi,
I have written code for class named as "InvoiceTriggerhandler".this class corresponding test class named as "Invocie Triggerhandlertest".my test class in failing and i got the below error "
Invoice Trigger Handler:
Public Class InvoiceTriggerHandler{
Public Void OnAfterInsert(Map<id, Invoice__c> newInvoiceMap,Map<id, Invoice__c> oldInvoiceMap){
}
Public Void OnAfterUpdate(Map<id, Invoice__c> newInvoiceMap,Map<id, Invoice__c> oldInvoiceMap){
if(RecursiveHandler.runITH_POOnce()) PlaceOrder(newInvoiceMap, oldInvoiceMap);
}
Private Void PlaceOrder(Map<id, Invoice__c> newInvoiceMap, Map<id, Invoice__c> oldInvoiceMap){
List<id> invIdToPlaceOrders=new List<Id>();
List<id> quoteIds=new List<Id>();
for(Invoice__c inv:newInvoiceMap.values()){
if(inv.Generate_order__c== True && inv.Generate_order__c!=oldInvoiceMap.get(inv.Id).Generate_order__c){
invIdToPlaceOrders.add(inv.id);
quoteIds.add(inv.Quote__c);
}
}
Map<id, Quote> qteMap=new Map<Id, Quote>([SELECT BillingCity,BillingCountry,BillingCountryCode,BillingLatitude,BillingLongitude,BillingName,BillingPostalCode,BillingState,BillingStateCode,BillingStreet,ContactId,Contact.FirstName,Contact.LastName,Description,Discount,Email,ExpirationDate,Fax,GrandTotal,Id,LineItemCount,Name,OpportunityId,Phone,Pricebook2Id,QuoteNumber,ShippingCity,ShippingCountry,ShippingCountryCode,ShippingHandling,ShippingLatitude,ShippingLongitude,ShippingName,ShippingPostalCode,ShippingState,ShippingStateCode,ShippingStreet,Status,Subtotal,Tax,TotalPrice,Dead_Line__c FROM Quote where id IN:quoteIds]);
List<Order> listOrderToInsert=new List<Order>();
Order temp;
Invoice__c inv;
Quote qte;
for(id invId:invIdToPlaceOrders){
temp=new Order();
inv=newInvoiceMap.get(invId);
qte=qteMap.get(inv.Quote__c);
temp.QuoteId=inv.Quote__c;
temp.AccountId=inv.AccountID__c;
temp.OpportunityId=inv.Opportunity__c;
temp.billTocontactId=qte.ContactID;
temp.billingStreet=qte.billingStreet;
temp.billingCity=qte.billingCity;
temp.billingState=qte.billingState;
temp.billingCountry=qte.billingCountry;
temp.BillingPostalCode=qte.BillingPostalCode;
temp.ShippingStreet=qte.ShippingStreet;
temp.ShippingCity=qte.ShippingCity;
temp.ShippingState=qte.ShippingState;
temp.ShippingCountry=qte.ShippingCountry;
temp.ShippingPostalCode=qte.ShippingPostalCode;
temp.EffectiveDate=System.today();
temp.Status='Initial Progress';
temp.Invoice__c=invId;
temp.Pricebook2Id=qte.Pricebook2Id;
temp.EndDate=qte.Dead_Line__c;
listOrderToInsert.add(temp);
}
if(listOrderToInsert.size()>0)
insert listOrderToInsert;
}
}
Invoice Triggerhandler Test:
---------------------------------------
@isTest(SeeAllData=true)
Public Class InvoiceTriggerHandlerTest{
@isTest public Static void TestPlaceOrder(){
Account a= new Account(Name='testprimary');
insert a;
Contact NewCon= New Contact ( LastName ='Test contact',Accountid =a.id);
insert NewCon;
Opportunity opp=New Opportunity(Name='Test Opportunity',Accountid=a.id,StageName='Closed Won',CloseDate=System.today());
insert opp;
Quote qt=[SELECT id,Name FROM Quote Limit 1];
Product2 prod=[SELECT id,Category__c from Product2 where Exclude_Pricing_Matrix__c=false AND Category__c='Item Category: A' LIMIT 1];
Id pbId=[SELECT id from PriceBook2 where name='Standard Price Book' LIMIT 1].id;
id PBEId=[SELECT id from PricebookEntry where Product2Id=:prod.Id and Pricebook2Id=:pbId LIMIT 1].Id;
QuoteLineItem qli=new QuoteLineItem (quoteid=qt.id,PricebookEntryId=PBEId,Follow_Pricing_Matrix__c=TRUE,Capacity__c='64 MB',Description='Testing Description',Quantity=30,UnitPrice=100);
insert qli;
Order ord=New Order(Name='Test Opportunity',Quoteid=qt.id, Accountid=a.id,Opportunityid=opp.id,BillToContactid=NewCon.id,ShipToContactid=NewCon.id,Manufacturer_Email__c='Test@merfantz.com',Status ='shipping',EffectiveDate=System.today());
insert ord;
List<Order> listOrderToInsert=new List<Order>();
Order qte = new Order();
listOrderToInsert.add(ord);
Test.StartTest();
insert listOrderToInsert;
Test.stopTest();
}
}
Order Trigger Handler:
----------------------------------
Public Class OrderTriggerHandler{
Public Void OnAfterInsert(Map<id, Order> newOrderMap){
AddProducts(newOrderMap);
}
Public Void OnAfterUpdate(Map<id, Order> newOrderMap,Map<id, Order> oldOrderMap){
}
Private Void AddProducts(Map<id, Order> newOrderMap){
List<id> quoteIds=new List<Id>();
for(Order ord:newOrderMap.values()){
quoteIds.add(ord.QuoteId);
}
Map<id, Quote> qteMap=new Map<Id, Quote>([SELECT id,(SELECT id,Quantity,UnitPrice,Capacity__c, Pricebookentryid from QuoteLineItems) FROM Quote where id IN:quoteIds]);
List<OrderItem> listOrLIs=new List<OrderItem>();
OrderItem OrLI;
for(Order ord:newOrderMap.values()){
for(QuoteLineItem qLI: qteMap.get(ord.Quoteid).QuoteLineItems){
OrLI=new OrderItem();
OrLI.PriceBookEntryId=qLI.PriceBookEntryId;
OrLI.Orderid=ord.Id;
OrLI.Capacity__c=qLI.Capacity__c;
OrLI.Quantity=qLI.Quantity;
OrLI.Unitprice=qLI.UnitPrice;
listOrLIs.add(OrLI);
}
}
if(listOrLIs.size()>0) insert listOrLIs;
}
}
Please help me anyone
I have written code for class named as "InvoiceTriggerhandler".this class corresponding test class named as "Invocie Triggerhandlertest".my test class in failing and i got the below error "
Invoice Trigger Handler:
Public Class InvoiceTriggerHandler{
Public Void OnAfterInsert(Map<id, Invoice__c> newInvoiceMap,Map<id, Invoice__c> oldInvoiceMap){
}
Public Void OnAfterUpdate(Map<id, Invoice__c> newInvoiceMap,Map<id, Invoice__c> oldInvoiceMap){
if(RecursiveHandler.runITH_POOnce()) PlaceOrder(newInvoiceMap, oldInvoiceMap);
}
Private Void PlaceOrder(Map<id, Invoice__c> newInvoiceMap, Map<id, Invoice__c> oldInvoiceMap){
List<id> invIdToPlaceOrders=new List<Id>();
List<id> quoteIds=new List<Id>();
for(Invoice__c inv:newInvoiceMap.values()){
if(inv.Generate_order__c== True && inv.Generate_order__c!=oldInvoiceMap.get(inv.Id).Generate_order__c){
invIdToPlaceOrders.add(inv.id);
quoteIds.add(inv.Quote__c);
}
}
Map<id, Quote> qteMap=new Map<Id, Quote>([SELECT BillingCity,BillingCountry,BillingCountryCode,BillingLatitude,BillingLongitude,BillingName,BillingPostalCode,BillingState,BillingStateCode,BillingStreet,ContactId,Contact.FirstName,Contact.LastName,Description,Discount,Email,ExpirationDate,Fax,GrandTotal,Id,LineItemCount,Name,OpportunityId,Phone,Pricebook2Id,QuoteNumber,ShippingCity,ShippingCountry,ShippingCountryCode,ShippingHandling,ShippingLatitude,ShippingLongitude,ShippingName,ShippingPostalCode,ShippingState,ShippingStateCode,ShippingStreet,Status,Subtotal,Tax,TotalPrice,Dead_Line__c FROM Quote where id IN:quoteIds]);
List<Order> listOrderToInsert=new List<Order>();
Order temp;
Invoice__c inv;
Quote qte;
for(id invId:invIdToPlaceOrders){
temp=new Order();
inv=newInvoiceMap.get(invId);
qte=qteMap.get(inv.Quote__c);
temp.QuoteId=inv.Quote__c;
temp.AccountId=inv.AccountID__c;
temp.OpportunityId=inv.Opportunity__c;
temp.billTocontactId=qte.ContactID;
temp.billingStreet=qte.billingStreet;
temp.billingCity=qte.billingCity;
temp.billingState=qte.billingState;
temp.billingCountry=qte.billingCountry;
temp.BillingPostalCode=qte.BillingPostalCode;
temp.ShippingStreet=qte.ShippingStreet;
temp.ShippingCity=qte.ShippingCity;
temp.ShippingState=qte.ShippingState;
temp.ShippingCountry=qte.ShippingCountry;
temp.ShippingPostalCode=qte.ShippingPostalCode;
temp.EffectiveDate=System.today();
temp.Status='Initial Progress';
temp.Invoice__c=invId;
temp.Pricebook2Id=qte.Pricebook2Id;
temp.EndDate=qte.Dead_Line__c;
listOrderToInsert.add(temp);
}
if(listOrderToInsert.size()>0)
insert listOrderToInsert;
}
}
Invoice Triggerhandler Test:
---------------------------------------
@isTest(SeeAllData=true)
Public Class InvoiceTriggerHandlerTest{
@isTest public Static void TestPlaceOrder(){
Account a= new Account(Name='testprimary');
insert a;
Contact NewCon= New Contact ( LastName ='Test contact',Accountid =a.id);
insert NewCon;
Opportunity opp=New Opportunity(Name='Test Opportunity',Accountid=a.id,StageName='Closed Won',CloseDate=System.today());
insert opp;
Quote qt=[SELECT id,Name FROM Quote Limit 1];
Product2 prod=[SELECT id,Category__c from Product2 where Exclude_Pricing_Matrix__c=false AND Category__c='Item Category: A' LIMIT 1];
Id pbId=[SELECT id from PriceBook2 where name='Standard Price Book' LIMIT 1].id;
id PBEId=[SELECT id from PricebookEntry where Product2Id=:prod.Id and Pricebook2Id=:pbId LIMIT 1].Id;
QuoteLineItem qli=new QuoteLineItem (quoteid=qt.id,PricebookEntryId=PBEId,Follow_Pricing_Matrix__c=TRUE,Capacity__c='64 MB',Description='Testing Description',Quantity=30,UnitPrice=100);
insert qli;
Order ord=New Order(Name='Test Opportunity',Quoteid=qt.id, Accountid=a.id,Opportunityid=opp.id,BillToContactid=NewCon.id,ShipToContactid=NewCon.id,Manufacturer_Email__c='Test@merfantz.com',Status ='shipping',EffectiveDate=System.today());
insert ord;
List<Order> listOrderToInsert=new List<Order>();
Order qte = new Order();
listOrderToInsert.add(ord);
Test.StartTest();
insert listOrderToInsert;
Test.stopTest();
}
}
Order Trigger Handler:
----------------------------------
Public Class OrderTriggerHandler{
Public Void OnAfterInsert(Map<id, Order> newOrderMap){
AddProducts(newOrderMap);
}
Public Void OnAfterUpdate(Map<id, Order> newOrderMap,Map<id, Order> oldOrderMap){
}
Private Void AddProducts(Map<id, Order> newOrderMap){
List<id> quoteIds=new List<Id>();
for(Order ord:newOrderMap.values()){
quoteIds.add(ord.QuoteId);
}
Map<id, Quote> qteMap=new Map<Id, Quote>([SELECT id,(SELECT id,Quantity,UnitPrice,Capacity__c, Pricebookentryid from QuoteLineItems) FROM Quote where id IN:quoteIds]);
List<OrderItem> listOrLIs=new List<OrderItem>();
OrderItem OrLI;
for(Order ord:newOrderMap.values()){
for(QuoteLineItem qLI: qteMap.get(ord.Quoteid).QuoteLineItems){
OrLI=new OrderItem();
OrLI.PriceBookEntryId=qLI.PriceBookEntryId;
OrLI.Orderid=ord.Id;
OrLI.Capacity__c=qLI.Capacity__c;
OrLI.Quantity=qLI.Quantity;
OrLI.Unitprice=qLI.UnitPrice;
listOrLIs.add(OrLI);
}
}
if(listOrLIs.size()>0) insert listOrLIs;
}
}
Please help me anyone
Order ord=New Order(Name='Test Opportunity',Quoteid=qt.id, Accountid=a.id,Opportunityid=opp.id,BillToContactid=NewCon.id,ShipToContactid=NewCon.id,Manufacturer_Email__c='Test@merfantz.com',Status ='shipping',EffectiveDate=System.today());
insert ord;
BECAUSE, you are not setting the PriceBook2Id.
--
Abhi