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 

Code coverage help please.

I am really new at apex code and worse at trying to do the test coverage. My code is only at 65% and I'm at a loss at what else do to. Can anyone help please.

Here is my current test code and my trigger is below that.
The Test Code: Coverage only at 65%
@IsTest Private class TestSRProducts {
   
    testMethod private static void getthedata() {
        // Create the user
       // User testUser = NewObjFactory.buildTestUser(0, 'Pipo', 'Tanya');
       // insert testuser;
        String PM7 = 'Tanya Pipo';
        User testuser = [Select ID, Name
                     From User
                     Where Name = :PM7];
        system.debug('User ' +testuser.id);
      
      
   
        // Create the Account
        Account testacct = NewObjFactory.buildTestAcct(0, 'User - IT', testuser.id);
        insert testacct;
       
       
        // Create the Contact
        Contact testcontact = NewObjFactory.buildTestContact(0, testacct.id, testuser.id);
        insert testcontact;
      
       
        // Create a product
        Product2 testprod = NewObjFactory.buildTestProducts(0);
        insert testprod;
        Product2 testprod2 = NewObjFactory.buildTestProd2(0);
        insert testprod2;
      
       
        // Create a Stocking Request
        Stocking_Request__c testSR = NewObjFactory.buildTestStockingReq(0, testacct.id, testuser.id, testcontact.id);
        insert testSr;
        system.debug('SR ' +testsr.id);
       
        // Create a SR Product and envokes the trigger
        SR_Products__C testsrprod = NewObjFactory.buildtestSRProducts(0, testSR.id);
        SR_Products__C testsrprod2 = NewObjFactory.buildtestSRProducts(0, testSR.id);
        testsrprod2.Product__c = [Select ID from Product2 where name = :'CLS2PAP SHEET SECONDS GLOSS'].id;
        Test.startTest();
        insert testsrprod;
        insert testsrprod2;
        Test.stopTest();
       
        Stocking_Request__c SR1 = [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 = :testsr.id];           
            If (SR1 != null){
       
         system.debug('product manager is '+SR1.name);
         system.debug('product manager7 is '+ SR1.product_manager_7__c);
          }
        System.assertEquals (SR1.product_manager_7__c, testuser.id);
        System.assertNotEquals (SR1.product_manager_6__c, testuser.id);
        System.assertNotEquals (SR1.product_manager_5__c, testuser.id);
        System.assertNotEquals (SR1.product_manager_4__c, testuser.id);
        System.assertNOtEquals (SR1.product_manager_3__c, testuser.id);
        System.assertNotEquals (SR1.product_manager_2__c, testuser.id);
        System.assertNotEquals (SR1.product_manager_1__c, testuser.id);
       
   
    }    //End getthedata
       
       
} //End Class TestSRProducts
The trigger:

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
     
        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
       
      
        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       
      
        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
       
        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       
      
        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
}
Best Answer chosen by karol.freeberg
Boom B OpFocusBoom B OpFocus
Hi Karol, I just got a chance to look at your trigger in more detail and I found a lot of issues in there. I saw that you try to get all products at line 11 but that is not a very good practice to have a SOQL inside a loop. There are also more SOQL inside a loop from line 67 - line 123. At the end you have DML update sr. All these are going to cause your trigger to run over the governor limits. You should change the Product_Manager__c in SR_Products__c to a lookup field instead a text, so you don't have to run a SOQL to get the user id. I've modified your trigger as following (with Product_Manager__c = lookup): 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 set stockingRequestIds = new Set(); // I assume this is a lookup field in your org // Loop all product records that have changed for (SR_Products__c prod : Trigger.new) { stockingRequestIds.add(prod.Stocking_Request__c); } Map mapSrs = new Map([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 in :stockingRequestIds]); //Set the PM variables with the PM from the SR-Products records for(SR_Products__c p : [SELECT product_manager__c, PM_Type__C, stocking_request__c FROM SR_Products__c WHERE stocking_request__c in :stockingRequestIds ]) { if (mapSRs.containsKey(p.stocking_request__c)) { stocking_request__c sr = mapSRs.get(p.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; if (p.PM_Type__c == 'PM1') sr.Product_Manager_1__c = p.product_manager__c; if (p.PM_Type__c == 'PM2') sr.Product_Manager_2__c = p.product_manager__c; if (p.PM_Type__c == 'PM3') sr.Product_Manager_3__c = p.product_manager__c; if (p.PM_Type__c == 'PM4') sr.Product_Manager_4__c = p.product_manager__c; if (p.PM_Type__c == 'PM5') sr.Product_Manager_5__c = p.product_manager__c; if (p.PM_Type__c == 'PM6') sr.Product_Manager_6__c = p.product_manager__c; if (p.PM_Type__c == 'PM7') sr.Product_Manager_7__c = p.product_manager__c; if (p.PM_Type__c == 'PM8') sr.Product_Manager_8__c = p.product_manager__c; } } //end loop products Update mapSrs.values(); } Hope this helps. *Optimizing Business Operations* *Boom Bertetti* Application Development Manager t: +1 781 262-3400 x3417 | m: +1 571 438-5009 Get stronger sales insights with our new App for Salesforce, Sales Activity Tracker .* Try it free >> *

