Skip to content

Foundry Lab 04 - VoltScript pre/postprocessors

Duration 15 Min

What you will learn

You'll learn how to create a JSON integration service, manipulate / validate input parameters in a VoltScript preprocessor and manipulate the output in VoltScript postprocessor.

Prerequisites

  • Lab 03 completed

Steps

Create Integration Service

  1. Log into Volt Foundry.
  2. On the Apps page, click ADD NEW
  3. On Configure Services tab, click the Integration tab and then click CONFIGURE NEW.

    CONFIGURE NEW

  4. Set the Name to "petstore".

  5. Select "JSON" under Service Type.
  6. Set the Base URL to "https://petstore.swagger.io/v2/".
  7. Click SAVE & ADD OPERATION.
  8. On the NewOperation tab, set the Name to "getPets".
  9. Set the Target URL to "pet/findByStatus?status=$status".

    Note

    The $status means it will pick up an input parameter called "status".

  10. Scroll down to the Request Input tab and click + Add Parameter.

  11. Set the NAME to "status".
  12. Set the TEST VALUE to "available".
  13. Scroll down to the bottom of the operation and click SAVE AND FETCH RESPONSE.

    Success

    A JSON payload of available pets is returned.

Create preprocessor

  1. On the NewOperation tab, expand the Advanced section.
  2. On the Custom Code Invocation tab, select the VoltScript radio button under Preprocessor.
  3. Select the Validate Input Parameter code snippet. This shows how to retrieve and check input parameters, using return false to abort execution.
  4. Replace the code with the following VoltScript code:

    Dim status as String
    status = VoltMxRequest.getInputParam("status")
    
    Select Case status
    
    Case "0":
        Call VoltMxResult.addInputParam("status","available")
    Case "1":
        Call VoltMxResult.addInputParam("status","pending")
    Case "2":
        Call VoltMxResult.addInputParam("status","sold")
    Case Else
        Call VoltMxResult.setErrorMessage("Unexpected status passed - " & status)
        return false
    End Select

    This manipulates the incoming parameter, switching from a number to a corresponding string.

  5. Change the test value to 0.

  6. Scroll down to the bottom of the operation and click SAVE AND FETCH RESPONSE.

    Note

    The output result just includes opstatus and httpStatusCode. This is because nothing has been mapped from the backend response (the response from the JSON service) to the output result (the Foundry response).

  7. On the Backend Response, click [0] to create a response.

    Create Response

    If you scroll down the operation and look at the Response Output tab, you'll see a responseList collection has been added to the output, along with all the JSON elements.

    Response Output

  8. On the Test tab, change the status passed in the JSON object to "available".

    available

  9. Click Save and Retest.

    Success

    You've ran validation and manipulated an input parameter. Although this particular use case is unlikely, two of the most common use cases for a preprocessor will be validating an input parameter and setting an input parameter, which we've done here.

Create postprocessor

  1. On the NewOperation tab, expand the Advanced section.
  2. On Custom Code Invocation tab, select the "VoltScript" radio button under Postprocessor.
  3. Paste the following VoltScript code:

    Dim child as JsonObject
    Dim responseList as JsonObject
    Dim newResponseList as New JsonObject()
    Set responseList = VoltMXResult.result.getChild("responseList")
    ForAll jsonObj in responseList.getChildren()
        Set child = jsonObj
        If (child.isChild("name")) Then
            Call child.insertValue("name", StrConv(child.getChild("name").scalarValue, 3))
            Call newResponseList.appendToJsonArray(child)
        End If
    End ForAll
    Call VoltMxResult.result.insertValue("responseList", newResponseList)

    This gets the responseList element in the VoltMxResult result. "responseList" was the name the response was mapped to on the Response Output tab. We then iterate the children and, for those with a "name" element, proper case the name and add to a new JsonObject array. We then replace the responseList element with the new JSON array.

  4. Scroll down to the bottom of the operation and click SAVE AND FETCH RESPONSE.

    Success

    You've manipulated the response from a third-party integration service. This is one of the most common use cases for a postprocessor.