Using Apex to generate a node
You can generate JSON nodes or x-www-form-urlencoded elements using Apex when the body is too complex for the standard options. For generating JSON bodies, refer to the Using Apex to Generate a JSON Node section below. For x-www-form-urlencoded bodies, see the Using Apex to generate a URL Form Encoded node section further down the page.
Using Apex to generate a JSON node
If the request body of the callout (or response body for an inbound call) is too complex to be generated with the regular options, you have the option to use an Apex class to generate part of the body. Simply select “Built using an Apex Class” in Node Type field and enter the class name that will generate that part of the JSON body.
The class needs to be global and extend the d_wh.JSONNodeCallable abstract class.
Below is an example class that can be used to generate a JSON node.
global class TestCallable extends d_wh.JSONNodeCallable { global override Object Generate(List<Id> mainRecordIds, Object cachedData) { // Add code to generate json node } }
The mainRecordIds parameter contains the list of ids of the used record(s). If the callout is a “One Record” type, the list will contain one Id. If no Main Object is selected, the list will be empty.
The cachedData parameter contains the object returned by the CacheDataCallable class, and null if a cache class is not mentioned ( See details about CacheDataCallable below).
In order to generate a JSON body, the returned object can be one of the following types: String, Decimal, Boolean, List<Object>, Map<String, Object>. If list type or map type is returned, the Object inside should also be one of the types: String, Decimal, Boolean, List<Object>, Map<String, Object>.
Using a CacheDataCallable class
If you select at least one node that is generated using Apex, you are provided with the option to run a cache class before generating nodes. In order to avoid hitting the SOQL limits when using apex to generate nodes (for example in lists), you can specify a class to run once, before the JSONNodeCallable classes. The class needs to be global and extend the d_wh.CacheDataCallable abstract class.
Below is an example class that can be used to cache some of the data before building the JSON.
global class TestCallable extends d_wh.CacheDataCallable { global override Object Cache(List<Id> mainRecordIds) { // Add code to query and return data to be cached } }
The mainRecordIds parameter contains the list of ids of the used record(s).
The response can be any type of object. It will be sent back to you as the cachedData parameter in the JSONNodeCallable class you use generating the node.
Using Apex to generate a URL Form Encoded node
If an element of the request body of the callout (or response body for an inbound call) is too complex to be generated with the regular options, you have the option to use an Apex class to generate that element of the body. Simply select “Built using an Apex Class” in Node Type field and enter the class name that will generate that part of the JSON body.
The selected class needs to be global and extend the d_wh.GenerateStringCallable abstract class.
Below is an example class that can be used to generate a JSON node.
global class TestCallable extends d_wh.GenerateStringCallable { global override String Generate(List<Id> mainRecordIds) { // Add code to generate the string to be used as the value of the item // return mainRecordIds[0]; } }
The mainRecordIds parameter contains the list of ids of the used record(s). If the callout is a “One Record” type, the list will contain one Id. If no Main Object is selected, the list will be empty. The returned String will be used as the value of the element in the request or response body.