Name

sn_ms_teams_ah.MSTeamsMessageBuilder

Description

No description available

Script

var MSTeamsMessageBuilder = Class.create();
MSTeamsMessageBuilder.prototype = {
  initialize: function() {
  },
  
  /**
   * Generates a MS Teams MessageCard payload and includes details from a 
   * Task record. 
   * 
   * @param {Object}      options 
   * @param {GlideRecord} options.task    - Task record to get values from
   * @param {string}      options.title   - Text to put in the header of the MessageCard
   * @param {string}      options.message - Custom message to add context
   * @param {string}      options.fields  - Comma separated list of fields to send 
   * @param {string}      options.themeColor - Accent color for MessageCard
   * 
   * @returns {Object} JS object representation of MessageCard payload
   * https://docs.microsoft.com/en-us/outlook/actionable-messages/card-reference
   */ 
  getPayloadFromTask: function(options) {
  	var title; 
  	if (options.title) {
  		title = options.title + ' - ' + options.task.getValue('number') ; 
  	} else {
  		title = options.task.getValue('number'); 
  	}
  	
  	var payload = this.generateBasicPayload({
  		title:           title, 
  		sectionTitle:    options.task.getValue('short_description'), 
  		additionalText:  options.message, 
  		themeColor:      options.themeColor
  	}); 
  	
  	// Retrieve the first section
  	var section = payload.sections[0]; 
  	
  	// Build the facts object from the options passed in
  	section.facts = this.getFactsFromGR(options.task, options.fields); 
  	
  	// Build the OpenUri action to open task in ServiceNow
  	var actionLabel = 'View ' + options.task.getED().getLabel() + ' in ServiceNow'; 
  	var actionUri   = gs.getProperty('glide.servlet.uri') + options.task.getLink(); 
  	var action      = this.generateOpenUriAction(actionLabel, actionUri); 
  	payload.potentialAction.push(action); 
  	
  	return payload; 
  }, 
  
  /**
   * Generates an array of "facts" from fields in a GlideRecord. 
   *
   * @param {GlideRecord} gr     - GlideRecord to get "facts" from
   * @param {string}      fields - Comma separated list of field names 
   * 
   * @returns {Array} Array of facts in the format found in MessageCard documentation. 
   * https://docs.microsoft.com/en-us/outlook/actionable-messages/card-reference
   */ 
  getFactsFromGR: function(gr, fields) {
  	var fieldArray = fields.split(','); 
  	var fieldName, fieldLabel, fieldValue, fact; 
  	var facts = []; 
  	
  	fieldArray.forEach(function (f) {
  		fact       = {}; 
  		fieldName  = f.trim();
  		if (gr.isValidField(fieldName)) {
  			fieldLabel = gr[fieldName].getLabel(); 
  			fieldValue = gr.getDisplayValue(fieldName); 
  			
  			if (!gs.nil(fieldValue)) {
  				fact.name  = fieldLabel; 
  				fact.value = fieldValue; 
  				facts.push(fact); 
  			}
  		}
  	}); 
  	
  	return facts; 
  }, 
  
  /** 
   * Generates an OpenUri Action. 
   * 
   * @param {string} name - Display value of the action 
   * @param {string} uri  - URI this action links to 
   * 
   * @returns {Object} OpenUri Action object as documented here: 
   * https://docs.microsoft.com/en-us/outlook/actionable-messages/card-reference
   */ 
  generateOpenUriAction: function(name, uri) {
  	var action = {
          "@type": "OpenUri",
          "name": name,
          "targets": [
  			{ "os": "default", "uri": uri },
  			{ "os": "iOS",     "uri": uri },
  			{ "os": "android", "uri": uri },
  			{ "os": "windows", "uri": uri }
  		]
      }; 
  	
  	return action; 
  }, 
  
  /**
   * Generates a basic MessageCard payload
   */ 
  generateBasicPayload: function(options) {
  	var payload = {
  		"@type":      "MessageCard", 
  		"@context":   "http://schema.org/extensions", 
  		"title":      options.title, 
  		"summary":    options.title, 
  		"themeColor": options.themeColor.replace('#', '') || "", 
  		"sections": [{
  			"markdown": false,
  			"title":            options.sectionTitle     || "", 
  			"text":             options.sectionText      || "", 
  			"activityTitle":    options.activityTitle    || "", 
  			"activitySubtitle": options.activitySubtitle || "", 
  			"activityText":     options.activityText     || ""
  		}, {
  			"text":             options.additionalText
  		}],
  		potentialAction: [] // This will be populated at runtime
  	};
  	
  	return payload; 
  }, 
  
  /**
   * Generates an error message when MS Teams returns an error
   * 
   * @param ${string} statusCode   - HTTP status code returned by MS Teams endpoint
   * @param ${string} responseBody - Response Body from MS Teams endpoint
   *
   * @returns ${string} Human readable representation of the error
   */
  generateErrorMessage: function(statusCode, responseBody) {
  	var message = 'Error sending message to Microsoft Teams. '; 
  	message += 'Status code: ' + statusCode   + '. '; 
  	message += 'Message: '     + responseBody + '.'; 
  	return message;
  }, 

  type: 'MSTeamsMessageBuilder'
};

Sys ID

7b22024b13e0030039a039ed9344b0f3

Offical Documentation

Official Docs: