+ Start a Discussion
Nathaniel Neblett 18Nathaniel Neblett 18 

Help with Test Class for Invocable Apex Action called from Flow

public without sharing class dedupeLeadProductInterest {
public class FlowInputs {
        @InvocableVariable public String varCombinedProductUnMod;
        @InvocableVariable public String LeadId;
    }

    @InvocableMethod
    public static void updateLead (List<FlowInputs> requests) {
    
    
     List<Id> LeadIds = new List<Id>();
        for (FlowInputs request : requests) {
            LeadIds.add(request.LeadId);
        }
        
        
        List<Lead> leads = [SELECT Id FROM Lead WHERE Id IN : LeadIds];

        List<Lead> LeadsToUpdate = new List<Lead>();

        for (FlowInputs request : requests) {
        
             for (Lead MyLead : leads) {
             
                if (MyLead.Id == request.LeadId) {

                String varCombinedProductUnMod = request.varCombinedProductUnMod;
                String LeadId = request.LeadId;
                List<String> duplicateRemoveList = new List<String>(new Set<String>(varCombinedProductUnMod.split(';')));
                String varApexModifiedCombinedProd = String.join(duplicateRemoveList, ';');

                MyLead.Product_Interest__c = varApexModifiedCombinedProd;
                LeadsToUpdate.add(MyLead);

        }
      }
    }
       database.update(LeadsToUpdate); 
    } 
}

 

Looking for some help to write a test class for the above invocable method used in an apex action from flow.  Unsure how to go about writing a test class for flow inputs. Thanks in advanced

Best Answer chosen by Nathaniel Neblett 18
Maharajan CMaharajan C
Hi Nathaniel,

Please use the below test class:
 
@isTest
public class dedupeLeadProductInterestTest {
    @isTest 
    Public static void testupdateLead(){
        
        // Add if there is anyother required fields are in lead creation
        Lead l = new Lead(company = 'Salesforce', LastName='Test SF Lead', Status = 'Open');
        insert l;
        
        dedupeLeadProductInterest.FlowInputs fi = new dedupeLeadProductInterest.FlowInputs();
        fi.LeadId = l.Id;
        // Change the below as per your input
        fi.varCombinedProductUnMod = 'Test';  
        
        Test.startTest();
        	dedupeLeadProductInterest.updateLead(new List<dedupeLeadProductInterest.FlowInputs>{fi});
        Test.stopTest();
    } 
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Nathaniel,

Please use the below test class:
 
@isTest
public class dedupeLeadProductInterestTest {
    @isTest 
    Public static void testupdateLead(){
        
        // Add if there is anyother required fields are in lead creation
        Lead l = new Lead(company = 'Salesforce', LastName='Test SF Lead', Status = 'Open');
        insert l;
        
        dedupeLeadProductInterest.FlowInputs fi = new dedupeLeadProductInterest.FlowInputs();
        fi.LeadId = l.Id;
        // Change the below as per your input
        fi.varCombinedProductUnMod = 'Test';  
        
        Test.startTest();
        	dedupeLeadProductInterest.updateLead(new List<dedupeLeadProductInterest.FlowInputs>{fi});
        Test.stopTest();
    } 
}

Thanks,
Maharajan.C
This was selected as the best answer
Nathaniel Neblett 18Nathaniel Neblett 18

Thanks @Maharajan.C  

I solved it with  the following ( before i saw you post)

i was forgetting to create the record in my test class. All good now. Thanks. 

 

posting my solution too as it might be usefull for others 

@isTest 
public without sharing class dedupeLeadProductInterestTest {

@isTest
    private static void validatededupeLeadProductInterest() {
        
		//declaring Flow Inputs to invocable apex class
		dedupeLeadProductInterest.FlowInputs firstInput = new dedupeLeadProductInterest.FlowInputs();
		
		//creating a test record
		Lead testLead1 = new Lead();
		testLead1.FirstName = 'Dave';
		testLead1.LastName = 'Attenborough';
		testLead1.Company = 'BBC';
		testLead1.Product_Interest__c = 'DAM;MO';	
        insert testLead1;

		//set the data that would be passed from the flow. the available inputs come from the available invocable variables in the class
        firstInput.LeadId = testLead1.Id;
        firstInput.varCombinedProductUnMod = 'DAM;MO;MO;DAM;MM;ME;Classic PM';
        
		// testing the method / function of the class
 		Test.startTest();
        
        dedupeLeadProductInterest.updateLead(
            new List<dedupeLeadProductInterest.FlowInputs>{firstInput});
        
		Test.stopTest();
    }
}