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
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();
    }
}
Eder Garcia 4Eder Garcia 4
Hi Team

I need help with the test class for the callable Apex action called from a flow

public with sharing class ECO_DataAccess_CancellationArco {

    //input details that comes to apex from flow
    public class FlowInputs{
    
        @InvocableVariable
        public String naturalpersonid;
        
        @InvocableVariable
        public String originTag;
        
        @InvocableVariable
        public Boolean dataTreatment;
        
    }

    //output returns from apex to flow
    public class Response{
    
        @InvocableVariable
        public Boolean isCanceled;
        
    }

    @InvocableMethod(label = 'Acc_Cancellation_Arco' description = 'Execute endpoint to core to remove an account.')
    public static List<Response> getCancellationArco(List<FlowInputs> flowInputs) {
        
        List<Response> responses = new List<Response>();
        Response res = new Response();
        FlowInputs request = flowInputs[0];

        Map<String, String> valuesPath = new Map<String, String>();
        Map<String, Object> bodyRequest = new Map<String, Object>();
        List<Map<String, Object>> cancellationArcoCheck = new List<Map<String, Object>>();
        
        valuesPath.put(':natural_person_id', request.naturalpersonid);
        if(String.isNotEmpty(request.originTag)){
            bodyRequest.put('originTag', request.originTag);
        }
        bodyRequest.put('dataTreatment', request.dataTreatment);
        System.debug(bodyRequest);
        ECO_DataAccess_Integrations integration = ECO_DataAccess_Integrations.getInstance();
        integration.result = integration.sendRequest('ECO_CancellationArco', JSON.Serialize(bodyRequest), valuesPath);
        if(integration.result.status) {
            if(integration.result.response.getStatusCode() == 200) {
                System.debug(integration.result.response.getBody());
                Map <String, Object> response = (Map <String, Object>) JSON.deserializeUntyped(integration.result.response.getBody());
                Object data = (Object) response.get('data');
                Map<String, Object> results = (Map<String, Object>)data;
                res.isCanceled = (Boolean) results.get('cancellationArco');
            } else {
                res.isCanceled = false;
                system.debug(integration.result.response.getBody());
            }
        } else {
            res.isCanceled = false;
            system.debug(integration.result.errorMessage);
            throw new AuraHandledException('User has an active product');
        }
        responses.add( res );
        return responses;
    }
}