I have an Apex class that matches Lead records to existing Contact records based on a phone number input. I'm using @InvocableMethod
to include it in a Flow. The Flow works as designed when I insert a single Lead record, but returns the error The number of results does not match the number of interviews that were executed in a single bulk execution request when I attempt to process multiple Lead records (e.g. via Workbench).
I believe this is the same issue described here: Bulkification of Flow with invocable action - error re: number of results - how do I resolve?, but being new to Apex, I am struggling to understand how to get the input/output numbers to match for my use case.
An example of my Apex class is below:
public class matchLeadToContact{
@InvocableMethod(label='Match Lead to Contact' description='Matches incoming Leads to Contact records based on phone number' category='Lead')
public static List<String> getContactRecords(List<methodInputs> leadRecord) {
String leadMobilePhone = leadRecord[0].MobilePhone;
String leadHomePhone = leadRecord[0].Phone;
leadHomePhone = leadHomePhone.replaceAll('^0-9', '');
leadMobilePhone = leadMobilePhone.replaceAll('^0-9', '');
List<String> contactRecord = new List<String>();
List<Contact> contacts = [SELECT Id FROM Contact WHERE Phone =: leadMobilePhone OR Phone =: leadHomePhone LIMIT 1];
for (Contact contact : contacts) {
contactRecord.add(contact.Id);
}
return contactRecord;
}
//input details that come to apex from flow
public class methodInputs{
@InvocableVariable(required=true)
public String MobilePhone;
@InvocableVariable(required=true)
public String Phone;
}}
Might someone be able to explain why I am receiving the error even though my SOQL query is limited to a single result? How do I fix this?
LIMIT 1
from the SOQL query and am trying to add a null value for the Contact output list in the event that no match is found. Can you point me in the right direction?for (Contact contact : contacts) { if (!contacts.isEmpty()) { contact = contacts[0]; } else { contact = 'No Result'; } contactRecord.add(contact); } return contactRecord;