integrations

Automate sending data from ServiceNow to MidlEO on incident creation

Author: Vasil Vasilev
July 27, 2023

ServiceNow and MidlEO platform support REST integration. That is why you can easily make an integration between both of them.
For example, when an incident is created in ServiceNow, the JSON data is automatically sent to my MidlEO system and trigger ticket creation and middleware automation based on the workflow logic behind it.
Here are the steps.


ServiceNow Business Rules

A Business rule is a server-side script that runs when a record is displayed, inserted, updated, or deleted, or when a table is queried.
If someone opens an incident, you can define a Business rule to run.

ServiceNow's RESTMessageV2 API

In the Business Rules advanced configuration, you can trigger custom scripts. For this purpose, you need ServiceNow RESTMessageV2 API. The RESTMessageV2 API allows you to send outbound REST messages using JavaScript.

How to configure

  1. In your ServiceNow instance, go to System Definition > Business Rules.
  2. Select New.
  3. Give your new Business Rule a name, choose what table you want it to trigger on (Incident in our case), and check the Advanced box.
  4. Next, choose After for when you want it to run. In this case, the data will be sent after the Incident is created
  5. navigate to the Advanced tab. In the Script editor, add the following code. Make sure to replace with your public MidlEO application URL. Should be in this form: https://server.domain/snowapi/newticket

(function executeRule(current, previous ) {
    try {
        var r = new sn_ws.RESTMessageV2();
        r.setEndpoint("https://server.domain/snowapi/newticket");
        r.setHttpMethod("post");
        var usr = new GlideRecord('sys_user');
        usr.get('sys_id', current.getValue("caller_id"));
        var useremail = usr.getValue('email');
        var userid=gs.getUserID();
        var username=gs.getUserName();
        var number = current.getValue("number");        
        var opened_at = current.getValue("opened_at");
        var short_description = current.getValue("short_description");
        var description = current.getValue("description");
        var service = current.getValue("service_offering");
        var confitem = current.getValue("cmdb_ci");
        var category = current.getValue("category");
        var priority = current.getValue("priority");
        var sys_id = current.getValue("sys_id");
        var subcategory = current.getValue("subcategory");
        var state = current.getValue("state");

        var obj = {
            "number": number,
            "useremail": useremail,
            "userid": userid,
            "username": username,
            "opened_at": opened_at,
            "short_description": short_description,
            "description": description,
            "category": category,
            "priority": priority,
            "sys_id": sys_id,
            "subcategory": subcategory,
            "service": service,
            "confitem": confitem,
            "state": state,
            "reqtype": "qm", //qm is for IBM MQ request
            "reqwf":"us8Ky5Tm" //workflow for middleware team for example
        };
        
        var body = JSON.stringify(obj);
        r.setRequestBody(body);
        var response = r.execute();
        var httpStatus = response.getStatusCode();
    } catch (ex) {
        var message = ex.message;
        gs.error(message);
    }
    gs.info(httpStatus);
})(current, previous);


reqtype - can be different depends on the desired request type
reqwf - can be taken from the  MidlEO WorkFlow section


Based on the WorkFlow logic, once a MidlEO ticket is opened, it can be automatically processed and sent for deployment, or manual intervention will be needed.

Screenshots from the final solution.

- ServiceNow incident opened




- MidlEO ticket opened




- ServiceNow work notes added once MidlEO ticket is updated/closed