4

When I am trying to pass records prepared into an Invocable Apex method from a flow that have had picklist values set on the record I receive the following error message.

An Apex error occurred: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_TYPE_ON_FIELD_IN_RECORD, Picklist Field: value not of required type: Value Set From Flow: Picklist Field

Then I came across the following known Salesforce issue which states there is no workaround - https://success.salesforce.com/issues_view?id=a1p3A0000003fH1QAI

In an effort to find a workaround I first tried casting the field as a String but got the following error.

An Apex error occurred: System.TypeException: Invalid conversion from runtime type java:moduleapi.interaction.FlowPicklistValue to String

I then tried JSON serialising and deserialising but then got the following error on deserialising (even untyped)

An Apex error occurred: System.JSONException: Type unsupported in JSON: moduleapi.interaction.FlowPicklistValue

Seems like there's a special internal type for Flow picklists that doesn't behave well in Apex. Was wondering if there were any ideas to solve this? Or accept that there is no workaround.

1 Answer 1

8

Turns out using String.valueOf() actually does the trick in being able to convert it to a String that Apex accepts. Wrote a little utility to help manage this until it gets fixed.

public inherited sharing class FlowUtilities {

    private Map<String, Schema.SObjectField> sObjectFieldsMap;

    public FlowUtilities (String sObjectType) {
        this.sObjectFieldsMap = Schema.describeSObjects(new List<String> { sObjectType })[0].fields.getMap();
    }

    public List<SObject> cleanPickListValues(List<SObject> records) {
        for (SObject record : records) {
            cleanPickListValues(record);
        }
        return records;
    }

    public SObject cleanPickListValues(SObject record) {
        for (String fieldName : record.getPopulatedFieldsAsMap().keySet()) {
            if (sObjectFieldsMap.get(fieldName).getDescribe().getType() == Schema.DisplayType.PICKLIST) {
                record.put(fieldName, String.valueOf(record.get(fieldName)));
            }
        }
        return record;
    }
}
1
  • +1 to this, worked like a charm just plug and play. Ran into this "fun" issue today.
    – tsalb
    Commented Jul 11, 2020 at 0:25

You must log in to answer this question.

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