All Answers

Boom B OpFocusBoom B OpFocus
try adding a test SR_Products__c for each PM_Type__c.

For example
SR_Products__c sr1 = new SR_Products__c(Name='test sr1', PM_Type__c='PM1');
SR_Products__c sr2 = new SR_Products__c(Name='test sr2', PM_Type__c='PM2);
.
.
.
(do this until PM8);

insert new List<SR_Products__c>{sr1, sr2, ...., sr8};
karol.freebergkarol.freeberg
That did not do it. Any other suggestions? (Note: PM_type__C is a formula field.)
Boom B OpFocusBoom B OpFocus
Hi Karol, I just got a chance to look at your trigger in more detail and I found a lot of issues in there. I saw that you try to get all products at line 11 but that is not a very good practice to have a SOQL inside a loop. There are also more SOQL inside a loop from line 67 - line 123. At the end you have DML update sr. All these are going to cause your trigger to run over the governor limits. You should change the Product_Manager__c in SR_Products__c to a lookup field instead a text, so you don't have to run a SOQL to get the user id. I've modified your trigger as following (with Product_Manager__c = lookup): 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 set stockingRequestIds = new Set(); // I assume this is a lookup field in your org // Loop all product records that have changed for (SR_Products__c prod : Trigger.new) { stockingRequestIds.add(prod.Stocking_Request__c); } Map mapSrs = new Map([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 in :stockingRequestIds]); //Set the PM variables with the PM from the SR-Products records for(SR_Products__c p : [SELECT product_manager__c, PM_Type__C, stocking_request__c FROM SR_Products__c WHERE stocking_request__c in :stockingRequestIds ]) { if (mapSRs.containsKey(p.stocking_request__c)) { stocking_request__c sr = mapSRs.get(p.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; if (p.PM_Type__c == 'PM1') sr.Product_Manager_1__c = p.product_manager__c; if (p.PM_Type__c == 'PM2') sr.Product_Manager_2__c = p.product_manager__c; if (p.PM_Type__c == 'PM3') sr.Product_Manager_3__c = p.product_manager__c; if (p.PM_Type__c == 'PM4') sr.Product_Manager_4__c = p.product_manager__c; if (p.PM_Type__c == 'PM5') sr.Product_Manager_5__c = p.product_manager__c; if (p.PM_Type__c == 'PM6') sr.Product_Manager_6__c = p.product_manager__c; if (p.PM_Type__c == 'PM7') sr.Product_Manager_7__c = p.product_manager__c; if (p.PM_Type__c == 'PM8') sr.Product_Manager_8__c = p.product_manager__c; } } //end loop products Update mapSrs.values(); } Hope this helps. *Optimizing Business Operations* *Boom Bertetti* Application Development Manager t: +1 781 262-3400 x3417 | m: +1 571 438-5009 Get stronger sales insights with our new App for Salesforce, Sales Activity Tracker .* Try it free >> *
This was selected as the best answer