function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jiangtao man 8jiangtao man 8 

Advanced Apex Specialist --- Step 4: Create the Test Data Factory

【Advanced Apex Specialist --- Step 4: Create the Test Data Factory】

Hi,everyone I working on 
Advanced Apex Specialist --- Step 4: Create the Test Data Factory today,and I continuously got the Error as below:
------------------------------------------------------------------------------------
Challenge Not yet complete... here's what's wrong:
【Ensure insertTestData uses the other methods in the class to insert Products, Pricebooks, Accounts, Contacts, Orders, and OrderItem.】

------------------------------------------------------------------------------------

but I hava no idea where I was wrong!!? I test my TestDataFactory class in Anonymous Window it is seems like work,and did not get and error,
and certainly got data inserted successfully.

I also use the other guys code to test several times,but got the same Error above,Can someo kindly give me some suggestion??
I really appreciate that,I got stuck here whole days......it is just simply generate some test datas...I could not figure out what is wrong with that。
I post my code down below,thanks!!


/**
 * @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 chatterGroup = new CollaborationGroup(
            Name = 'TEST'+Constants.INVENTORY_ANNOUNCEMENTS,  
            CollaborationType = 'Public'
        );
        return chatterGroup;
    }

    /**
     * @name CreateProducts
     * @description Constructs a list of Product2 records for unit tests
    **/
    public static List<Product2> ConstructProducts(Integer cnt){
        List<Product2> products=new List<Product2>();
        //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.
        for(Integer i=0;i<cnt;i++){
            Product2 eachProduct=new Product2();
            eachProduct.Name='testProduct '+i;
            eachProduct.IsActive=true;
            eachProduct.Initial_Inventory__c=10;
            eachProduct.Family=Constants.PRODUCT_FAMILY.get(math.mod(i,4)).getValue();
            products.add(eachProduct);
        }
        return products;
    }

    /**
     * @name CreatePricebookEntries
     * @description Constructs a list of PricebookEntry records for unit tests
    **/
    public static List<PriceBookEntry> ConstructPricebookEntries(List<Product2> prods){
        List<PriceBookEntry> pbeList = new List<PriceBookEntry>();
        //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
        for(Product2 p:prods){
            PriceBookEntry pbe = new PriceBookEntry();
            pbe.Product2Id = p.Id;
            pbe.IsActive = true;
            pbe.Pricebook2Id=Constants.STANDARD_PRICEBOOK_ID;
            pbe.UnitPrice = 100;
            pbeList.add(pbe);
        }       
        return pbeList;
    }

    /**
     * @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> accounts = new List<Account>();
        for(Integer i = 0 ; i<cnt; i++) {
            Account acc = new Account(name='Account' + i);
            accounts.add(acc);
        }
        System.debug('account size' + accounts.size());
        return accounts;
    }

    /**
     * @name CreateContacts
     * @description Constructs a list of Contact 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> contacts = new List<Contact>();
        for(Integer i=0;i<cnt;i++){
            Contact con = new Contact();
            con.LastName = 'TestContact'+i;
            Integer index = Math.mod(i, accts.size());
            con.AccountId = accts.get(index).Id;
            contacts.add(con);            
        }
        System.debug('contacts size' + contacts.size());
        System.debug('accts size' + accts.size());
        return contacts;
    }

    /**
     * @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> orders = new List<Order>();
        for(Integer i=0;i<cnt;i++){
            Order ord = new Order();
            ord.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
            ord.Status='Draft';
            ord.EffectiveDate = System.today();
            ord.AccountId = accts.get(Math.mod(i, accts.size())).Id;           
            orders.add(ord);
        }                
        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){
        //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> items = new List<OrderItem>();
        for(Integer i=0;i<cnt;i++){
            OrderItem ord = new OrderItem();
            ord.Quantity = Constants.DEFAULT_ROWS;
            ord.UnitPrice = 250;
            ord.OrderId =ords.get(Math.mod(i, ords.size())).Id;
            ord.PricebookEntryId = pbes.get(math.mod(i, pbes.size())).Id;
            items.add(ord);
        }
        
        return items;
    }

    /**
     * @name SetupTestData
     * @description Inserts accounts, contacts, Products, PricebookEntries, Orders, and OrderItems.
    **/
                        
    public static void InsertTestData(Integer cnt){
        //InsertTestData
        //ToDo: Ensure this method calls each of the construct methods
        //  and inserts the results for use as test data.
                
        CollaborationGroup groups = TestDataFactory.ConstructCollaborationGroup();
        insert groups;
        
        List<Product2>  products= TestDataFactory.ConstructProducts(cnt);
        insert products;
        
        List<PriceBookEntry> entries = TestDataFactory.ConstructPricebookEntries(products);
        insert entries;
        
        List<Account> accts = TestDataFactory.ConstructAccounts(cnt);
        insert accts;
        
        List<Contact> contacts = TestDataFactory.ConstructContacts(cnt,accts);
        insert contacts;
        
        List<Order> orders = TestDataFactory.ConstructOrders( cnt,  accts);
        insert orders;
        
        List<OrderItem> items = TestDataFactory.ConstructOrderItems(cnt, entries, orders);
        insert items;
    }
        
        public static void VerifyQuantityOrdered(Product2 originalProduct, Product2 updatedProduct, Integer qtyOrdered) {
        System.assertEquals((updatedProduct.Quantity_Ordered__c - originalProduct.Quantity_Ordered__c), qtyOrdered);
    }

}
jiangtao man 8jiangtao man 8
attach the image
jiangtao man 8jiangtao man 8
Hi,Nagarjuna ,thank you so much for your reply,I Did not expect someone reply me so quickly!!
I will check you codes first and get you feedback,thanks!!
jiangtao man 8jiangtao man 8
@Nagarjuna Parala
hi, Nagarjuna ,I double checked My codes with the answer you offered,I actually have the same code as you offered,it's really the same exactly,and I even copy your code to verify but I still got the same Error 【Ensure insertTestData uses the other methods in the class to insert Products, Pricebooks, Accounts, Contacts, Orders, and OrderItem】,i'm so confused。。
jiangtao man 8jiangtao man 8
@Nagarjuna Parala
Hi,Nagarjuna ,that's really kind of you ,

