Microsoft Teams integration - card notifications

User485076
Tera Contributor

Hi, 

I'm trying to change some fields on the notification card you receive in Teams as part of the integration. You can see from the screenshot that we're pulling in the Short Description to form the card title. In the code below, the other fields are set in the variable cardFieldsPerTable. I want to include a field from the RITM here as well under state, since we already have the RITM information from the ritmGr varible, I thought I could just dot walk to get it but doesn't work. Any suggestions?

 

User485076_0-1683637804300.png

code snippet:

 

var isRequestWithMultipleRITM = false;
		if (table == "sc_request") {
			var ritmGr = new GlideRecord("sc_req_item");
			ritmGr.addQuery("request", approvalGr.sysapproval);
			ritmGr.orderBy("number");
			ritmGr.query();

			if (ritmGr.next()) {
				if (!ritmGr.hasNext()) {
					shortDescription = translatedString.replace('{0}', ritmGr.getDisplayValue("short_description"));
				} else {
					isRequestWithMultipleRITM = true;

					var content = {
						"firstRitmShortDesc": ritmGr.getDisplayValue("short_description"),
						"otherRitmCount": (ritmGr.getRowCount() - 1).toString()
					};

					if (content.otherRitmCount == "1")
						shortDescription = gs.getMessageLang("Request for: {0} and {1} other item", approverLang, [content.firstRitmShortDesc, content.otherRitmCount]);
					else
						shortDescription = gs.getMessageLang("Request for: {0} and {1} other items", approverLang, [content.firstRitmShortDesc, content.otherRitmCount]);
				}
			}
		}

		var cardFieldsPerTable = {
			"default": ["sysapproval.number", "sysapproval.requested_for", "ritmGr.number", "sysapproval.due_date", "state"],
			"change_request": ["sysapproval.number", "sysapproval.risk", "sysapproval.start_date", "sysapproval.end_date", "state"]
		};

 

 

3 REPLIES 3

User485076
Tera Contributor

full code:

 

