0

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;
    }
}
``

1 Answer 1

0

Your going to need to bulkify this.

  1. you have SOQL queries in the for loop
  2. The method your calling does allow a list of records see https://customers.geopointe.com/s/article/Apex-API#assignrecords

I would expect something more like this, you probably need to do something with the result and might need to use the callback class name on the assignRecords call.

global static List<Response> execute(List<Request> requests) {

    List<Response> responses = new List<Response>();
    List<Id> assignListAccounts = new List<Id>();
    List<Id> assignListLeads = new List<Id>();
    for (Request curRequest : requests) {
        Id requestId = curRequest.requestId;

        String objectName = requestId.getSObjectType().getDescribe().getName();

        if (objectName == 'Account') {
            assignListAccounts.add(requestId);
            System.debug('assignList = ' + assignListAccounts);
        } else if (objectName == 'Account') {
            assignListLeads.add(requestId);
            System.debug('assignList = ' + assignListAccounts);
        } else {
            assignResults = 'Invalid Object';
        }
    }
    String assignResults;
    
    if (!assignListAccounts.isEmpty()) {
        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 = 'Account'
        ]) {
            assignPlanList.add(ap.Id);
        }

        assignResults = geopointe.API.assignRecords(assignListAccounts, assignPlanList, null);
        System.debug('Assign Results = ' + assignResults);
        Response curResponse = new Response();
        curResponse.assignResults = assignResults;
        responses.add(curResponse);
    }
    if (!assignListLeads.isEmpty()) {
        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 = 'Lead'
        ]) {
            assignPlanList.add(ap.Id);
        }

        assignResults = geopointe.API.assignRecords(assignListLeads, assignPlanList, null);
        System.debug('Assign Results = ' + assignResults);
        Response curResponse = new Response();
        curResponse.assignResults = assignResults;
        responses.add(curResponse);
    }

    return responses;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.