0

I'm hitting a null pointer exception on an Invocable Method that I'm working on. I suspect that this is happening in the runClass method when I'm trying to convert my List of user ids ("userIds") to FlowOutput so they can be passed back to the Flow.

In support of this, in the debug logs, looks like the getContactIds and getUserIds methods are being called without issue.

I don't have much experience with Invocable Variables. When I tried refactoring my List to FlowOutput section, I was running into the error

Error Occurred: The number of results does not match the number of interviews that were executed in a single bulk execution request.

Which I understand typically comes up when your flow inputs and outputs are mis-matched.

What needs to be fixed in this code?


public without sharing class AccountToUserIdList {

/*
Invoc method will accept a list of account ids and return a 
list of user ids, whose contacts are associated with the input list's accounts
*/


    @InvocableMethod
    public static List<FlowOutput> runClass(List<FlowInput> inputAccountIds){

        List<FlowOutput> outputList = new List<FlowOutput>();

        List<String> accountIds = new List<String>();
        List<String> contactIds = new List<String>();
        List<String> userIds = new List<String>();



        for(FlowInput input : inputAccountIds){

            system.debug('*** Running through input...');
            

            for(String currentString : input.accountIdsList){
                accountIds.add(currentString);
            }
        }


        contactIds = getContactIds(accountIds);

        userIds = getUserIds(contactIds);


        FlowOutput newOutput = new FlowOutput();

        for(String id : userIds){            

         //   newOutput.userId = id;

         newOutput.userIdsListOutput.add(id);

         

        }

        outputList.add(newOutput);


        return outputList;

    }



    public static List<String> getContactIds(List<String> inputAccountIds){

        List<String> returnList = new List<String>();

        system.debug('*** Searching for Contacts from input Account Ids');

        for(Contact c : [SELECT Id, AccountId FROM Contact WHERE AccountId IN :inputAccountIds]){
            returnList.add(c.Id);
        }

        system.debug('Found these Contacts ' + returnList);

        return returnList;

    }


    public static List<String> getUserIds(List<String> inputContactIds){

        List<String> returnList = new List<String>();

        system.debug('*** Searching for Users from input Contact Ids');


        for(User u : [SELECT Id, ContactId FROM User WHERE ContactId IN :inputContactIds]){
            returnList.add(u.Id);
        }

        system.debug('Found these Users ' + returnList);


        return returnList;


    }


    public class FlowInput{

        @InvocableVariable
        public String accountId;


        @InvocableVariable
        public List<String> accountIdsList;

    }


    public class FlowOutput{

        @InvocableVariable
        public String userId;


        @InvocableVariable
        public List<String> userIdsListOutput;

    }

}
1
  • Your FlowInput looks suspect -- does the flow interview pass a single accountId or a collection of accountIds? Remember, the invocable is called once as a bulk operation and gets all the inputs from all the flow interviews in the transaction and returns a list of the same arity as the input; SFDC takes care of distributing the results to each interview
    – cropredy
    Commented Oct 12, 2021 at 23:12

2 Answers 2

1

You are adding adding to null because it is not initialized yet

Thanks for Asking
What you are doing you are adding Id in null which is not initialized yet.so before adding id to List you have initialized it Like below.
newOutput.userIdsListOutput = new List<string>{id};
Also i don't think you need wrapper to return user Id's you can also return List which contain user Id's you can take reference from code which i am putting below.

 public static List<String> runClass(List<FlowInput> inputAccountIds){

     List<FlowOutput> outputList = new List<FlowOutput>();

     List<String> accountIds = new List<String>();
     List<String> contactIds = new List<String>();
     List<String> userIds = new List<String>();



     for(FlowInput input : inputAccountIds){

         system.debug('*** Running through input...');
         

         for(String currentString : input.accountIdsList){
             accountIds.add(currentString);
         }
     }


     contactIds = getContactIds(accountIds);

     userIds = getUserIds(contactIds);


     FlowOutput newOutput = new FlowOutput();
     List<String> userIdsList = new List<String>();
     for(String id : userIds){            
      userIdsList.add(id);

     }
     //newOutput.userIdsListOutput = userIdsList;
     
     return userIdsList;

 }


1
  • Great catch! I updated the code slightly to initialize input.userIdsListOutput as a List, and now it's working as intended. My only pushback on Rajat's solution is that using the above implementation, I hit the error -- "The number of results does not match the number of interviews that were executed in a single bulk execution request. " From my understanding, this is because the class treats FlowInput as 1 entry, and the userIdsList List<String> output as many. More here developer.salesforce.com/forums/?id=906F0000000g0PyIAI
    – JH-105539
    Commented Oct 13, 2021 at 22:08
1

In your code, you're receiving a List of FlowInput objects, and while you're technically returning a list of FlowOutput objects, the outputList will always have a single FlowOutput object in it.

i.e. you're getting all userIds for all accounts referenced in all FlowInputs, and putting them into a single FlowOutput.

You need to update your code to have one FlowOutput for every FlowInput.

I'd suggest solving this problem first, then look at how you'll do it in a bulkified manner. To me, it looks like this problem has arisen because you're conflating your invocable requirements with your bulkification requirements.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .