Author: Purnendu Kumar

In this blog, we are going to see:

  1. How to create a record in ServiceNow using MuleSoft
  2. How to get real time data from ServiceNow to MuleSoft

Assumptions: You should have basic knowledge of Anypoint Studio tool and Anypoint Platform.

Pre-requisites:

  1. Anypoint Platform Account
  2. ServiceNow Account

If you do not have a ServiceNow account, then please follow the steps below to create it:

Step 1: Go to https://developer.servicenow.com/

Click on Sign up and start building.

Provide required details.

You will receive an email from ServiceNow containing a link- asking to verify your email address.

Click on the link and verify it.

Click on Sign In and provide your email address and password.

Step 2: Now you can request a developer instance which we will need while configuring ServiceNow connector.

Select an instance. I am using “New York” for this demo.

Now, click on request.

Click on instance url. You will be redirected to the below page.

Now, you are all set to use the ServiceNow developer instance.

Whenever you need to login next time, use instance url and provide your developer instance login details.

Part 1: Now, let’s see the steps to create a record in ServiceNow using MuleSoft. 

Let’s create a Mule application in Anypoint Studio as shown below. I’m taking the “Incident” table from ServiceNow for this demo and will be using below Mule flow to create an incident in SNOW (ServiceNow).

This application will take post calls from any client and create an incident in ServiceNow.

  1. Create a Mule project. Give it a meaningful name. I’ve named it “mule-to-service-now-app”.
  2. Drag a HTTP listener in the Canvas and add connector configuration. I’m using default configuration with port ‘8081’. Set the path as /mule-to-snow.
  3. Drag ServiceNow connector to the flow. Configure ServiceNow connector with required values for insert operation.

If ServiceNow connector is not visible in Mule palette, then please add ServiceNow connector to Mule palette from exchange.

I’m using the 6.5.0 version for this demo. Make sure pom.xml has the correct version, as the previous version does not show the “New York” instance version in connector configuration.

<dependency>
            <groupId>com.MuleSoft.connectors</groupId>
            <artifactId>mule-servicenow-connector</artifactId>
            <version>6.5.0</version>
            <classifier>mule-plugin</classifier>
</dependency>

Steps to configure ServiceNow connector: Use below information

User Name: ‘admin’ ( You will receive after instance creation)

Password: You will receive after instance creation

Service Address: You will receive after instance creation

Service Now Version: It should be the same as what you selected while creating the instance

Go to the Transport section and select basic auth http message dispatcher provider and provide below details and test the connection.

 Set Service as “incident” and Operations as “insert”.

Now drag and drop the Transform message component before the ServiceNow connector in the flow. You will see meta-data of incident tables are available in the flow. You can use any fields. I’m using Description and short_description fields from Incident table for this demo.

Here is the dataweave code where Description from request is mapped to Description and Subject is mapped to short description.

%dw 2.0
output application/xml
ns inc http://www.service-now.com/incident
---
{
  inc#insert: {
    Description : payload.Description,
    short_description: payload.Subject,
  }
}

Convert the output from SNOW to json and log it in Console.

Mule flow: 

Configuration xml

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:servicenow="http://www.MuleSoft.org/schema/mule/servicenow" xmlns:ee="http://www.MuleSoft.org/schema/mule/ee/core"
	xmlns:http="http://www.MuleSoft.org/schema/mule/http"
	xmlns="http://www.MuleSoft.org/schema/mule/core" xmlns:doc="http://www.MuleSoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.MuleSoft.org/schema/mule/core http://www.MuleSoft.org/schema/mule/core/current/mule.xsd
http://www.MuleSoft.org/schema/mule/http http://www.MuleSoft.org/schema/mule/http/current/mule-http.xsd
http://www.MuleSoft.org/schema/mule/ee/core http://www.MuleSoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.MuleSoft.org/schema/mule/servicenow http://www.MuleSoft.org/schema/mule/servicenow/current/mule-servicenow.xsd">
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="820822dd-6bae-4f3d-b999-129cb5e6f35c" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<servicenow:config name="ServiceNow_Config" doc:name="ServiceNow Config" doc:id="6674e692-75c1-4bc3-947a-81f22275b019" >
		<servicenow:basic-connection username="admin" password="K93v***" serviceAddress="https://dev****.service-now.com" >
			<servicenow:transport >
				<servicenow:basic-auth-http-message-dispatcher-provider username="admin" password="K93vP****" />
			</servicenow:transport>
		</servicenow:basic-connection>
	</servicenow:config>
	<flow name="mule-to-service-now-appFlow" doc:id="cb638a4c-ef6c-4012-9153-c0944a055718" >
		<http:listener doc:name="Listener" doc:id="6c693a9e-029c-4e17-a33d-a40802fc1896" config-ref="HTTP_Listener_config" path="/mule-to-snow"/>

		<ee:transform doc:name="Incident create request" doc:id="7e792de7-608d-486f-a73c-705d95ca9cfb" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/xml
ns inc http://www.service-now.com/incident
---
{
  inc#insert: {
    Description : payload.Description,
    short_description: payload.Subject,
  }
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>

		<servicenow:invoke doc:name="Invoke" doc:id="6774bf93-0842-4109-a642-9d054df0096e" config-ref="ServiceNow_Config" service="incident" operation="insert"/>
		<ee:transform doc:name="Convert SNOW output into json" doc:id="75c1b357-001b-4cc7-a43f-46073db600c3" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<logger level="INFO" doc:name="print payload" doc:id="13f0b87f-43b0-4520-bfbe-6a30c42b8b6d" message="Incident created in Service Now : #[payload]"/>
	</flow>
