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
karol.freebergkarol.freeberg 

Help coding apex test

I am totally new at coding apex. I managed to write the code but I have a lot of unanswered questions about how to write the test. Below is the code I need to write the test for and below that is what I have written so far for the test. What is happening in the code is I have a Dedicated Stock (DS) parent object (It contains 8 product manager fields used for an approval process). The DS parent object has a relationship to the SR_Products child objects (0-many). The product child records contain one product manager field and it is filled depending on who the product manager is for the product selected. When an insert or update is done to the SR_Products record the trigger runs to fill in the corresponding product manager field on the parent DS object. The trouble I am having with writing the test is (basically I don't know much here): 1) In the test what happens with formula fields? The child object contains formula fields that are key to the test. Don't know how to fill those. 2)Do you have to put test data in for the parent, child, and user objects. 3) Do I use real Id numbers or how do I set that up? What would I do the assert on? Any help to get me going is appreciated.

trigger SR_Set_Approvers on SR_Products__c (After insert, After Update) {

    // This trigger sets all the product managers from the Stocking Request records
    // With the data from the SR - Product Details records
   
    // Loop all product records that have changed
    for (SR_Products__c prod : Trigger.new) {
       
     system.debug('product name1 = ' + prod.Stocking_Request__c);
        // Get all the product records related to the SR
        SR_Products__c[] allproducts = [SELECT product_manager__c, PM_Type__C,
                                        stocking_request__c
             FROM SR_Products__c
             WHERE stocking_request__c = :Prod.stocking_request__c 
             ];
        String Pm1 = '';
        String Pm2 = '';
        String Pm3 = '';
        String Pm4 = '';
        String Pm5 = '';
        String Pm6 = '';
        String Pm7 = '';
        String Pm8 = '';
   //Set the PM variables with the PM from the SR-Products records       
     for(SR_Products__c p : allproducts) {
       
   If (p.PM_Type__c == 'PM1')
                pm1 = p.product_manager__c;
            If (p.PM_Type__c == 'PM2')
                pm2 = p.product_manager__c;
            If (p.PM_Type__c == 'PM3')
                pm3 = p.product_manager__c;
            If (p.PM_Type__c == 'PM4')
                pm4 = p.product_manager__c;
            If (p.PM_Type__c == 'PM5')
                pm5 = p.product_manager__c;
            If (p.PM_Type__c == 'PM6')
                pm6 = p.product_manager__c;
            If (p.PM_Type__c == 'PM7')
                pm7 = p.product_manager__c;
            If (p.PM_Type__c == 'PM8')
                pm8 = p.product_manager__c;                   
        } //end loop products
       
        // Get the Stocking Request record
        system.debug('product name = ' + prod.Stocking_Request__c);
        Stocking_Request__c sr = [Select ID, Name,
                               Product_Manager_1__c, Product_Manager_2__c,
                               Product_Manager_3__c, Product_Manager_4__c,
                               Product_Manager_5__c, Product_Manager_6__c,
                               Product_Manager_7__c, Product_Manager_8__c
                     From Stocking_Request__c
                     Where ID = :prod.Stocking_Request__c];
       
      // Reset all PM fields to blank to start out with
      sr.Product_Manager_1__c = null;
        sr.Product_Manager_2__c = null;
        sr.Product_Manager_3__c = null;
        sr.Product_Manager_4__c = null;
        sr.Product_Manager_5__c = null;
        sr.Product_Manager_6__c = null;
        sr.Product_Manager_7__c = null;
        sr.Product_Manager_8__c = null;
        // Get the user record IDs for the PM variable fields
        // And set the PM fields in the stocking request record
       system.debug('pm1 ID using =  ' + pm1 + pm2);
        If (PM1 != '' ){
         User u1 = [Select ID, Name
                     From User
                     Where Name = :PM1];           
            If (u1 != null)sr.Product_Manager_1__c = u1.ID;
        } //End PM1 if
       
  system.debug('pm2 ID using =  ' + pm2);
        If (PM2 != '' ){
         User u2 = [Select ID, Name
                     From User
                     Where Name = :PM2];
            If (u2 != null)sr.Product_Manager_2__c = u2.id;
        } //End PM2 if       
  system.debug('pm3 ID using =  ' + pm3);
        If (PM3 != '' ){
         User u3 = [Select ID, Name
                     From User
                     Where Name = :PM3];
            If (u3 != null)sr.Product_Manager_3__c = u3.id;
        } //End PM3 if
        system.debug('pm4 ID using =  ' + pm4);
        If (PM4 != ''){
         User u4 = [Select ID, Name
                     From User
                     Where Name = :PM4];
            If (u4 != null)sr.Product_Manager_4__c = u4.id;
        } //End PM4 if       
        system.debug('pm5 ID using =  ' + pm5);
        If (PM5 != '' ){
         User u5 = [Select ID, Name
                     From User
                     Where Name = :PM5];
            If (u5 != null)sr.Product_Manager_5__c = u5.id;
        } //End PM5 if       
       
        If (PM6 != ''){
         User u6 = [Select ID, Name
                     From User
                     Where Name = :PM6];
            If (u6 != null)sr.Product_Manager_6__c = u6.id;
        } //End PM6 if       
 
        If (PM7 != '' ){
         User u7 = [Select ID, Name
                     From User
                     Where Name = :PM7];
            If (u7 != null)sr.Product_Manager_7__c = u7.id;
        } //End PM7 if       

        If (PM8 != '' ){
         User u8 = [Select ID, Name
                     From User
                     Where Name = :PM8];
            If (u8 != null)sr.Product_Manager_8__c = u8.id;
        } //End PM8 if       
       Update sr;
      
}  // End for looping of changed product records
}


