1

I'm using a very basic Apex Invocable Class from UnofficalSf to create SOQL queries from Flow and trying to see if there is any way I can pass one of the flow variables, such as {!vaname} or even better something like {$Record.Name} into this class without having to blow up the Apex Class. So some method of escape characters to use in the SOQL or small change to the Apex that might work?

Right now the Invocable receives the entire statement as a quoted string such as: SELECT Id from Account WHERE Name = {!vaname} instead of SELECT Id from Account WHERE Name = Dell Computers

Apex Class

global with sharing class CreateSobjectInvoc {

    @InvocableMethod(label='Executes a SOQL query' category='Data')
    global static List<OutputParameters> bulkInvoke( List<InputParameters> inputs ) {
        List<OutputParameters> outputs = new List<OutputParameters>();
        for (InputParameters input : inputs) {
            outputs.add(invoke(input));
        }
        return outputs;
    }

    private static OutputParameters invoke(InputParameters input) {
        List<SObject> records = Database.query(input.query);
        // Return output
        OutputParameters output = new OutputParameters();
        output.records = records;
        return output;
    }

    /**
     * Wrapper class for input parameters
     */
    global class InputParameters {
        @InvocableVariable(required=true)
        global String query;
    }

    /**
     * Wrapper class for output parameters
     */
    global class OutputParameters {
        @InvocableVariable
        global List<SObject> records;
    }
}

1 Answer 1

1

The class just receives a string that needs to be a well formed SOQL query. It doesn't know what {!vaname} relates to.

You'll need to create a formula resource that constructs the full SOQL query.

i.e. the formula would be something like:

"SELECT Id from Account WHERE Name = '" & {!vaname} & "'"

You then pass this formula to the invocable.

If you don't have full control over {!vaname}, you can replace the quotes in it with the SUBSTITUTE function.

e.g. your vaname formula could be SUBSTITUTE({!My_Screen_Component}, "'", "\'")

(I'm guessing the account name is coming from some kind of screen component)

3
  • Yes I get that but the practical issue is that when developing the query for this class we don't need to use quotes. So the string is sent from Flow with no quotes and it errors when I do add quotes. So is there an escape method can add for the & {!vaname} & part.
    – ddeve
    Commented Oct 25, 2021 at 0:30
  • Do you mean how do you escape the single quotes? I've updated my answer to include that.
    – Nick C
    Commented Oct 25, 2021 at 23:01
  • So yes I believe the answer was in escaping the single quotes. Though I didn't actually need to add the single quotes in the original query, adding then escaping them did the trick.
    – ddeve
    Commented Oct 26, 2021 at 1:36

You must log in to answer this question.

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