You need to sign in to do that
Don't have an account?
SARANG DESHPANDE
I am getting above error while validating challenge for Advanced Apex specialist. Following is my code, which I think is working perfectly well. I can't think of a reson why this error is being thrown. Any help is appreciated.:
Salesforce throwing unfathomable error after check challenge
I am getting above error while validating challenge for Advanced Apex specialist. Following is my code, which I think is working perfectly well. I can't think of a reson why this error is being thrown. Any help is appreciated.:
/** * @name TestDataFactory * @description Contains methods to construct and/or validate commonly used records **/ public with sharing class TestDataFactory { /** * @name ConstructCollaborationGroup * @description **/ public static CollaborationGroup ConstructCollaborationGroup(){ //ToDo: Ensure this method returns a single Chatter CollaborationGroup // whose Name starts with 'TEST' followed by the INVENTORY_ANNOUNCEMENTS constant // and configured so anyone can join, see and post updates. CollaborationGroup cGroup = new CollaborationGroup(name = 'TEST' + Constants.INVENTORY_ANNOUNCEMENTS, CollaborationType = 'Public', IsAutoArchiveDisabled = true); insert cGroup; //Database.SaveResult sr = Database.insert(cGroup); return cGroup; } /** * @name CreateProducts * @description Constructs a list of Product2 records for unit tests **/ public static List<Product2> ConstructProducts(Integer cnt){ //ToDo: Ensure this method returns a list, of size cnt, of uniquely named Product2 records // with all the required fields populated // and IsActive = true // an Initial Inventory set to 10 // and iterating through the product family picklist values throughout the list. List<Schema.PicklistEntry> picklistValues = Constants.PRODUCT_FAMILY; List<Product2> productTestDataList = new List<Product2>(); for(Integer i = 0 ; i < cnt ; i++){ Product2 prod = new Product2(name = 'Product' + i, IsActive = true, Initial_Inventory__c = 10, Family = picklistValues[i].getValue()); productTestDataList.add(prod); } insert productTestDataList; return productTestDataList; } /** * @name CreatePricebookEntries * @description Constructs a list of PricebookEntry records for unit tests **/ public static List<PricebookEntry> ConstructPricebookEntries(List<Product2> prods){ //ToDo: Ensure this method returns a corresponding list of PricebookEntries records // related to the provided Products // with all the required fields populated // and IsActive = true // and belonging to the standard Pricebook List<PricebookEntry> pBookEntryList = new List<PricebookEntry>(); for(Product2 prod : prods){ PricebookEntry pEntry = new PricebookEntry(UseStandardPrice = true, Product2Id = prod.Id, IsActive = true, UnitPrice = 25, Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID); pBookEntryList.add(pEntry); } insert pBookEntryList; return pBookEntryList; } /** * @name CreateAccounts * @description Constructs a list of Account records for unit tests **/ public static List<Account> ConstructAccounts(Integer cnt){ //ToDo: Ensure this method returns a list of size cnt of uniquely named Account records // with all of the required fields populated. List<Account> accList = new List<Account>(); for(Integer i = 0 ; i< cnt ; i++){ Account acc = new Account(name = 'Account' + i); accList.add(acc); } insert accList; return accList; } /** * @name CreateContacts * @description Constructs a list of Contacxt records for unit tests **/ public static List<Contact> ConstructContacts(Integer cnt, List<Account> accts){ //ToDo: Ensure this method returns a list, of size cnt, of uniquely named Contact records // related to the provided Accounts // with all of the required fields populated. List<Contact> contList = new List<Contact>(); for(Integer i = 0 ; i < cnt ; i++){ Contact cont = new Contact(lastname = 'Contact' + i, AccountId = accts[i].Id); contList.add(cont); } insert contList; return contList; } /** * @name CreateOrders * @description Constructs a list of Order records for unit tests **/ public static List<Order> ConstructOrders(Integer cnt, List<Account> accts){ //ToDo: Ensure this method returns a list of size cnt of uniquely named Order records // related to the provided Accounts // with all of the required fields populated. List<Order> contList = new List<Order>(); for(Integer i = 0 ; i < cnt ; i++){ Order cont = new Order( name = 'order ' + i, AccountId = accts[i].Id, EffectiveDate = Date.today()); contList.add(cont); } insert contList; return contList; } /** * @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){ //ToDo: Ensure this method returns a list of size cnt of OrderItem records // related to the provided Pricebook Entries // and related to the provided Orders // with all of the required fields populated. // Hint: Use the DEFAULT_ROWS constant for Quantity as it will be used in the next challenge List<OrderItem> oItemList = new List<OrderItem>(); for(Integer i = 0 ; i < cnt ; i++){ OrderItem oItem = new OrderItem(UnitPrice = 100.00, Quantity = Constants.DEFAULT_ROWS, OrderId = ords[i].Id, PricebookEntryId = pbes[i].Id); oItemList.add(oItem); } insert oItemList; return oItemList; } /** * @name SetupTestData * @description Inserts accounts, contacts, Products, PricebookEntries, Orders, and OrderItems. **/ public static void InsertTestData(Integer cnt){ //ToDo: Ensure this method calls each of the construct methods // and inserts the results for use as test data. List<Product2> productsList = ConstructProducts(cnt); List<PricebookEntry> pBookList = ConstructPricebookEntries(productsList); List<Account> actList = ConstructAccounts(cnt); ConstructContacts(cnt, actList); List<Order> orderList = ConstructOrders(cnt, actList); ConstructOrderItems(cnt, pBookList, orderList); } }
https://raw.githubusercontent.com/amitjpr/Superbadge-AdvancedApexSpecialist/master/src/classes/TestDataFactory.cls
https://github.com/amitjpr/Superbadge-AdvancedApexSpecialist/blob/master/src/classes/TestDataFactory.cls
Thanks for replying. After watching your code I see only difference is that you have inserted all records in InsertTestData by returning the list to this method method and I have inserted records in respective methods and then returned it. Can you please explain what difference does this make effectively?
And I did not remove the with sharing clause as well :)