Our existing examples of using API calls in groovy focus on SOAP style web services, therefore this article provides a basic example of working with the recently added support for REST. This illustrative example queries internal custom object records. The basic features of groovy REST support is well covered by the Application Composer documentation, and we will articles with examples of creating, updating, and deleting records.
Defining web services is similar for both SOAP and REST, done using the Common Setup - Web Services feature in Application Composer. Before this a decision needs to be made in the way in which you can pass a criteria to your RESTful service.
The first option is by defining the URL Resource parameters inside the Web Service definition. For example the following definition which signifies that we will pass the CustomerName parameter in the adf.webservices.myWSName(getParams) GET call script. As you can see below the Resource URL definition includes the returned fields list and a limit on rows returned. Note the notation of parameter with the double hashtags.
The second - and the one recommended in most cases - is to define the generic service resource endpoint and then pass query (q=) parameters as a map in the GET request call. This means fewer web service definitions and scripts that contain specific use-cases. As such below example service definition can be used by multiple scripts to query the Invoice_c custom object using its RESTful service.
Note: In addition pre-built queries - known as finders - are available on selected standard objects.
In this example a custom Formula Field exists on the Account standard object which will lookup custom object records (Invoices) for the matching Account.
Below queries the Invoice object using the Customer (Account) field, which is a Dynamic Choice List. Because of this field type we need to use the associated foreign key field Customer_id_c for our query (as the display value is not queryable). The script passes the run-time value (PartyId) taken from the users current Account object record.
def conn = adf.webServices.Invoices def custName = PartyId def myMAP = ['q':'Customer_Id_c='+custName] conn.dynamicQueryParams = myMAP def rslt = conn.GET() def val = rslt.items.RecordName return val
This very simple illustrative script returns just the RecordName field from the first matching Invoice record in the response Map object. This is shown on the screen as below:
If you get generic HTTP errors when testing, attempt the same request using a REST client (like POSTMAN) and investigate the raw response for more detailed error messages.
You may notice the Groovy Palette shows yellow warning messages from the type checking process, but these can be ignored as the API's used are valid.
For more details on RESTful groovy features review the excellent related documentation.