[SalesForce] Can’t invoke @InvocableMethod via the REST API

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.

Best Answer

... 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 :)

Related Topic