this is my report:


I was Desperated so I remind that 
【Create a new Trailhead Playground for this Superbadge. If you use an existing org, you might have trouble validating the challenges.】

So I launched a new 【hands-on Trailhead Playground org】 and re-download the 【unmanaged Superbadge package.】

and Redo the Step 1, in Step 2,Step 3,Step 4,I used the resource code your offered to me ,and thanks God , finally Passed the step 4 challenge!!

I think because I used my old  existing org so I encount some trick trouble could not validating the challenges and
Others have definitely not seen it....maybe

anyway the problem was solved right now,I really appreciate that you are such a good person!!a ton of my thanks AGAIN!

maybe next time~~~~

Thanks and Regards

JIANGTAO MAN
Ajaysingh thakurAjaysingh thakur
Issue is line 15, Name should be 'TEST' followed by Contants.INVENTORY_ANNOUNCENTS. Please replace line 15 with below:-

Name = 'TEST'+Constants.INVENTORY_ANNOUNCEMENTS, 
Dipendra DadhichDipendra Dadhich

@ajaysingh Thakur

Please add this variable in Constants Class
public static final String INVENTORY_ANNOUNCEMENTS = 'Inventory Announcements';

ani gho 10ani gho 10
for any one who need to pass the challange Advanced Apex Specialist # 4 Create the Test Data Factory

