This example shows how REST API requests can be used within a ServiceNow Workflow to deploy a Buttonwood Broker blueprint from a ServiceNow Service Catalog item.
The following related articles provide further details in configuring associated processes:
- Automate REST API Reference
- ServiceNow - Example - How to Create REST API Requests
- ServiceNow - Example - Populate the ServiceNow Catalog with Published Automate Catalog Items
Note: This is an example only. Please consult with your organisation's ServiceNow administrators to correctly gather and store the required variables and keys to ensure the process conforms to the organisation's business processes and policies.
Prerequisites
- This example has been tested against the following versions of ServiceNow:
- Jakarta through to Quebec
- This example has been tested against the following instance types of ServiceNow:
- Hosted
- Hosted developer clone
- Developer (this is a free service which can be requested via http://developer.servicenow.com)
- Access to the ServiceNow Workflow Editor module is required (may require additional licensing)
- The following REST API messages have been created as per article ServiceNow - Example - How to Create REST API Requests
Name Method Endpoint POST Content Deploy Blueprint POST https://<automate_fqdn_or_ip>/api/v1/deployment {
"catalogItemId" : ${catalogItemId},
"leaseEndDate" : ${leaseEndDate},
"name" : "${deploymentName}",
"description" : "${deploymentDescription}",
"costCentreName" : "${costCentreName}"
}
Information Required
The following information will be required for this example.
- API Token for the user to allow ServiceNow to communicate with the Automate API service
- All other required information is expected to be passed in via a ServiceNow catalog item order form
Creating the Workflow
- Log in to ServiceNow as an administrative user
- Navigate to Workflow > Workflow Editor
A new tab will open to the Workflow Editor - Click the New Workflow button
- Enter the following details for the workflow
- Name: Name of the workflow
- Table: The ServiceNow table context for this workflow - Requested Item [sc_req_item]
- Description: Description of this workflow
- Click Submit
- A basic workflow canvas is populated with Begin and End activities
- Using the activity selector, navigate to Core Activities > Utilities
Locate the Run Script activity - Drag and drop the Run Script activity onto the workflow canvas
The activity editor will automatically open - Paste the following script into the script editor
// Start logging workflow.info("Script Deploy Blueprint starting..."); // Configure variables var userName = gs.getUserName(); var userDisplayName = gs.getUserDisplayName(); var apiToken = 'OWY1ZTE1NWUtNWM2MC00YWFiLTg2YTEtZGNiYmZmOGE5OGM1'; var sNowRestMessage = 'Buttonwood Cloud Broker'; // Name of the REST Message created in the Service Now instance which communicates with a Buttonwood Broker var sNowRestMethodPostDeployment = 'Deploy Blueprint'; // Name of the HTTP Method for POST /deployment API call workflow.info("User Name: " + userName); workflow.info("Display Name: " + userDisplayName); workflow.info("API Token: " + apiToken); workflow.info("Catalog ID: " + current.variables.catalogItemId); workflow.info("Deployment Name: " + current.variables.deploymentName); workflow.info("Deployment Description: " + current.variables.deploymentDescription); workflow.info("Cost Centre: " + current.variables.costCentreName); workflow.info("Lease End Date: " + current.variables.leaseEndDate); // Deploy the requested catalog item workflow.inf("Deploying the requested catalog item"); try { var r = new sn_ws.RESTMessageV2(sNowRestMessage, sNowRestMethodPostDeployment); r.setStringParameterNoEscape('BearerApiToken', apiToken); r.setStringParameterNoEscape('catalogItemId', current.variables.catalogItemId); r.setStringParameterNoEscape('deploymentName', current.variables.deploymentName); r.setStringParameterNoEscape('deploymentDescription', current.variables.deploymentDescription); r.setStringParameterNoEscape('costCentreName', current.variables.costCentreName); r.setStringParameterNoEscape('leaseEndDate', current.variables.leaseEndDate); var response = r.execute(); var responseBody = response.getBody(); var httpStatus = response.getStatusCode(); } catch(ex) { var message = ex.getMessage(); } workflow.info("HTTP Status: " + httpStatus); workflow.info("Response Body: " + responseBody); // End logging workflow.info("Script Deploy Blueprint ending...");
- Ensure that the following variables are edited to reflect the environment
- apiToken: API Token for the user to allow ServiceNow to communicate with the Broker API service
- Click Submit
- Utilise the activity connectors to insert the Run Script activity into the workflow between Begin and End activities
- The workflow can now be published
Obtaining the Workflow Value/Identifier
When creating a ServiceNow catalog item, a workflow can be associated with the catalog item. When the catalog item is ordered, the associated workflow is started.
The following steps describe the steps required to obtain the workflow value/identifier, which is used to associate this workflow to a catalog item.
- Log in to ServiceNow as an administrative user
- Navigate to Workflow > Workflow Editor
A new tab will open to the Workflow Editor - Select the required workflow
- From the Workflow Actions menu, select Properties
- From the More Actions menu, select Show XML
The XML output opens in a new window - Find and not the value for the workflow xml node
Running the Workflow
This script runs when it is called by an associated catalog item, which passes variables into this workflow. To run this workflow, order the catalog item.
- Log in to ServiceNow
- Navigate to Self-Service > Service Catalog
- Select the category which the blueprints have been populated into
This example uses the Services category - Select the catalog item
This example uses the AWS Blueprint item
Note: If expected catalog items do no appear as expected, please check that the user has access to the service catalog/category, and that the correct sys_id values have been specified when populating the service catalog items - Provide the information as requested in the catalog item order form
- Click Order Now
- A confirmation screen is displayed
- The deployment is seen in Buttonwood Broker
Checking Workflow Logs
- Log in to ServiceNow as an administrative user
- Navigate to Workflow > All Contexts
- The workflow task should appear towards the top of the list when sorted by Started
- Click on the Started time for the relevant record
Note: Clicking on other values instead of the time will open the generic record for those values rather than the context record for the time that the task ran - The Workflow Log tab displays a log of the request as well as any logged outputs from the associated workflow script