ServiceNow

Setup the App administrator account

App admin url: https://developer.servicenow.com/dev.do#!/home

Once the app admin account is created and verified you need to create an instance.

It will take a minute or two or three or five to complete.

Now your developer instance is ready and accessible: https://dev180380.service-now.com/. Note that when creating a new account, your instance ID (dev180380 in this case) will be different.

Setup Webhooks

To configure webhooks in ServiceNow to receive notifications when a new record is created, follow these steps. This involves creating an Outbound REST Message, setting up a Business Rule to capture record creation, and configuring the webhook.

Create an Outbound REST Message

  • Navigate to System Web Services > Outbound > REST Message.

  • Click on the New button to create a new REST Message.

  • Fill in the fields such as:

    • Name: e.g., AppmixerNotifications.

    • Endpoint: https://<YOUR_API_BASE>/plugins/appmixer/servicenow/events for example https://api.appmixer.com/plugins/appmixer/servicenow/events

  • Click Submit and then open the newly created REST Message.

  • Click on the HTTP Methods tab and create a new HTTP Method.

  • Fill in the details such as:

    • Name: e.g., events.

    • HTTP Method: POST.

    • Endpoint: Ensure this is filled with your webhook listener’s URL.

  • Optionally, add default HTTP Request Headers (e.g., Content-Type) and customize the Request Body or Query Parameters if needed.

  • Save the HTTP Method.

Outbound REST

Create a Business Rule

  • Navigate to the table for which you want to capture new record creation. For example, if it’s the Incident table, go to Incident > All.

  • Right-click on the form header and select Configure > Business Rules.

  • Alternatively, you can navigate to Activity Subscriptions -> Administration -> Business Rules.

  • Click on the New button to create a new Business Rule.

    • Set the fields as follows:

    • Name: e.g., incident events.

    • Table: Select the appropriate table (e.g., Incident).

    • When: after, Insert, Update, Delete. This setting will trigger an event in Appmixer whenever a record is inserted, updated, or deleted in the table. Business Rule

  • Under Advanced, check the box Advanced to open the script editor.

You need to be a user with admin permissions to perform this step.

  • In the Script section, add the following code. Please note that AppmixerNotifications must match the name of the Outbound REST Message specified in the previous step, and events should correspond to the event name defined in the Outbound REST Message.

    (function executeRule(current, previous /*null when async*/) {
    
        function serializeGlideRecordToJSON(gr) {
            const obj = {};
            const fields = gr.getFields();
    
            for (let i = 0; i < fields.size(); i++) {
                const field = fields.get(i);
                const fieldName = field.getName();
    
                // skipping sys_id and other sensitive fields if necessary
                if (fieldName !== 'sys_id' && fieldName !== 'sys_updated_on' && fieldName !== 'sys_created_on') {
                    obj[fieldName] = gr.getValue(fieldName);
                }
            }
    
            return obj;
        }
    
        const operation = current.operation();
        
        // Uncomment this line for tracing
        // messages can be seen in System Logs > System Log > All
        //gs.info("Webhook Rule Fired for " + gs.getProperty('instance_name'));
    
        const instance = gs.getProperty('instance_name');
        const tableName = current.getTableName();
        
        // Make sure this matches the name of your Outbound REST Message
        // `AppmixerNotifications` is the name of the Outbound REST Message
        // `events` is the name of the HTTP method within the `AppmixerNotifications` REST Message mapped to the Appmixer plugin url  
        const rq = new sn_ws.RESTMessageV2('AppmixerNotifications', 'events');
        const requestBody = {
            'type': instance + '.' + tableName + '.' + operation,
            'data': serializeGlideRecordToJSON(current)
        };
    
        rq.setRequestBody(JSON.stringify(requestBody));
        rq.execute();
    
    })(current, previous);    

Debug

To see messages triggered by the Business Rule go System Logs > System Log > All

Log Message Screenshot

To see messages triggered by the sn_ws.RESTMessageV2 within the Business Rule, go to System Logs > Outbound HTTP Requests

Log Message Screenshot

Last updated

Was this helpful?