DxSherpa - D

ServiceNow Orchestration – Custom REST Activity

ServiceNow Orchestration

ServiceNow orchestration provides a workflow engine to manage processes and automate things outside of ServiceNow instance.

Note: One should have basic knowledge of ServiceNow Orchestration and REST.

In ServiceNow Orchestration, activity designer is used to create custom activities. It enables user to construct reusable activity according to organization’s business requirement.

The User should have workflow_admin or activity_creator role to create custom activity.

Following are the customizable activity templates to create and manage custom activities.

  • SSH
  • PowerShell
  • REST
  • SOAP
  • JMS
  • SFTP
  • JDBC
  • JavaScript Probes
  • Probes

Let’s understand the creation of a REST activity. For demo, create a REST custom activity to make user in JIRA where JIRA is an open source Project Management tool.

NOTE: JIRA API information is available on the link provided below: https://developer.atlassian.com/cloud/jira/platform/rest/

Step 1: Create REST message to consume JIRA created user API:

Name: Name of the REST message.

Description: Provide short description.

Endpoint: API of the tool (JIRA) that one wants to consume.

In Authentication tab :

  • Authentication Type: Select the authentication type:

1) Basic

2) OAuth 2.0

  • Basic Auth Profile: Select the basic profile that is created.ServiceNow Orchestration – Custom REST Activity

In HTTP Request Tab :

  • HTTP Headers: This means that one will accept the ‘application data’ in JSON format and ‘content type’ will be JSON.ServiceNow Orchestration – Custom REST Activity

In HTTP Methods:

Now create HTTP methods of GET and POST to perform create or post operations.

ServiceNow Orchestration – Custom REST Activity

Click on New:

  • REST Message: It will populate automatically.
  • Name: Name of the method.
  • HTTP Method: POST
  • Endpoint: It will be automatically populated.ServiceNow Orchestration – Custom REST ActivityServiceNow Orchestration – Custom REST Activity

In HTTP request:

  • HTTP Headers: This implies that one will accept the ‘application’ data in JSON format and ‘content type’ will be JSON.
  • Select appropriate mid server, if required.
  • HTTP Query Parameter: Provide query parameters.
  • Content: Provide the data to create user in JIRA.
    ServiceNow Orchestration – Custom REST ActivityServiceNow Orchestration – Custom REST Activity

Now, click on auto-generate variables and test.  201 is the success code if the test is successful.

Step 2: Now let’s create custom activity:

In navigator search for orchestration ->workflow editor and open the workflow editor.

Click on Custom tab ->to create new custom activity click on (+) ->REST web service Activity.

ServiceNow Orchestration – Custom REST Activity

Multiple tabs are available in the activity. Let’s discuss them sequentially.

GENERAL TAB:

  • Name: Name of the activity
  • Execution Template: This is read-only field depending on the chosen template.
  • Short Description: Provides short description of the activity.
  • Application: Global.
  • Accessible from: Select the scope from which activity will be accessible.
  • Description: Provide description and click on continue.ServiceNow Orchestration – Custom REST Activity

INPUTS TAB:

Here, one can define information which will be given by the end user to create user. These are called as input variables. In below example, let’s take input from end user as their Name, Password, Email Address, and Display Name.

  • Name: Name the input variable
  • Type: Select variable such as string, Boolean, Integer, Decimal, Object.
  • Mandatory: Select YES, if you want to make this variable mandatory or else NO.
  • Default: Mention the default value of the variable.

Click on Continue.

ServiceNow Orchestration – Custom REST Activity

EXECUTION COMMAND TAB:

In execution command tab, one maps the input variables with workflow activity values.

  • REST Message: Search and select the self-created rest message.
  • REST Message Function: Select the method created earlier in HTTP Methods (POST or GET).
  • Endpoint: Mention the Endpoint.
  • Variable Substitution: Map input variables with activity values. Just drag and drop the inputs from INPUTS to Value. This enables to map activity inputs to respective rest message.
    ServiceNow Orchestration – Custom REST Activity
  • Timeout: Specify time limit for trying to connect to the API in seconds.
  • Authentication: There are multiple options available for using credentials. According to organizational need, one can select one of the available options.
  • Here, let’s select ‘Use Existing Credentials in REST message’. Now, it will use the credentials mentioned in REST message.Click on continue.ServiceNow Orchestration – Custom REST Activity

OUTPUT TAB:

In this tab, the variables in the output stage will be mapped to parameter values of REST test payload.

In this scenario, let’s take ‘Key’ as the output variable and drag and drop it into variable name in ‘Parse Rule’ section.

In Post-Processing, one can write scripts to handle/format  the incoming data using script. For example, validation of the data.

ServiceNow Orchestration – Custom REST Activity

Now click on the variable name of the ‘Parse Rule’ section. In this section, one needs to write parsing rule for output variable.

  • Parsing Source: Select the source for parsing. Here, let’s parse the Result body.
  • Parsing Type: There are 4 types of parsing:
    • Direct
    • XML
    • JSON
    • RegEx

Here, JSON parsing type is selected.

  • Sample payload data: Place the sample payload data.

Hint: One can get the sample payload data by using POSTMAN tool. Try to consume the JIRA rest API in postman and get the result body after success.

  • Expression: Select any of the variables in sample data payload e.g ‘Key’. Automatically, its expression gets created in expression tab.
  • Click on test expression. This will parse that particular expression and will give result in ‘Parsing result’.
    ServiceNow Orchestration – Custom REST Activity

Click on submit.

CONDITION TAB:

In condition tab, specify the conditions that workflow will require such as success, failure.

In any workflow defined, there should be at least 1 exit condition defined as ‘true’ otherwise workflow will not end and will get stuck. Workflow engine will always evaluate all false conditions based on order, then it will process true condition.

In this scenario, the workflow will be successful only if it gets the output key as not null i.e. it satisfies activity.Output.key!=null  condition.

On failure, workflow will end with else condition ‘true’.

ServiceNow Orchestration – Custom REST Activity

Step 3: Now click on Test Inputs:

Fill the test values and check the status. Also, check in JIRA whether user got created or not.

ServiceNow Orchestration – Custom REST Activity

This is how one can create custom test activity in ServiceNow orchestration.

Let us see the same activity in workflow with small example.

For demo, first create a catalog Item where user can fill the information.

ServiceNow Orchestration – Custom REST Activity

  • Now go to Orchestration->workflow Editor->create new workflow for JIRA user creation on sc_req_item table.
    ServiceNow Orchestration – Custom REST Activity
  • In Run Script: Get User Information from service catalog to workflow scratchpad for further use in the activity.For example:workflow.scratchpad.name = current.variables.name;workflow.scratchpad.displayName = current.variables.displayName;workflow.scratchpad.emailAddress = current.variables.emailAddress;workflow.scratchpad.password = current.variables.password;
    ServiceNow Orchestration – Custom REST Activity
  • In Jira Activity: Drag the custom activity created in workflow and fill the information as shown in screenshot.
    ServiceNow Orchestration – Custom REST Activity
  • END: connect to end of the activity.Now go to the catalog item and click on order now. This will create the user in JIRA.
    ServiceNow Orchestration – Custom REST Activity

We hope this blog has helped you in understanding Service Now Orchestration with custom REST activity in an efficient way.