I have a flow where I'm populating a Lead record variable with some field values

enter image description here

I want to pass this variable to an apex @InvocableMethod, so that apex can upsert the lead, and do some other logic.

My apex method looks like this

@InvocableMethod(label='Upsert Lead')
    public static List<String> upsertLead(List<Lead> leads ){

Then in the flow, I assign the newLead variable to the upsertLead invocable method

enter image description here

However, when I run the flow, I receive the following error:

Apex_Upsert_Lead Inputs: leads = {!newLead} (Lead (No ID)) Error Occurred: An Apex error occurred: System.UnexpectedException: Illegal arguments

As per the docs here https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm I should be able to pass a specific-sobject type to the invocable method.

Also, note that the argument on the method is a list, but the variable in the flow is a single record lead variable. This is expected (I believe), simply because the Invocable interface is bulkified.

If the data types were not compatible the flow wouldn't let me assign that variable to the input of the method.

Thoughts on what I'm doing wrong?


1 Answer 1


So I somehow figured this out and I'm 100% sure it's a salesforce bug. Looking at the debug logs, my apex method was actually being called just fine, and the error was happening much later in the code.

The specific error was on a line as follows:


turns out, every time I referenced custom_picklist__c in the code, no matter how I was using it (even on debug statements), I'd get the illegal argument exception.

The field was populated via the flow, using a text default value, which IS an available value in the picklist.

I can only assume that there's a salesforce bug where picklist field references populated via a default text value in a flow, are somehow evaluated differently in apex (behind the scenes) and thus throws an error.

What I did is I simply didn't "touch" or "read" that field value in the code, and now it's all working as expected.

  • 1
    Ran into similar issue. Turns out selected picklist value returned by a flow is not a string but some flow wrapper class around it. So in my case it helped just to wrap all picklist fields on sobject into String.valueOf(record.picklist__c) before actually calling any string specific methods on values.
    – wesaw
    Sep 8, 2020 at 15:50

You must log in to answer this question.

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