Invocable Method + Variables — Hitting Null Pointer Exception

apexinvocable-methodinvocablevariable

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;

    }

}

Best Answer

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;

 }


Related Topic