My test so far:

@IsTest

Private class TestSRProducts {
   
    static TestMethod void AddProduct() {
        //Setup new Stocking Request
        Stocking_Request__c AR = new Stocking_Request__c();
        AR.Sold_To__c = 'ABC Company';
        AR.Business_Case__c = 'Bus';
       
       
        //Setup new product
        SR_Products__c testAddP = new SR_Products__c();
        testAddp.Stocking_Request__c = 'ABC Company';
        testAddp.Product__c = '01td00000035nAg';
        testAddp.Form__c = 'Web';
        testAddp.Certification__c = 'FSC';
        testAddp.Pounds__c = 10;
        testAddp.Selling_Price__c = 45.00;
        testAddp.New_or_Existing__c = 'New';
        testAddp.width__c = 12;
        testAddp.Diameter__c = 12;
        testAddp.length__c = 24;
        testaddp.Core_Size__c = 24;
        testaddp.Grain__C = 'Long';
        testaddp.Pounds_Per__c = 'Month';
        Test.startTest();
        insert testaddp;
        Test.stoptest();
       System.assertnotEquals(testaddp.id, null);
      
    } //End AddProduct
       
       
       
} //End Class TestSRProducts
Best Answer chosen by karol.freeberg
Stephan SpiegelStephan Spiegel
  1. Formula fields get calculated just the same as they would in a production environment: if you set the fields on the objects that they are calculated from, they will be calculated in your test as well.
  2. You'll have to create test data for the parent and child objects. User records are visible to test methods, so you could use real users that exist in your database. Ideally though you wouldn't hard-code IDs in your test method, but do a query for any  user accounts you need (for example [Select ID from User where ProfileId in (Select Id from Profile where Name = 'Product Manager') Limit 8] - then you can assign the User IDs returned in the query to the Product Manager fields on your test SR_Products).
  3. You'll want to do asserts on fields that you want to make sure where set correctly by your trigger, e.g. that the Product Manager fields on the Stocking Requests got set the way you would expect them to.
If you run your test method with more than a few SR_Products, you'll notice that you'll start to hit governor limits: your trigger has SOQL queries and DML operations inside of loops, which is not a best practice. Let me know if you'd like help bulkifying your code.

All Answers

Stephan SpiegelStephan Spiegel
  1. Formula fields get calculated just the same as they would in a production environment: if you set the fields on the objects that they are calculated from, they will be calculated in your test as well.
  2. You'll have to create test data for the parent and child objects. User records are visible to test methods, so you could use real users that exist in your database. Ideally though you wouldn't hard-code IDs in your test method, but do a query for any  user accounts you need (for example [Select ID from User where ProfileId in (Select Id from Profile where Name = 'Product Manager') Limit 8] - then you can assign the User IDs returned in the query to the Product Manager fields on your test SR_Products).
  3. You'll want to do asserts on fields that you want to make sure where set correctly by your trigger, e.g. that the Product Manager fields on the Stocking Requests got set the way you would expect them to.
If you run your test method with more than a few SR_Products, you'll notice that you'll start to hit governor limits: your trigger has SOQL queries and DML operations inside of loops, which is not a best practice. Let me know if you'd like help bulkifying your code.
This was selected as the best answer
karol.freebergkarol.freeberg
I rewrote the trigger, bulkified it, and moved it to production so I am set. Thank you for your input.