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
Kelsey ShannonKelsey Shannon 

Help with Test Class Coverage for Invocable Method

Hello - I'm struggling to get the proper coverage with my invocable method that I'm using in a visual flow, specifically the lines inside the for loop are being listed as not covered. How would I test those? 

Class:
public class Invocable_eG_ProductSBUSearch
{
    @InvocableMethod(label='Find Valid Approvers' description ='Searchs the database for matches in SEA Approvers')
    //productSBUSearch comes from the flow and contains the eGreensheet ID and Tier selected concatenated. Example = 'a1s2D0000005DQAQA2,Tier II'
    public static List<List<Approver__c>> approverIds (List<String> productSBUSearch){
        
        // One sObject per search eGreensheet record (bulkify)
        List<List<SObject>> wrapsApprovers = new List<List<SObject>>();
        for (String searchTerm : productSBUSearch) {
            wrapsApprovers.add(new List<SObject>());
            }
        
        //Split out productSBUSearch into tier and eG ID
        String[] VarIds = productSBUSearch[0].split(',');
        
        //Pull eGreensheet Product SBU into a list
        List<Workflow_Details__c> eGs = [SELECT Product_SBU__c FROM Workflow_Details__c where Id in :VarIds];
        system.debug('eGs list has ' + eGs.size() + ' values returned');
        system.debug(database.query('select id,Product_SBU__c from workflow_details__c where Id in :VarIds'));
                     
        //Pull all possible approvers
        
        List<Approver__c> possibleApprovers = [SELECT Id, Product_SBU__c, Tier__c, Approver_Name__c, Name FROM Approver__c WHERE Active__c = true and Approver_type__c = 'Product Approver' and Product_SBU__c != null and Tier__c in :VarIds ORDER BY Approver_Name__r.LastName];
        system.debug('List of approvers:' + possibleApprovers);
                
        //Add all approvers who match on Product SBU to the final list
        for (Approver__c approver : possibleApprovers) {
            //Query for matches
            system.debug(approver.Product_SBU__c);
            system.debug(eGs[0].Product_SBU__c);
           if(approver.Product_SBU__c!=null && eGs[0].Product_SBU__c!=null && eGs[0].Product_SBU__c.contains(approver.Product_SBU__c)){
                //Return fields to the flow collection
                 wrapsApprovers[0].add(approver);
                 System.debug(approver);
                }
        }
        return wrapsApprovers; 
 }
}
Test:
@isTest 
private class Invocable_eG_ProductSBUSearchTest {
    static testMethod void approverIdsTest(){
      //Create Users for the SEA Approval Matrix
       Profile pf = [select id from profile where name='eGreensheet Approver'];    
       User u = new User();
           u.LastName = 'Approver Test User';
           u.Username = 'egreensheetApprover@test.com.honeywell';
           u.Email = 'Approver@test.com';
           u.Alias = 'appuser' ;
           u.CommunityNickname= 'approveruser';
           u.TimeZoneSidKey = 'America/Los_Angeles';
           u.LocaleSidKey='en_US';
           u.EmailEncodingKey= 'ISO-8859-1';
           u.ProfileId = pf.Id;
           u.LanguageLocaleKey = 'en_US';
           u.Functional_Role__c='testrole';
           u.SBU_User__c='testsbu';
           u.CBT__c='HTSI';
           u.CBT_Team__c ='testcbtteam';
       insert u; 
        
       User u2 = new User();
           u2.LastName = 'Approver Test User 2';
           u2.Username = 'egreensheetApprover2@test.com.honeywell';
           u2.Email = 'Approver2@test.com';
           u2.Alias = 'appuser2' ;
           u2.CommunityNickname= 'approveruser2';
           u2.TimeZoneSidKey = 'America/Los_Angeles';
           u2.LocaleSidKey='en_US';
           u2.EmailEncodingKey= 'ISO-8859-1';
           u2.ProfileId = pf.Id;
           u2.LanguageLocaleKey = 'en_US';
           u2.Functional_Role__c='testrole';
           u2.SBU_User__c='testsbu';
           u2.CBT__c='HTSI';
           u2.CBT_Team__c ='testcbtteam';
       insert u2; 
        
      //Add approvers to SEA Approval Matrix where Type = Product Approver
      Approver__c a = new Approver__c();
        a.Name = 'Approver Test user';
        a.Approver_Name__c = u.Id;
        a.Approver_type__c = 'Product Approver';
        a.Tier__c = 'Tier II';
        string productSBU = 'Electronic Solutions';
        a.Product_SBU__c = productSBU;
       insert a;
        
      Approver__c a2 = new Approver__c();
        a2.Name = 'Approver Test user 2';
        a2.Approver_Name__c = u2.Id;
        a2.Approver_type__c = 'Product Approver';
        a2.Tier__c = 'Tier II';
        string productSBU2 = 'Electronic Solutions; Engines & Power Systems';
        a.Product_SBU__c = productSBU2;
       insert a2;
        
      //Add eGreensheet record test 1
      Workflow_details__c eg = new Workflow_details__c();
        string productSBUeg = 'Electronic Solutions';
        eg.Product_SBU__c = productSBUeg;
        eg.Sales_Channel__c = 'Americas AM';
        eg.Wokflow_Name__c = 'Test eGreensheet';
       // eg.Customer_Name__c
        eg.Approval_Level_Required__c = 'Tier II';
        eg.Sales_K__c = 0;
        eg.X5_Year_Revenue_K__c = 0;
        eg.Terms_and_Conditions__c = 'Standard';
        eg.Front_End_Loss__c = 0;
        eg.Exposed_Cost__c = 0;
        eg.Documentum__c = 'test.com';
      insert eg;
        
      //Send through eGreensheet record into class
        String productSBUSearchString = eg.Id+','+eg.Approval_Level_Required__c;
     	List<String> productSBUSearch = new List<String>();
		productSBUSearch.add(productSBUSearchString);
        Invocable_eG_ProductSBUSearch.approverIds(productSBUSearch);
        system.debug(productSBUSearch);
        
        
      //Add eGreensheet record test 2
      Workflow_details__c eg2 = new Workflow_details__c();
        string productSBUeg2 = 'Other Aero';
        eg2.Product_SBU__c = productSBUeg2;
        eg2.Sales_Channel__c = 'Americas AM';
        eg2.Wokflow_Name__c = 'Test eGreensheet';
       // eg.Customer_Name__c
        eg2.Approval_Level_Required__c = 'Tier II';
        eg2.Sales_K__c = 0;
        eg2.X5_Year_Revenue_K__c = 0;
        eg2.Terms_and_Conditions__c = 'Standard';
        eg2.Front_End_Loss__c = 0;
        eg2.Exposed_Cost__c = 0;
        eg2.Documentum__c = 'test.com';
      insert eg2;
       
         //Send through invalid eGreensheet record
        String productSBUSearchString2 = eg2.Id+','+eg2.Approval_Level_Required__c;
     	List<String> productSBUSearch2 = new List<String>();
		productSBUSearch2.add(productSBUSearchString2);
        Invocable_eG_ProductSBUSearch.approverIds(productSBUSearch2);
                     
    }                                                               
}

 
Andrew GAndrew G
Hi 
Since the loop requires approvers, have you confirmed that the approvers in the test class are returned by the query
SELECT Id, Product_SBU__c, Tier__c, Approver_Name__c, Name FROM Approver__c WHERE Active__c = true and Approver_type__c = 'Product Approver' and Product_SBU__c != null and Tier__c in :VarIds ORDER BY Approver_Name__r.LastName
I assume the Active__c field is set active by default, as I don't see it being set in the test data.
Also, does your VarIds contain Ids of the test data (as it appears the test data for approvers has set Tier__c  to "Tier II" not an Id).

Regards
Andrew

 
Kelsey ShannonKelsey Shannon
Yes, the active is set active by default and the Id of the test data is Approver__c.Id. Lines 31 and 33 are the only ones that aren't covered
Andrew GAndrew G
Ok, nothing obvious jumps out, but if it's not passing the IF statement at Line 31, cut out elements of the IF test to see which is the issue.

Random thought might be to try .containsIgnoreCase 
Alternate test to try (yes longer code) - but it may help identify the issue.
String approverSBU = approver.Product_SBU__c;
String productSBU = eGs[0].Product_SBU__c;

if(
String.isNotEmpty(approverSBU) && 
String.isNotEmpty(productSBU) && 
productSBU.containsIgnoreCase (approverSBU)
)

//or in a similar vein switch the contains test
approverSBU.contains(productSBU)

Regards
Andrew