ServiceNow with Microsoft Teams Approvals Part 2 - HTTP Trigger
Published Mar 13 2023 07:10 AM 6,977 Views
Microsoft

As more and more organizations move towards remote work, communication and collaboration tools have become essential for seamless and efficient workflows. Microsoft Teams is one such tool that allows teams to communicate, collaborate, and stay productive from anywhere. Teams Approvals is a feature within Microsoft Teams that enables users to create, manage, and track approvals directly within Teams. On the other hand, ServiceNow is a powerful platform for managing workflows, incidents, and service requests. Integrating Teams Approvals with ServiceNow can streamline approval processes and improve overall efficiency.

 

There are different ways to implement this integration, in the first part of the post, we saw how we could integrate ServiceNow with Microsoft Teams Approvals to simplify the approval process for ServiceNow change requests.

 

In this second part of the post, we will explore how we can create a custom HTTP trigger in Power Automate to allow ServiceNow to trigger the approval process in Microsoft Teams. This will enable us to automate the approval process even further and integrate it with other systems that may not be directly connected to ServiceNow.

 

By the end of this post, you'll have a better understanding of how to integrate Microsoft Teams Approvals with ServiceNow and the benefits it can bring to your organization.

 

 

Prerequisites

 

ServiceNow Environment

 

ServiceNow provides a free development environment called the Now Platform (https://developer.servicenow.com/dev.do), which enables developers to quickly build, test, and deploy applications to improve workflows and productivity within their organizations. With a free account on the Now Platform, you can start building powerful applications that leverage ServiceNow's advanced capabilities for workflow management, incident management, and service requests. Whether you're an experienced developer or just getting started, the Now Platform offers a flexible and easy-to-use environment for creating custom solutions tailored to your organization's unique needs.

 

Microsoft Teams

  1. Go to the Microsoft Teams website (https://www.microsoft.com/en-us/microsoft-teams/group-chat-software) and click on the "Sign up for free" button.

  2. Enter your email address and click on "Next". If you already have a Microsoft account, you can sign in with that account.

  3. Create a new password and click on "Sign up".

  4. Follow the prompts to complete the setup process, such as entering your name and company information.

  5. Once your account is set up, you can download the Microsoft Teams app on your desktop or mobile device and start collaborating with your team.

 

With a free Microsoft Teams account, you can chat with colleagues, make voice and video calls, share files, and collaborate on projects from anywhere. Plus, with its seamless integration with other Microsoft products, such as Office 365 and SharePoint, Microsoft Teams can help streamline your organization's workflows and improve productivity.

 

Power Automate Environment

 

Microsoft provides a free Power Automate environment (https://powerautomate.microsoft.com/en-us/#home-signup), which enables users to automate workflows across multiple applications and services. With Power Automate, you can create automated processes, such as approvals, notifications, and data synchronization, to save time and increase efficiency. Whether you're a business user or a developer, the Power Automate environment provides an intuitive and easy-to-use interface for creating and managing workflows. Plus, with its seamless integration with other Microsoft products, such as Teams and SharePoint, Power Automate can help streamline your organization's workflows and improve collaboration.

 

 

Scenarios

First, ensure that you have a valid account for both Microsoft Teams and ServiceNow, as well as access to Power Automate.

 

1. Scheduled Trigger

This scenario is available in the first part of this post.

 

2. Http Trigger

HTTP triggers are a powerful feature in Power Automate that allows us to create custom APIs and automate processes with external systems. By creating a custom HTTP trigger, we can make the approval process more flexible and scalable, and enable other systems to interact with Microsoft Teams Approvals without having to go through ServiceNow.

 

Here's a scenario on how to integrate Microsoft Teams Approvals with ServiceNow using Power Automate's scheduled trigger:

 

  1. Open a web browser and go to the Power Automate portal.

  2. Sign in to Power Automate using your Microsoft account or organizational account.

  3. Once you're signed in, you should see the Power Automate portal dashboard. On the left-hand menu, click on "Create" to create a new flow.

  4. Choose a template from the available options, or start from scratch by selecting "Instant - from blank".

 

 

create schedule flow.png

 

 

 

  1. Give your flow a name, such as Pending Approvals via ServiceNow. Choose the When an HTTP request is received as the trigger, and click on Create.

 

 

create http flow.png

 

 

 

 

To set up the "When a HTTP request is received" trigger, we need to define the input parameters that will be sent via a rule from ServiceNow (we will create this rule later).

 

  1. Click on the "When a HTTP request is received" trigger to select it. Paste the following Request Body JSON Schema that will be sent by ServiceNow into the text box:

 

{
    "type": "object",
    "properties": {
        "sysapproval": {
            "type": "string"
        },
        "sys_id": {
            "type": "string"
        },
        "due_date": {
            "type": "string"
        },
        "sys_created_by": {
            "type": "string"
        },
        "approver": {
            "type": "string"
        },
        "short_description": {
            "type": "string"
        },
        "description": {
            "type": "string"
        },
        "opened_by": {
            "type": "string"
        },
        "approver_email": {
            "type": "string"
        },
        "requestor_email": {
            "type": "string"
        },
        "link_details": {
            "type": "string"
        }
    }
}

 

 

json body.png

 

 

 

The next step is sending the approval request to Microsoft Teams Approvals app.

 

  1. Add a new action after the "When a HTTP request is received" step. In the search box, type "Approvals" and select the Start and wait for an approval:

 

 

start approval.png

 

 

 

  1. Select Approve/Reject - Everyone must approve for the Approval type field.

  2. Set up the mapping between the ServiceNow data fields and the corresponding fields in the Power Automate action:

Field Value
Title short_description
Assigned to approver_email
Details description
Item link link_details
Item link description ServiceNow more detail

 

 

approvals fields.png

 

 

 

 

  1. Add a new action after the "Start and wait for an approval" step. In the search box, type "Control" and select the Condition control:

 

 

condition control.png

 

 

 

 

  1. Inform the value Outcome from the Start and wait for an approval step and the value Approve:

 

 

 

 

if condition.png

 

 

 

  1. Add a new action for the If yes step. In the search box, type "ServiceNow" and select the Update Record control:

 

update record approve.png

 

 

This action will update the corresponding approval records in ServiceNow with the latest information retrieved from Microsoft Teams.

  1. In the Record Type field, make sure to select the second "Approval" item in the dropdown list, which corresponds to the "approval" table from production environment and not the first one which is for the staging environment.

  2. In the System ID field, select the sys_id from the When a HTTP request is received step.

 

sys_id_if_approval.png

 

 

 

  1. In the Comments field, select the Responses Comments from the Start and wait for an approval step.

  2. In the State field, inform the value approved.

 

approve request.png

 

 

 

  1. Repeat the steps 12-16, but add the new action inside the If not step and the value rejected for the State field.

 

approve reject steps.png

 

 

 

 

  1. Save the flow.

 

Note that when you save your trigger, you will be given a URL that you can use to invoke the trigger from external systems.

 

Set up ServiceNow Business Rule

 

Business Rules are server-side logic that execute when database records are queried, updated, inserted, or deleted. For more details, please check the ServiceNow Business Rules documentation.

 

To set up a ServiceNow Business Rule to send a request to Microsoft Power Automate every time there is a new pending approval, you can follow these general steps:

 

  1. Navigate to the Business Rules module in ServiceNow. This can be done by typing "Business Rules" in the navigation bar and selecting the appropriate option.

 

snow-br-menu.png

 

 

 

 

  1. Click the "New" button to create a new business rule:

 

snow-br-new.png

 

 

 

 

  1. Give the business rule a name and description that accurately describes its purpose. For example, MS Approvals.

  2. Choose the table to which the business rule applies. This can be done by selecting the table from the drop-down menu in the "Applies to" field. For example, Approval [sysapproval_approver].

  3. Specify the conditions that must be met for the business rule to run. This is done by adding conditions to the "When to run" section of the business rule. For example, you could specify that the rule should run after a new record is created or updated.

  4. Add a filter condition to consider only pending approvals (state is requested).

 

snow-br-whentorun.png

 

  1. Click on the Advanced tab to define the actions that the business rule should take when it runs. This is done by adding script to the "Script" section of the business rule. The script can be written in JavaScript and can perform a variety of actions, such as updating a field, sending an email notification, or creating a related record.

 

It's important to note that creating a business rule in ServiceNow requires some knowledge of JavaScript and the ServiceNow platform. If you're not familiar with these concepts, it may be helpful to seek assistance from a ServiceNow administrator or developer.

 

Add the following code:

 

 

(function executeRule(current, previous) {
	
	var snowURL = "https://dev128124.service-now.com/";
	var sysapproval = current.getValue("sysapproval");
	var approver = current.getValue("approver");
	var openedBy;
	
	var jsonTask ;
	var jsonApprover ;
	var jsonRequestor ;

    //this isnt the best practice. Consider change it to oauth2 authentication
	var user = 'CHANGE THIS VALUE';
	var password = 'CHANGE THIS VALUE';
	
	//GET TASK with the details of the pending approval 
	
	var taskRequest = new sn_ws.RESTMessageV2();
	
	taskRequest.setBasicAuth(user,password);
	taskRequest.setRequestHeader("Accept","application/json");
	taskRequest.setRequestHeader('Content-Type','application/json');
	
	taskRequest.setEndpoint(snowURL + "api/now/table/task?sysparm_query=sys_idIN" + sysapproval + "&sysparm_exclude_reference_link=true&sysparm_fields=number%2Cshort_description%2Copened_by%2Csys_id%2Cimpact%2Copened_at%2Cpriority%2Cdescription");

	taskRequest.setHttpMethod("GET");		
	
	var responseTask = taskRequest.execute();

	var taskBody = responseTask.getBody();

	jsonTask = new JSONParser().parse(taskBody);

	openedBy = jsonTask.result[0].opened_by;
	
	//GET APPROVER EMAIL that will be used to send the card to Teams Approvals
	
	var approverRequest = new sn_ws.RESTMessageV2();
	
	approverRequest.setBasicAuth(user,password);
	approverRequest.setRequestHeader("Accept","application/json");
	approverRequest.setRequestHeader('Content-Type','application/json');
	
	approverRequest.setEndpoint(snowURL + "api/now/table/sys_user?sysparm_query=sys_idIN"+ approver +"&sysparm_fields=sys_id,name,email");

	approverRequest.setHttpMethod("GET");		
	
	var responseApprover = approverRequest.execute();

	var approverBody = responseApprover.getBody();

	jsonApprover = new JSONParser().parse(approverBody);

	//GET REQUESTOR EMAIL that will be used on Teams Approvals card
	var requestorRequest = new sn_ws.RESTMessageV2();
	
	requestorRequest.setBasicAuth(user,password);
	requestorRequest.setRequestHeader("Accept","application/json");
	requestorRequest.setRequestHeader('Content-Type','application/json');
	
	requestorRequest.setEndpoint(snowURL + "api/now/table/sys_user?sysparm_query=sys_idIN"+ openedBy +"&sysparm_fields=sys_id,name,email");

	requestorRequest.setHttpMethod("GET");		
	
	var requestorResponse = requestorRequest.execute();

	var bodyRequestor = requestorResponse.getBody();

	jsonRequestor = new JSONParser().parse(bodyRequestor);

	//CALL POWER AUTOMATE TO SEND TO TEAMS APPROVALS
    try {

		var body = {
				"sysapproval": sysapproval,
				"sys_id": current.getValue("sys_id"),
				"due_date": current.getValue("due_date"),
				"sys_created_by": current.getValue("sys_created_by"),
				"approver": approver,
				"short_description": "" + jsonTask.result[0].short_description,
				"description": "" + jsonTask.result[0].description,
				"opened_by": "" + openedBy,
				"approver_email": "" + jsonApprover.result[0].email,
				"requestor_email": "" + jsonRequestor.result[0].email,
				"link_details" : snowURL + "now/nav/ui/classic/params/target/sysapproval_approver.do%3Fsys_id%3D" + current.getValue("sys_id")
		};
		
        var r = new sn_ws.RESTMessageV2();
        
        // Change the following URL by the one you have created as HTTP trigger on Power Automate
		r.setEndpoint("https://xxx.westus.logic.azure.com:443/workflows/xxxxxxxx56f6/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=EKb91O-VP86wsFeP144H63-vZHK8XFpcuu7HLHc7DEM");
		
		r.setHttpMethod("POST");
        r.setRequestBody(JSON.stringify(body));
	
		r.setRequestHeader("Accept","application/json");
		r.setRequestHeader('Content-Type','application/json');
      
        var response = r.execute();
        var httpStatus = response.getStatusCode();
		
    } catch (ex) {
        var message = ex.message;
		gs.error("Error message: " + message);
    }

    gs.info("Webhook target HTTP status response: " + httpStatus);
  
    
})(current, previous);

 

  1. Click on Submit to save it.

 

snow-br-script.png

 

  1. To get to the list of pending approvals in ServiceNow:

 

  1. In the navigation bar at the top of the screen, click on the "My Approvals" link. This will take you to the "My Approvals" page.
  1. On the "My Approvals" page, you should see a list of all of your pending approvals. This list will include the details of each approval request, such as the name of the item being approved, the requester, and the date the request was submitted.

 

In case you can't see any pending approval, consider removing the filter on the top of the page by clicking on All.

 

snow-br-removefilter.png

 

 

  1. To view the details of a specific approval request, click on the request in the list. This will take you to the approval form, where you can review the details of the request. Change the status to Requested and click on Update.

 

snow-br-requested.png

 

 

  1. Switch back to Microsoft Teams and observe that your pending approval request is there:

approvals card.png

 

 

 

 

 

 

Co-Authors
Version history
Last update:
‎Apr 17 2023 07:03 AM
Updated by: