+ Start a Discussion
SARANG DESHPANDESARANG DESHPANDE 

Salesforce throwing unfathomable error after check challenge

User-added image

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



 
Raj VakatiRaj Vakati
Use this code


https://raw.githubusercontent.com/amitjpr/Superbadge-AdvancedApexSpecialist/master/src/classes/TestDataFactory.cls
https://github.com/amitjpr/Superbadge-AdvancedApexSpecialist/blob/master/src/classes/TestDataFactory.cls
 
/**
 * @name TestDataFactory
 * @description Contains methods to construct and/or validate commonly used records
 **/
public class TestDataFactory {

  /**
   * @name constructCollaborationGroup
   * @description
   **/
  public static CollaborationGroup ConstructCollaborationGroup(){
    CollaborationGroup cgroup = new CollaborationGroup();
    cgroup.Name = 'TEST' + Constants.INVENTORY_ANNOUNCEMENTS;
    cgroup.CanHaveGuests = false;
    cgroup.CollaborationType = 'Public';
    cgroup.IsArchived = false;
    cgroup.IsAutoArchiveDisabled = false;
    return cgroup;
  }

  /**
   * @name CreateProducts
   * @description constructs a List of Product2 records for unit tests
   **/
  public static List<Product2> constructProducts(Integer cnt){
    List<Schema.PickListEntry> familyValueList = Product2.Family.getDescribe().getPickListValues();
    Integer listSize = familyValueList.size();

    List<Product2> productList = new List<Product2>();
    for (Integer i = 0; i < cnt; i++) {
      Product2 p = new Product2();
      p.Name = 'Product ' + i;
      p.Family = familyValueList[Math.mod(i, listSize)].getValue();
      p.Initial_Inventory__c = 10;
      p.IsActive = true;
      productList.add(p);
    }
    return productList;
  }

  /**
   * @name CreatePricebookEntries
   * @description constructs a List of PricebookEntry records for unit tests
   **/
  public static List<PricebookEntry> constructPricebookEntries(List<Product2> productList){
    List<PricebookEntry> pbes = new List<PricebookEntry>();
    for (Product2 product: productList) {
      PricebookEntry pbe = new PricebookEntry();
      pbe.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
      pbe.Product2Id = product.Id;
      pbe.IsActive = true;
      pbe.UnitPrice = 1;
      pbes.add(pbe);
    }
    return pbes;
  }

  /**
   * @name CreateAccounts
   * @description constructs a List of Account records for unit tests
   **/
  public static List<Account> constructAccounts(Integer cnt){
    List<Account> accts = new List<Account>();
    for (Integer i = 0; i < cnt; i++) {
      Account acct = new Account();
      acct.Name = 'Account ' + i;
      accts.add(acct);
    }
    return accts;
  }

  /**
   * @name CreateContacts
   * @description constructs a List of Contacxt records for unit tests
   **/
  public static List<Contact> constructContacts(Integer cnt, List<Account> accts){
    Integer listSize = accts.size();

    List<Contact> contactList = new List<Contact>();
    for (Integer i = 0; i < cnt; i++) {
      Contact c = new Contact();
      c.LastName = 'Contact ' + i;
      c.AccountId = accts[Math.mod(i, listSize)].Id;
      contactList.add(c);
    }

    return contactList;
  }

  /**
   * @name CreateOrders
   * @description constructs a List of Order records for unit tests
   **/
  public static List<Order> constructOrders(Integer cnt, List<Account> accts){
    Integer listSize = accts.size();

    List<Order> orders = new List<Order>();

    for (Integer i = 0; i < cnt; i++) {
      Order o = new Order();
      o.Name = 'Order ' + i;
      o.AccountId = accts[Math.mod(i, listSize)].Id;
      o.EffectiveDate = Date.today();
      o.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
      o.Status = 'Draft';
      orders.add(o);
    }

    return orders;
  }

  /**
   * @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){
    Integer pbeListSize = pbes.size();
    Integer orderListSize = ords.size();

    List<OrderItem> orderItemList = new List<OrderItem>();

    for (Integer i = 0; i < cnt; i++) {
      OrderItem oi = new OrderItem();
      oi.OrderId = ords[Math.mod(i, orderListSize)].Id;
      oi.PriceBookEntryId = pbes[Math.mod(i, pbeListSize)].Id;
      oi.Quantity = Constants.DEFAULT_ROWS;
      oi.UnitPrice = 1;
      orderItemList.add(oi);
    }

    return orderItemList;
  }

  public static void VerifyQuantityOrdered(Product2 originalProduct, Product2 updatedProduct, Integer qtyOrdered){
    Integer sumQuantity = Integer.valueOf(originalProduct.Quantity_Ordered__c) + qtyOrdered;
    System.assertEquals(updatedProduct.Quantity_Ordered__c, sumQuantity);
  }

  /**
   * @name SetupTestData
   * @description Inserts accounts, contacts, Products, PricebookEntries, Orders, and OrderItems.
   **/
  public static void InsertTestData(Integer cnt){
    insert constructCollaborationGroup();

    List<Product2> productList = constructProducts(cnt);
    insert productList;

    List<PricebookEntry> pbes = constructPricebookEntries(productList);
    insert pbes;

    List<Account> accts = constructAccounts(cnt);
    insert accts;
    insert constructContacts(cnt, accts);

    List<Order> ords = constructOrders(cnt, accts);
    insert ords;

    insert constructOrderItems(cnt, pbes, ords);
  }
}

 
SARANG DESHPANDESARANG DESHPANDE

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 :)