var MSTeamsApprovalNotificationUtil = Class.create();
MSTeamsApprovalNotificationUtil.prototype = {
    initialize: function() {
    },

	/*
	 * Approval adaptive card has two containers,
	 * a) 1st container includes heading, fields and view details button
	 * b) 2nd container includes text note input and approve/reject buttons
	 * When create, for request with multiple RITMs, only show 1st container without fields
	 * When update, exclude 2nd contains
	 */
	getAdaptiveCard: function(table, sysId, cardType, cardData, action) {
		var cardsUtil = new MSTeamsAdaptiveCardsUtil();
		var cardsHandler = cardsUtil.getCardsHandler(table);
		var card = cardsHandler.getAdaptiveCard(table, sysId, cardType, cardData);

		if (card && action == 'create' && !cardData.is_request_with_multiple_RITM) {
			var actionsUtil = new MSTeamsMessageActionsUtil();
			var actionsHandler = actionsUtil.getActionsHandler(table);
			var secondContainerWithActions = actionsHandler.getMessageActions(table, sysId, cardType);
			card["body"].push(secondContainerWithActions);
		}

		return card;
	},

	/*
	 * approval notifcation is actionable when
	 * 1. no signature is needed
	 * 2. is not approve for HR Case
	 * 3. short description is not empty (request is exception)
	 */
	checkApprovalActionable: function (approvalGr) {
		return !this._isApprovalESig(approvalGr) && !this._isApprovalHRCase(approvalGr) && this._isApprovalNotifActionable(approvalGr);
	},

	_isApprovalESig: function (approvalGr) {
		var esigRegistryGR = new GlideRecord("e_signature_registry");
		if (!esigRegistryGR.isValid())
			return false;

		var table = approvalGr.getValue("source_table");
		if (!table)
			table = approvalGr.sysapproval.sys_class_name;
		if (!table)
			return false;

		esigRegistryGR.addQuery("enabled", "true");
		esigRegistryGR.addQuery("table_name", table);
		esigRegistryGR.query();
		return esigRegistryGR.hasNext();
	},

	_isApprovalHRCase: function (approvalGr) {
		var sourceTable = approvalGr.getValue("source_table");
		var hrCaseTables = new GlideTableHierarchy("sn_hr_core_case").getAllExtensions();
		return hrCaseTables.indexOf(sourceTable) >= 0;
	},

	_isApprovalNotifActionable: function (approvalGr) {
		var sourceTable = approvalGr.getValue("source_table");

		// for request table
		if (sourceTable == "sc_request" || approvalGr.sysapproval.sys_class_name == "sc_request")
			return true;

		// for task table
		if (approvalGr.sysapproval)
			return !gs.nil(approvalGr.getDisplayValue("sysapproval.short_description"));

		// for non-task table
		if (!approvalGr.sysapproval) {
			var sourceGr = new GlideRecord(sourceTable);
			if (!sourceGr.isValidField("short_description"))
				return false;
			return !gs.nil(sourceGr.getDisplayValue("short_description"));
		}
	},

	// build msg and fields to show on adaptive card
	buildCardFieldsObj: function (approvalGr) {
		if (!approvalGr)
			return {};

		var util = new MSTeamsNotificationBuilderUtil();
		var approverLang = util.getUserLang(approvalGr.approver);
		var translatedString = gs.getMessageLang("Approval for: {0}", approverLang);
		var shortDescription = translatedString.replace('{0}', approvalGr.getDisplayValue("sysapproval.short_description"));
		var description = approvalGr.getDisplayValue("sysapproval.description");
		var table = approvalGr.getValue("source_table");
		if (!table)
			table = approvalGr.sysapproval.sys_class_name;

		// for request, build short description and check whether a request with multiple RITMs
		var isRequestWithMultipleRITM = false;
		if (table == "sc_request") {
			var ritmGr = new GlideRecord("sc_req_item");
			ritmGr.addQuery("request", approvalGr.sysapproval);
			ritmGr.orderBy("number");
			ritmGr.query();

			if (ritmGr.next()) {
				if (!ritmGr.hasNext()) {
					shortDescription = translatedString.replace('{0}', ritmGr.getDisplayValue("short_description"));
				} else {
					isRequestWithMultipleRITM = true;

					var content = {
						"firstRitmShortDesc": ritmGr.getDisplayValue("short_description"),
						"otherRitmCount": (ritmGr.getRowCount() - 1).toString()
						
					};

					if (content.otherRitmCount == "1")
						shortDescription = gs.getMessageLang("Request for: {0} and {1} other item", approverLang, [content.firstRitmShortDesc, content.otherRitmCount]);
					else
						shortDescription = gs.getMessageLang("Request for: {0} and {1} other items", approverLang, [content.firstRitmShortDesc, content.otherRitmCount]);
				}
			}
		}

		var cardFieldsPerTable = {
			"default": ["sysapproval.number", "sysapproval.requested_for", "sysapproval.due_date", "state"],
			"change_request": ["sysapproval.number", "sysapproval.risk", "sysapproval.start_date", "sysapproval.end_date", "state"]
		};

		var cardFieldsObj = {
			"card_msg_heading": shortDescription,
			"card_description": description,
			"card_fields": (table in cardFieldsPerTable) ? cardFieldsPerTable[table] : cardFieldsPerTable["default"],
			"is_request_with_multiple_RITM": isRequestWithMultipleRITM,
			"portal_suffix": util.getPortalSuffix(approvalGr)
		};

		return cardFieldsObj;
	},

    type: 'MSTeamsApprovalNotificationUtil'
};

Hello @User

 

Did you get to achieve this by modifying the script, currently I am trying to include variables information in the actionable notifications. Modified script works in background script, but not reflected in ServiceNow for Teams.

TJ29
Tera Contributor

Did you manage to work this out? I'd like to stick the variables on mine