1

I want to use the processbuilder to do a callout. In this callout I want to add some variables from the current record but get variable does not exist see below:

global class or_service {
 @InvocableVariable
       global String pName;
@InvocableMethod
public static void PutCallout() {
    List<Orbirental_Service__c> os = null;

    Http http = new Http();
    HttpRequest request = new HttpRequest();

    try {
        os = [SELECT Url__c, ApiKey__c FROM Orbirental_Service__c LIMIT 1]; 
    }catch(QueryException ex) {
        os = null;
    }

    request.setEndpoint(os[0].Url__c +'v1/properties');
    request.setMethod('POST');
    request.setHeader('Content-Type', 'application/json');
    request.setHeader('X-ORBIRENTAL-APIKEY', os[0].ApiKey__c);
    // Set the body as a JSON object
    request.setBody('{"type": "HOUSE",'+
                    '"name":'+ pName +
                    ',"agencyUid": "000000-0000-0000-0000-00000000",'+
                    '"baseGuests": 4,'+
                    '"maximumGuests": 6,'+
                    '"baseDailyRate": 220,'+
                    '"city": "Utrecht",'+
                    '"state": "Utrecht",'+
                    '"acceptInstantBook": false,'+
                    '"isActive": true}');
    HttpResponse response = http.send(request);
    // Parse the JSON response
    if (response.getStatusCode() != 200) {
        System.debug('The status code returned was not expected: ' +
                     response.getStatusCode() + ' ' + response.getStatus());
    } else {
        System.debug(response.getBody());
    }
}

}

4
  • pName should be declared static.
    – Raul
    Oct 8 '18 at 12:21
  • @Raul invocablevariable should not be static. please read out InvocableVariable Considerations section here - developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… Oct 8 '18 at 12:23
  • Gotcha Pragati thanks, @Thomas, why not pass a parameter to PutCallout() method from process builder?
    – Raul
    Oct 8 '18 at 12:42
  • @Raul How would i do this and where can I find info about how to achieve this
    – Thomas
    Oct 8 '18 at 12:54
3

Your @InvocableMethod should be a static method taking a single List<String> parameter:

The invocable method must be static and public or global, and its class must be an outer class.

Note that the parameter of the method is required to be

A list of a primitive data type or a list of lists of a primitive data type – the generic Object type is not supported.
A list of an sObject type or a list of lists of an sObject type – the generic sObject type is not supported.
A list of a user-defined type, containing variables of the supported types and with the InvocableVariable annotation. Create a custom global or public Apex class to implement your data type, and make sure your class contains at least one member variable with the invocable variable annotation.

That's why you need a List<String> here. The @InvocableVariable annotation is used on the members of custom classes that encapsulate the parameters of your @InvocableMethod, not on member variables of the class containing your method.

Additionally, you probably don't need the global modifier unless you're expecting to use this class across namespace boundaries.

Hence, you may want to modify your class along these lines to support the inbound List data type:

public class or_service {
    @InvocableMethod
    public static void MyInvocableMethod(List<String> pNames) {
         for (String pName : pNames) {
             PutCallout(pName);
         }
    }

    public static void PutCallout(String pName) {
    }
}
1
  • Thanks for the explanation, just 1 typo in the answer public static void MyInvocableMethod(List<String> pName) should be MyInvocableMethod(List<String> pNames)
    – Thomas
    Oct 8 '18 at 13:38

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.