Contact Us 1-800-596-4880

ServiceNow Examples - Mule 4

You can try the following examples in Studio or Design Center.

Retrieve a ServiceNow Incident Record

In these steps, you create a Mule flow in Flow Designer that retrieves a record based on an incident number:

  1. Create a Mule application as a project in Design Center.

  2. Add and configure an HTTP Listener as a trigger for your flow, and set a path such as:

    /incident/get

    Or, use the default configuration for the HTTP Listener connection.

  3. Add the Invoke operation from the ServiceNow connector to your flow.

  4. Set up and test a ServiceNow configuration for the connection to the ServiceNow server, for example:

    • Configuration Name: ServiceNow_Config

    • Username: my_user_name

    • Password: my_password

    • Service Address: https://my_account.service-now.com

    • ServiceNow Version: Orlando

      Your ServiceNow account must either have the soap role set or be an admin account. Admin accounts automatically have access to SOAP and are preferred for testing. However, for production, create a dedicated API account with the soap role and any additional roles set to grant minimal necessary access.

    If the connection test is unsuccessful, correct any invalid connection parameters and retest the connection.

  5. In the Transport tab for the ServiceNow configuration, set up, test, and save a Basic Auth HTTP Message Dispatcher.

    Use the same username and password that you provided in the General tab, for example:

    • Username: my_user_name

    • Password: my_password

      If the connection test is unsuccessful, correct any invalid connection parameters and retest the connection.

  6. Select the Web Service Configuration values for the ServiceNow connector, for example:

    • Service: incident

    • Operation: getRecords

  7. Make these values available as menu items in the Service and Operation fields.

    The names become available once the connector loads the metadata for them.

    Do not add these names manually.
  8. Provide a transformation script for the connector.

  9. From the Invoke operation in Design Center, click Map for the Body field.

  10. Click the Script tab at the bottom of the Body window.

  11. Copy and paste in the transformation script as in this example:

    %dw 2.0
    output application/xml
    ns inc http://www.service-now.com/incident
    ---
    {
        inc#getRecords: {
            inc#number: "INC0012345"
        }
    }
    This example changes the output data type to application/xml.
  12. Add a Logger to the end of your flow that takes payload as the message.

  13. Deploy, sync, or run the app.

  14. Test the app by opening this from a browser: http://url.for.your.app:8081/incident/get or from an app such as Postman.

    The response should look similar to this example (several fields omitted for brevity):

<getRecordsResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <getRecordsResult>
    <active>1</active>
    <additional_assignee_list/>
    <approval>not requested</approval>
    <business_stc>0</business_stc>
    <calendar_duration/>
    <calendar_stc>0</calendar_stc>
    <caller_id>6816f79cc0a8016401c5a33be04be441</caller_id>
    <category>inquiry</category>
    <escalation>0</escalation>
    <knowledge>0</knowledge>
    <made_sla>1</made_sla>
    <notify>1</notify>
    <number>INC0021195</number>
    <opened_at>2017-07-27 03:13:54</opened_at>
    <opened_by>6816f79cc0a8016401c5a33be04be441</opened_by>
    <order>0</order>
    <priority>5</priority>
    <short_description>Server outage</short_description>
    <state>1</state>
    <sys_class_name>incident</sys_class_name>
    <sys_created_by>admin</sys_created_by>
    <sys_created_on>2017-07-27 03:14:52</sys_created_on>
    <sys_domain>global</sys_domain>
  </getRecordsResult>
</getRecordsResponse>

Retrieve Records from a ServiceNow Incident Table

