You need to sign in to do that
Don't have an account?
Shrutika Vithalkar 16
Advance Apex Specialist Step 4
I am getting following error. Can someone tell me what am I missing?
Challenge Not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: []
Challenge Not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: []
/** * @name TestDataFactory * @description Contains methods to construct and/or validate commonly used records **/ public with sharing class TestDataFactory { private static final Id STANDARD_PRICEBOOK_ID = (Test.isRunningTest()) ? Test.getStandardPricebookId() : [SELECT id FROM PriceBook2 WHERE isStandard = true LIMIT 1].id; /** * @name ConstructCollaborationGroup * @description **/ public static CollaborationGroup ConstructCollaborationGroup(){ CollaborationGroup collabGroup = new CollaborationGroup(); collabGroup.Name='Test'+Constants.INVENTORY_ANNOUNCEMENTS; collabGroup.CollaborationType='Public'; //can be 'Public' or 'Private' return collabGroup; } /** * @name CreateProducts * @description Constructs a list of Product2 records for unit tests **/ public static List<Product2> ConstructProducts(Integer cnt){ List<Product2> productList=new List<Product2>(); list<Schema.PicklistEntry> pEntries = Constants.PRODUCT_FAMILY; Integer j=0; for(Integer i=0;i<cnt;i++){ Product2 prod=new Product2(name='testProd'+i, IsActive=true, Initial_Inventory__c=10); if(j==4){ j=0; } prod.Family=pEntries.get(j).getValue(); j++; productList.add(prod); } return productList; } /** * @name CreatePricebookEntries * @description Constructs a list of PricebookEntry records for unit tests **/ public static List<PriceBookEntry> ConstructPricebookEntries(List<Product2> prods){ List<PriceBookEntry> priceBokkList=new List<PriceBookEntry>(); system.debug('STANDARD_PRICEBOOK_ID'+STANDARD_PRICEBOOK_ID); for(Product2 prod:prods){ PriceBookEntry pbe=new PriceBookEntry(); pbe.product2Id=prod.id; pbe.Pricebook2Id=STANDARD_PRICEBOOK_ID; pbe.isActive=true; pbe.UseStandardPrice=true; pbe.UnitPrice=10; priceBokkList.add(pbe); } return priceBokkList; } /** * @name CreateAccounts * @description Constructs a list of Account records for unit tests **/ public static List<Account> ConstructAccounts(Integer cnt){ List<Account>acclist=new List<Account>(); for(integer i=0;i<cnt;i++){ Account acc=new Account(name='test'+i); acclist.add(acc); } return acclist; } /** * @name CreateContacts * @description Constructs a list of Contacxt records for unit tests **/ public static List<Contact> ConstructContacts(Integer cnt, List<Account> accts){ List<Contact> contactlist=new List<Contact>(); for(integer i=0;i<cnt;i++){ Contact con=new Contact(lastname='TestContact'+i,AccountId=accts.get(i).id); contactlist.add(con); } return contactlist; } /** * @name CreateOrders * @description Constructs a list of Order records for unit tests **/ public static List<Order> ConstructOrders(Integer cnt, List<Account> accts){ List<Order> orderList =new List<Order>(); for(Integer i=0;i<cnt;i++){ Order orderObj=new Order(); orderObj.Name='order'+i; orderObj.AccountId=accts.get(i).id; orderObj.Status=Constants.DRAFT_ORDER_STATUS; orderObj.EffectiveDate=system.today(); orderList.add(orderObj); } return orderList; } /** * @name CreateOrderItems * @description Constructs a list of OrderItem records for unit tests **/ public static List<OrderItem> ConstructOrderItems(integer cnt, list<pricebookentry> pbes, list<order> ords){ List<OrderItem> orderItems = new List<OrderItem>(); for(Integer i=0;i<cnt;i++){ OrderItem item=new OrderItem(); item.orderId=ords.get(i).id; item.PricebookEntryId=pbes.get(i).id; item.Quantity = Constants.DEFAULT_ROWS; item.UnitPrice = 1; orderItems.add(item); } return orderItems; } /** * @name SetupTestData * @description Inserts accounts, contacts, Products, PricebookEntries, Orders, and OrderItems. **/ public static void InsertTestData(Integer cnt){ list<Account> accounts = ConstructAccounts(cnt); Insert accounts; list<Contact> contacts = ConstructContacts(cnt, accounts); insert contacts; list<Product2> products= ConstructProducts(cnt); insert products; list<PriceBookEntry> entries = ConstructPricebookEntries(products); insert entries; list<Order> orders = ConstructOrders(cnt, accounts); insert orders; list<OrderItem> orderItems = ConstructOrderItems(cnt, entries, orders); insert orderItems; } }
I would suggest you refer below link for similar discussion.
https://developer.salesforce.com/forums/?id=9060G0000005OLEQA2
Best Regards,
Sandhya