Struggling with building my first test class so I can deploy an Apex trigger. The trigger converts an Opportunity/Opportunity Products into a Sales Order/Order Items.
Receiving the below error message:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId, unknown (versions 3.0 and higher must specify pricebook entry id, others must specify product id): [PricebookEntryId, unknown]
Below is my test class:
public class Create_Order_Test_Class {
@isTest static void testSetup(){
Account testAcct = new Account (Name = 'My Test Account');
insert testAcct;
Opportunity oppt = new Opportunity();
oppt.Name ='New Test Deal';
oppt.AccountID = testAcct.Id;
oppt.StageName = 'Closed Won';
oppt.Amount = 24000;
oppt.CloseDate = System.today();
insert oppt;
Order ord = new Order();
ord.OpportunityId = oppt.Id;
ord.AccountId = oppt.AccountId;
ord.EffectiveDate = oppt.CloseDate;
ord.Status= 'Draft';
ord.Pricebook2Id = oppt.Pricebook2Id;
insert ord;
Product2 p = new Product2();
p.ProductCode = 'attribution_annual';
p.Name = 'Attribution - Annual';
insert p;
String pbs = Test.getStandardPricebookId();
List<PricebookEntry> pbes = new List<PricebookEntry>();
pbes.add(new PricebookEntry(Pricebook2Id = pbs, Product2Id = p.Id, UnitPrice = 24000, IsActive = true, UseStandardPrice = false));
insert pbes;
Test.startTest();
Map<String, String> productsMap = new Map<String, String>();
for (PricebookEntry x : [Select Id, Product2.Name from PricebookEntry where Product2.ProductCode in ('Standard Price Book')]){
productsMap.put(x.Product2.Name, x.Id);
}
update testAcct;
update oppt;
OpportunityLineItem oli = new OpportunityLineItem (OpportunityId = oppt.Id, Quantity = 1, UnitPrice = 24000, PricebookEntryId = productsMap.get('Standard Price Book'));
insert oli;
Map<string, string> items = new Map<string, string>();
for(OpportunityLineItem x: [select id,PricebookEntryId from OpportunityLineItem where OpportunityId =: oppt.Id]){
items.put(x.PricebookEntryId, x.id);
OrderItem oi = new OrderItem(OrderId = items.get(oli.OpportunityId), Quantity = oli.Quantity, PricebookEntryId = oli.PricebookEntryId, UnitPrice = oli.UnitPrice);
update oi;
Test.stopTest();
}
}
}