</mule>

Test the application: 

Use postman or any browser to trigger the flow with below payload. You will see sys_id and incident number in response.

Request:

{
"Description" : "Description from MuleSoft",
"Subject" : "subject from MuleSoft"
}
 
**Mapping inside mule application:
    Description : payload.Description,
    short_description: payload.Subject,

Response:

{
    "headers": {},
    "attachments": {},
    "body": {
        "insertResponse": {
            "sys_id": "14980bbf2f70201095d2ad2ef699b620",
            "number": "INC0010002"
        }
    }

Note: Both sys_id and number are unique in SNOW for Incident. Sys_id can be used to update an existing incident.

Login to ServiceNow and search for incidents in “filter navigator”.

You will be able to see incidents created.

Click on the incident number to see the details.

Part 2: Now, let’s see how to get real time data from ServiceNow to MuleSoft.

We will do the below task to achieve this.

  1. We have to create a Mule application and deploy it to Cloudhub. We have to deploy the Mule application on cloudhub as events sent by ServiceNow did not reach the application deployed on a local machine.
  2.  We have to create a Business Rule in ServiceNow for the table. In our case it would be in ‘Incident’.

So, let’s create a Mule application in Anypoint studio and deploy it to Cloudhub.

Below is a sample Mule application which has HTTP listener with path as /service-now-to-mule. We are converting payload into json and printing it in console to see the event that we will be getting from ServiceNow.

ServiceNow will invoke this flow using a business rule which uses REST API. We will see this at the end.

Mule application

Configuration xml:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.MuleSoft.org/schema/mule/ee/core" xmlns:http="http://www.MuleSoft.org/schema/mule/http"
	xmlns="http://www.MuleSoft.org/schema/mule/core"
	xmlns:doc="http://www.MuleSoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.MuleSoft.org/schema/mule/core http://www.MuleSoft.org/schema/mule/core/current/mule.xsd
http://www.MuleSoft.org/schema/mule/http http://www.MuleSoft.org/schema/mule/http/current/mule-http.xsd
http://www.MuleSoft.org/schema/mule/ee/core http://www.MuleSoft.org/schema/mule/ee/core/current/mule-ee.xsd">
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="0134abf8-a350-49f8-88df-6a7fcbcf100d" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<flow name="service-now-to-muleFlow" doc:id="26d4f80e-8de5-4bf4-9bcd-605853873171" >
		<http:listener doc:name="Listener" doc:id="53a72dd8-7487-4170-a24d-9d54ccc7989c" config-ref="HTTP_Listener_config" path="/service-now-to-mule"/>
		<ee:transform doc:name="Transform Message" doc:id="759651b4-75e5-48e3-9435-60b2cf616d4d" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<logger level="INFO" doc:name="Logger" doc:id="d8b1c8de-f895-4d4f-b99b-81cb1f8295f6" message="Real time event from service Now : #[payload]"/>
	</flow>
</mule>

Steps to deploy the application to Cloudhub:

Right click on Project -> Anypoint Platform -> Deploy to CloudHub

Provide Anypoint Platform credentials and click on Sign in.

Choose the environment as Sandbox.

Keep the default setting and click on Deploy Application.

Click on Open in Browser: You will see the application starting under Runtime Manager.

Go to the Settings section and you will see App url. 

This is the Inbound endpoint url of our Mule application. We will use this url in Business Rule to send the event.

http://service-now-to-mule.us-e2.cloudhub.io/service-now-to-mule

Now, let’s create a Business Rule for the Incident table in ServiceNow.

Login to ServiceNow.

Open any existing Incident and right click on 3 horizontal lines->Configure->Business Rules.

Create New Business Rule.

Give a name, table should be Incident.

Check Advanced option.

When to run: Change when to ‘After’. It sends the event when the record is fully created.

Check Insert. I want to send events only when a new record gets created in the Incident table. We have other options as well. You can set as per your requirement.

Go to the Advanced section and paste below script and submit. Your business rule is configured which will send the event containing all the fields listed in MyObj to MuleSoft inbound endpoint which we have just created.

Note: setEndpoint = http://service-now-to-mule.us-e2.cloudhub.io/service-now-to-mule

Script: 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
		try { 
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('http://service-now-to-mule.us-e2.cloudhub.io/service-now-to-mule');
//Inbound endpoint of your mule application
request.setHttpMethod('POST');
request.setRequestHeader('Content-Type','application/json');
// fields name that you want to see inside mule

		var MyObj = {
			'incident' : current.getValue('number'), 
			'short_description' : current.getValue('short_description'),
			'description' : current.getValue('description'),
			'sys_id' : current.getValue('sys_id'),
			'sys_updated_by' : current.getValue('sys_updated_by'),
			'sys_created_by' : current.getValue('sys_created_by'),
			'sys_updated_on' : current.getValue('sys_updated_on'),
			'sys_created_on' : current.getValue('sys_created_on')
			
		};
request.setRequestBody(JSON.stringify(MyObj));
var response = request.execute();
		
gs.log(response.getBody());
}
catch(ex) {
 var message = ex.getMessage();
}
	

})(current, previous);

Test the integration: 

Go to ServiceNow and create an Incident manually.

Fill description, short description, and select any existing caller and then submit.

The moment you submit this record in ServiceNow, you will see the event getting printed in the Cloudhub logs section.

Please see below screenshot for your reference.

Now you can transform this data inside MuleSoft and send it to any application/system as needed.

I hope you enjoyed reading it. Thanks!

Leave a Comment