We are using a managed package, Geopointe, to define regions and Assign Leads based on Assignment Plans. The Business is insisting that "warm" Leads (based on the Lead Source) be assigned in realtime.
By default this is not supported by Geopointe, but they do offer an API to run the Assignment Plans. There is a single method exposed in the Api that creates an Apex Job to process the Assignments.
I have created an Apex Class with an Invocable Method to run the Assignment Plans. I have added the Class to an Asynchronous path of a Record Triggered Flow on the Lead. This works fine when Leads are created manually or imported in batches of less than 100. However once the batch size exceeds 100 the Flow produces the error "You've exceeded the limit of 100 jobs in the flex queue for org".
Is it possible to optimize my Apex Class to better handle this?
Would I be better off using this in Trigger instead of a Record Triggered Flow?
global without sharing class Geopointe_AssignPlan_InvocableUpdate {
@InvocableMethod(
label='Run Geopointe Assignment Plan'
description='Run Geopointe Assignment Plan Jobs for Account and Leads'
)
global static List<Response> execute(List<Request> requests) {
List<Response> responses = new List<Response>();
for (Request curRequest: requests ){
Response curResponse = new Response();
String assignResults;
Id requestId = curRequest.requestId;
String objectName = requestId.getSObjectType().getDescribe().getName();
if (objectName == 'Account' || objectName == 'Lead'){
List<Id> assignList = new List<Id>();
assignList.add(requestId);
System.debug('assignList = ' + assignList);
List<Id> assignPlanList = new List<Id>();
for(geopointe__GP_Assignment_Plan__c ap : [
SELECT Id
FROM geopointe__GP_Assignment_Plan__c
WHERE geopointe__Map_Object__c = :objectName
]){
assignPlanList.add(ap.Id);
}
System.debug('assignPlanList = ' + assignPlanList);
assignResults = geopointe.API.assignRecords(assignList, assignPlanList, null);
System.debug('Assign Results = ' + assignResults);
}else{
assignResults = 'Invalid Object';
}
curResponse.assignResults = assignResults;
responses.add(curResponse);
}
return responses;
}
global class InvocableActionException extends Exception{}
global class Request {
@InvocableVariable
(label='Id') global Id requestId;
}
global class Response {
@invocableVariable
global String assignResults;
}
}
``