ServiceNow Integration with FlexDeploy via Incoming Webhook

Objective – When change ticket is approved or rejected in Service Now, we can configure an incoming webhook to notify FlexDeploy, which in turn can either approve it or reject tasks associated with change ticket. You can continue to do polling for change ticket status but this will allow for better performance and speed.

  1. The first step would be to create a Business Rule in Service Now for a specific table. The business rule will trigger the event whenever the ticket is updated, and the information is passed to FlexDeploy.

  2. The second step would be to configure the incoming webhook in FlexDeploy and set up the script that finds the change ticket details based on the change number in the payload and calls approve or reject methods using the FlexDeploy function class.

Configuration in ServiceNow

  •   Send Dynamic date from ServiceNow by setting up Business Rules.

  •   Go to your service now instance, search for Business Rule, and create a new business rule.

image-20240124-135637.png
  • Configuration should be like below. The filter condition used here is based on how FlexDeploy out of box determines a SNOW ticket is approved or not. You can use a different filter if needed.

image-20240130-040137.png
  • Go to the Advanced tab and set up the script. The language used in the below example is ServiceNow script.

  • You must place your endpoint URL under the setEndpoint method. This is URL to FlexDeploy incoming webhook for processing this event.

(function executeRule(current, previous /*null when async*/ ) { gs.debug("Business rule invoked!!"); var request = new sn_ws.RESTMessageV2(); request.setEndpoint('Place the endpoint URL here'); // here you can add the url where you want mssg request.setHttpMethod('POST'); request.setRequestHeader("Accept", "application/json"); request.setRequestHeader("Content-Type", "application/json"); request.setRequestBody("{\"number\":\""+current.number+"\",\"approval\":\""+current.approval+"\"}"); // field you want var response = request.execute(); gs.debug(response.getBody()); })(current, previous);
  • Your business rule is ready with the server-side script.

Configuration in FlexDeploy

  • Now you will need to configure the incoming webhook with the matching script provider.

  • Configure the Incoming webhook.

Provider Match Script - ServiceNow

This sample provider match script for ServiceNow validates payload received from ServiceNow, which matches what was scripted in ServiceNow in one of the previous step.

//perform checks and functions to ensure an incoming message is valid and matches this provider LOG.fine("Evaluating ServiceNow for incoming message"); def userAgent = HTTP_HEADERS['user-agent']; def match = userAgent && userAgent.contains("ServiceNow"); LOG.fine("ServiceNow provider is a match: ${match}"); return match;

Function Script - Approve/Reject Task

  • Below is the script that needs to be placed in the incoming webhook.

import flexagon.fd.model2.pojo.*; // Execute FLEXDEPLOY functions on an incoming webhook message def functionName = "updateTask"; def approval = PAYLOAD.approval; def changeNumber = PAYLOAD.number; LOG.info("Ticket: " + changeNumber + " with approval: " + approval); def tasks = FLEXDEPLOY.getPendingTasksByChangeTicket(changeNumber); for (task in tasks) { if(approval.equalsIgnoreCase("approved")) { FLEXDEPLOY.approveTaskById(task.getTaskId(),'Approved Via Webhook'); LOG.info("Task is approved"); } else if(approval.equalsIgnoreCase("rejected")) { FLEXDEPLOY.rejectTaskById(task.getTaskId(),'Rejected Via Webhook'); LOG.info("Task is rejected!!"); } }
The following macros are not currently supported in the footer:
  • style