10

I've been trying to use the new Spring '15 functionality related to Invocable Actions / @InvocableMethod / @InvocableVariable using the current documentation:

https://www.salesforce.com/us/developer/docs/api_rest/

http://releasenotes.docs.salesforce.com/spring15/spring15/release-notes/rn_forcecom_flow_apex.htm

http://releasenotes.docs.salesforce.com/spring15/spring15/release-notes/rn_apex_new_classes_methods.htm#invocablemethod

http://releasenotes.docs.salesforce.com/spring15/spring15/release-notes/rn_api_invocable_actions.htm#rn_api_invocable_actions

I'm wondering how it would be possible to execute the method through the REST API as the documentation says. I am able to get a response when I use the following call through Workbench or Hurl:

https://yourInstance.salesforce.com/services/data/v33.0/actions/custom/apex/InvocableClass

InvocableClass is the apex class that contains my @InvocableMethod.

The fun bit is that everything works ok when the @InvocableMethod is executed through a Process built with Process Builder.

Why?

Thanks for your help in advance!

PS: I have a support case meanwhile with SF as I couldn't find any solution at the moment.

6
  • What response does https://.salesforce.com/services/data/v33.0/actions/custom/apex/InvocableClass give you?
    – Dominic
    Jan 22, 2015 at 18:56
  • { "description" : null, "inputs" : [ { "byteLength" : 0, "description" : null, "label" : "testString1", "maxOccurs" : 1, "name" : "testString1", "picklistValues" : null, "required" : false, "sobjectType" : null, "type" : "STRING" } ], "label" : "InvocableClass", "name" : "InvocableClass", "outputs" : [ ], "standard" : false, "targetEntityName" : null, "type" : "APEX" } ------ Everything seems ok, however the method itself is creating a simple account, however this account is never created when the call is done by REST :(
    – Carolina
    Jan 22, 2015 at 19:33
  • That isn't very helpful is it? It doesn't give the \@InvocableMethod name and it does list inputs, which presumably the \@InvocableMethod has, but the \@InvocableClass itself doesn't.
    – Dominic
    Jan 22, 2015 at 19:37
  • :) Yeap, sadly no so helpful, however everything seems right it recognises that the method has a parameter "testString1" and everything else, however nothing happens. Tried GET and POST no luck in any :(
    – Carolina
    Jan 22, 2015 at 19:45
  • Hi @Carolina - I know you answered this, but, for completeness, could you add the source of the @InvocableMethod when it didn't work?
    – metadaddy
    Feb 3, 2015 at 23:15

1 Answer 1

13

... and here it is the way to do it:

To be able to execute the @InvocableMethod that is in the class, this method needs to have the one permitted parameter. If the method is defined without the parameter then this is not executed, since the REST API doesn't have enough information to know what needs to be executed / invoked.

Therefore this is the way to implement the @InvocableMethod and the corresponding call:

public class InvocableClass {
    @InvocableMethod
    public static List<Account> InsertAccount(List <String> names) {

       List<Account> myAccounts = new List<Account>();
       for (String  accName: names)
       {
         Account acc = new Account(Name = accName);
         myAccounts.add(acc) ;
       }

       insert myAccounts;

       return myAccounts;
    }
}

Through workbench we will do a POST call to: /services/data/v33.0/actions/custom/apex/InvocableClass

In the body we could do something like:

{
 "inputs" : [ {
    "names" : "v1"
    },
    {
    "names" : "v2"
    }]
}

Using the same call, we could change the @InvocableMethod in order to do a query within it.

If instead of executing/invoking the method what we would like to get is the metadata, then we will do a GET call to the same URL and we will get a response like this one:

{
  "description" : null,
  "inputs" : [ {
    "byteLength" : 0,
    "description" : null,
    "label" : "Account",
    "maxOccurs" : 1,
    "name" : "Account",
    "picklistValues" : null,
    "required" : false,
    "sobjectType" : null,
    "type" : "STRING"
  } ],
  "label" : "InvocableClass",
  "name" : "InvocableClass",
  "outputs" : [ {
    "description" : null,
    "label" : "output",
    "maxOccurs" : 1,
    "name" : "output",
    "picklistValues" : null,
    "sobjectType" : "Account",
    "type" : "SOBJECT"
  } ],
  "standard" : false,
  "targetEntityName" : null,
  "type" : "APEX"
}

Note: if the method is defined without parameter it won't be invoked/ executed through REST : public static void InsertAccount() //or public static List < Account > InsertAccount() However through Process Builder any definition is good and the method will be always invoked/executed :)

1
  • 1
    It is perfectly possible to invoke action without parameters through REST. This is how you have to specify the request: { "inputs":[{}] } May 15, 2018 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.