here goes the code...
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 chatterGroup = new CollaborationGroup(
            Name = 'TEST'+Constants.INVENTORY_ANNOUNCEMENTS,  
            CollaborationType = 'Public'
        );
        return chatterGroup;
    }

    /**
     * @name CreateProducts
     * @description Constructs a list of Product2 records for unit tests
    **/
    public static List<Product2> ConstructProducts(Integer cnt){
        List<Product2> products=new List<Product2>();
        //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.
        for(Integer i=0;i<cnt;i++){
            Product2 eachProduct=new Product2();
            eachProduct.Name='testProduct '+i;
            eachProduct.IsActive=true;
            eachProduct.Initial_Inventory__c=10;
            eachProduct.Family=Constants.PRODUCT_FAMILY.get(math.mod(i,4)).getValue();
            products.add(eachProduct);
        }
        return products;
    }

    /**
     * @name CreatePricebookEntries
     * @description Constructs a list of PricebookEntry records for unit tests
    **/
    public static List<PriceBookEntry> ConstructPricebookEntries(List<Product2> prods){
        List<PriceBookEntry> pbeList = new List<PriceBookEntry>();
        //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
        for(Product2 p:prods){
            PriceBookEntry pbe = new PriceBookEntry();
            pbe.Product2Id = p.Id;
            pbe.IsActive = true;
            pbe.Pricebook2Id=Constants.STANDARD_PRICEBOOK_ID;
            pbe.UnitPrice = 100;
            pbeList.add(pbe);
        }       
        return pbeList;
    }

    /**
     * @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> accounts = new List<Account>();
        for(Integer i = 0 ; i<cnt; i++) {
            Account acc = new Account(name='Account' + i);
            accounts.add(acc);
        }
        System.debug('account size' + accounts.size());
        return accounts;
    }

    /**
     * @name CreateContacts
     * @description Constructs a list of Contact 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> contacts = new List<Contact>();
        for(Integer i=0;i<cnt;i++){
            Contact con = new Contact();
            con.LastName = 'TestContact'+i;
            Integer index = Math.mod(i, accts.size());
            con.AccountId = accts.get(index).Id;
            contacts.add(con);            
        }
        System.debug('contacts size' + contacts.size());
        System.debug('accts size' + accts.size());
        return contacts;
    }

    /**
     * @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> orders = new List<Order>();
        for(Integer i=0;i<cnt;i++){
            Order ord = new Order();
            ord.Pricebook2Id = Constants.STANDARD_PRICEBOOK_ID;
            ord.Status='Draft';
            ord.EffectiveDate = System.today();
            ord.AccountId = accts.get(Math.mod(i, accts.size())).Id;           
            orders.add(ord);
        }                
        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){
        //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> items = new List<OrderItem>();
        for(Integer i=0;i<cnt;i++){
            OrderItem ord = new OrderItem();
            ord.Quantity = Constants.DEFAULT_ROWS;
            ord.UnitPrice = 250;
            ord.OrderId =ords.get(Math.mod(i, ords.size())).Id;
            ord.PricebookEntryId = pbes.get(math.mod(i, pbes.size())).Id;
            items.add(ord);
        }
        
        return items;
    }

    /**
     * @name SetupTestData
     * @description Inserts accounts, contacts, Products, PricebookEntries, Orders, and OrderItems.
    **/
                        
    public static void InsertTestData(Integer cnt){
        //InsertTestData
        //ToDo: Ensure this method calls each of the construct methods
        //  and inserts the results for use as test data.
                
        CollaborationGroup groups = TestDataFactory.ConstructCollaborationGroup();
        insert groups;
        
        List<Product2>  products= TestDataFactory.ConstructProducts(cnt);
        insert products;
        
        List<PriceBookEntry> entries = TestDataFactory.ConstructPricebookEntries(products);
        insert entries;
        
        List<Account> accts = TestDataFactory.ConstructAccounts(cnt);
        insert accts;
        
        List<Contact> contacts = TestDataFactory.ConstructContacts(cnt,accts);
        insert contacts;
        
        List<Order> orders = TestDataFactory.ConstructOrders( cnt,  accts);
        insert orders;
        
        List<OrderItem> items = TestDataFactory.ConstructOrderItems(cnt, entries, orders);
        insert items;
    }
        
        public static void VerifyQuantityOrdered(Product2 originalProduct, Product2 updatedProduct, Integer qtyOrdered) {
        System.assertEquals((updatedProduct.Quantity_Ordered__c - originalProduct.Quantity_Ordered__c), qtyOrdered);
    }

}

 
Sai Kiran 1831Sai Kiran 1831
After pasting the above code, facing this error

There was an unexpected error in your org which is preventing this assessment check from completing: System.LimitException: Too many SOQL queries: 101

Any suggestions on this..
Gabriel Kremer 27Gabriel Kremer 27

Hey developers,

had the same error message as the thread opener. In my case I simply ignored the parameter cnt of type Integer in the method signatur of "InsertTestData". I just had to use the argument to call the other methods where cnt is requested:
 

public static void InsertTestData(Integer cnt){
        // this line as an example
         List<Product2> prods = ConstructProducts(cnt); // passing argument cnt
        // and so on
}

Hope this helps - at least someone :D.