0

I am calling an apex class from a flow (in flow debugger) where I want to get the current user/starter of the flow. I tried to get the user with apex but get a system nulpointer exception. I also cannot debug the user in my debug logs. What am I missing see:

public class getCancelOptionsCurrentUser {
    @InvocableMethod(label = 'Get cancel options current user' description = 'Get amendment Cancel reasons')
    public static List<List<String>> getCancelOptionsMsPicklist() {
    String CancelOptions;
    List<String> lstCancelOptions = new List<String>();
        Id CurrUserId = UserInfo.getUserId();
         system.debug('CurrUserId:'+ CurrUserId);
        try {
            CancelOptions = [SELECT Id, Reasons_Cancel__c from User where Id = :UserInfo.getUserId()].get(0).Reasons_Cancel__c;
            system.debug('CancelOptions:'+ CancelOptions);
        } Catch(Exception e) {
            System.debug('Error Canceloptions current user: ' + e.getMessage());
        }
        if (String.isNotBlank(CancelOptions)) lstCancelOptions = CancelOptions.split(';');
        return new List<List<String>>{lstCancelOptions};
    }
}

Nulpointer Exception on line:

CancelOptions = [SELECT Id, Reasons_Cancel__c from User where Id = :UserInfo.getUserId()].get(0).Reasons_Cancel__c;
11
  • why not just create a formula field in flow and in the resource use {!$User.Id}
    – User6670
    Commented Apr 6, 2020 at 13:35
  • @User6670 I need to get the "Reasons_Cancel__c" of the user in the apex class so thats why I want to get the current user in apex
    – Thomas
    Commented Apr 6, 2020 at 13:41
  • i guess we can get that field too from formula
    – User6670
    Commented Apr 6, 2020 at 13:49
  • @User6670 I get where you are coming from. Since I want to use this in an managed package I dont want to have dependencies of fields directly in my flow. So thats why I want to get the user from apex.
    – Thomas
    Commented Apr 6, 2020 at 14:02
  • Please edit your post to specify which line is throwing the null pointer exception. Commented Apr 6, 2020 at 14:34

1 Answer 1

0

For anyone who bumps into this issue. It seems that you cannot use :UserInfo.getUserId() directly into soql. First create a variable and assign :UserInfo.getUserId() to the variable then use :variable in soql.

1
  • 1
    CancelOptions = [SELECT Id, Reasons_Cancel__c from User where Id = :UserInfo.getUserId()].get(0).Reasons_Cancel__c; works for me as does CancelOptions = [SELECT Id, Reasons_Cancel__c from User where Id = :UserInfo.getUserId()][0].Reasons_Cancel__c;
    – cropredy
    Commented Apr 7, 2020 at 3:39

You must log in to answer this question.

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