This example creates a Mule flow in Anypoint Studio 7 to retrieve records from the Incident table.

  1. Create a Mule project in Anypoint Studio.

  2. Add and configure an HTTP Listener trigger for your flow with this path:

    getrecords

    HTTP Listener general settings with path set to getrecords
  3. Add a Transform Message component after the HTTP connector and provide a transformation script similar to this:

    %dw 2.0
    output application/xml
    ns ns0 http://www.service-now.com/incident
    ---
    {
    	ns0#getRecords: {
    		ns0#description: 'Test WSDL QA'
    	}
    }
    Add a description that matches records in your ServiceNow incident table.
  4. Add the Invoke operation from the ServiceNow connector after the Transform component.

  5. Set up, test, and save a ServiceNow configuration for the connection to the ServiceNow server. If the connection is unsuccessful, correct any invalid connection parameters, and test again.

    Servicenow studio connection
    Servicenow studio config transport tab
  6. In the General tab in the navigation, set Service to incident and Operation to getRecords.

  7. Optionally, you can choose Show Reference Values from the values (ALL, TRUE, FALSE)

    In Studio version 7.5.0 and later you can choose service and operation keys without specifying the Show Reference Values key to resolve the metadata. However, in earlier Studio versions, metadata won’t be loaded until you specify all of the metadata keys, and if you don’t specify all of the metadata keys, it results in a tooling exception.
    Servicenow metadata for Studio 7.5.0 without reference values
  8. Set the Message Body to payload.

  9. Add the Transform Message component to transform XML to JSON for better readability. For example:

    %dw 2.0
    output application/json
    ---
    payload
  10. Add a Logger to the end of your flow that takes a payload (or #[payload]) as the message.

    Servicenow flow - incident get records
  11. Deploy or Run your app.

  12. Test the app by navigating to http://localhost:8081/getrecords

The response should look similar to this example (several fields are omitted for brevity):

{
  "headers": {

  },
  "attachments": {

  },
  "body": {
    "getRecordsResponse": {
      "getRecordsResult": {
        "active": "1",
        "activity_due": "2019-09-24 09:48:15",
        "approval": "not requested",
        "business_stc": "0",
        "calendar_stc": "0",
        "category": "Software",
        "child_incidents": "0",
        "description": "Test WSDL QA",
        "escalation": "0",
        "hold_reason": "0",
        "impact": "1",
        "incident_state": "1",
        "knowledge": "0",
        "made_sla": "1",
        "notify": "1",
        "number": "INC0011616",
        "opened_at": "2019-09-20 07:30:34",
        "opened_by": "6816f79cc0a8016401c5a33be04be441",
        "order": "0",
        "priority": "1",
        "reassignment_count": "0",
        "reopen_count": "0",
        "severity": "3",
        "state": "1",
        "sys_class_name": "incident",
        "sys_created_by": "admin",
        "sys_created_on": "2019-09-20 07:30:34",
        "sys_domain": "global",
        "sys_domain_path": "/",
        "sys_id": "c03deab4db840010a0e6e04a48961999",
        "sys_mod_count": "47",
        "sys_updated_by": "system",
        "sys_updated_on": "2019-09-24 07:48:15",
        "upon_approval": "proceed",
        "upon_reject": "cancel",
        "urgency": "1",
      }
    }
  }
}

If you use ALL for Show Reference Values, the response is similar to this abbreviated output, in which the response element name for the display value field begins with dv:

{
  "headers": {

  },
  "attachments": {

  },
  "body": {
    "getRecordsResponse": {
      "getRecordsResult": {
        "active": "1",
        "dv_active": "true",
        "activity_due": "2020-05-07 13:14:19",
        "dv_activity_due": "2020-05-07 06:14:19",
        "additional_assignee_list": null,
        "dv_additional_assignee_list": null,
        "approval": "not requested",
        "dv_approval": "Not Yet Requested",
      }
    }
  }
}

The response element name for the display value field is prefixed with dv.

For more information, see ServiceNow - Display value.

Retrieve ServiceNow Incident Table Keys

This example creates a Mule flow to retrieve keys from the Incident table in a ServiceNow instance.

  1. Create a Mule application as a project in Anypoint Studio 7.

  2. Add and configure an HTTP Listener trigger for your flow using a Path such as this one:

    /getkeys

  3. Add a Transform message component after the HTTP connector.

  4. Provide a transformation script similar to this (use a description that matches records in your ServiceNow incident table):

    %dw 2.0
    output application/xml
    ns ns0 http://www.service-now.com/incident
    ---
    {
    	ns0#getKeys: {
    		ns0#description: 'Test WSDL QA'
    	}
    }
  5. Add the Invoke operation from the ServiceNow connector after the Transform component.

  6. Set up, test, and save a ServiceNow configuration for the connection to the ServiceNow server. If the connection is unsuccessful, correct any invalid connection parameters and test again.

  7. In the General tab navigation, set Service to incident and Operation to getKeys.

  8. Set the Message Body to payload.

  9. Add the Transform Message component to transform XML to JSON for better readability. For example:

    %dw 2.0
    output application/json
    ---
    payload
  10. Add a Logger to the end of your flow that takes a payload (or #[payload]) as the message.

  11. Deploy or Run your app.

  12. To test the app, navigate to: http://localhost:8081/getkeys

  13. The response should look similar to this:

    {
      "headers": {
    
      },
      "attachments": {
    
      },
      "body": {
        "getKeysResponse": {
          "sys_id": "c03deab4db840010a0e6e04a48961999,0f517ab8db840010a0e6e04a489619bc,6f1236f8db840010a0e6e04a489619f5",
          "count": "3"
        }
      }
    }
View on GitHub