forked from ServiceNowDevProgram/code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
72 lines (72 loc) · 2.97 KB
/
code.js
File metadata and controls
72 lines (72 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Script Include: AuditFieldChangeNotifier
* Description: Sends Slack/Teams notifications when specific fields change on configured tables.
* Usable in: Business Rule (after update)
*/
var AuditFieldChangeNotifier = Class.create();
AuditFieldChangeNotifier.prototype = {
initialize: function () {
// Slack or Teams webhook URL (store in sys_properties for security)
this.webhookUrl = gs.getProperty('x_custom.audit_notifier.webhook_url', '');
// Default app name
this.appName = gs.getProperty('x_custom.audit_notifier.app_name', 'ServiceNow');
},
/**
* Send notification if the specified fields have changed
* @param {GlideRecord} current - Current record
* @param {GlideRecord} previous - Previous record
* @param {Array} fieldsToWatch - Array of field names to monitor
*/
notifyOnFieldChange: function (current, previous, fieldsToWatch) {
try {
if (!this.webhookUrl) {
gs.warn('[AuditFieldChangeNotifier] Webhook URL not configured.');
return;
}
var changes = [];
fieldsToWatch.forEach(function (field) {
if (current[field] + '' !== previous[field] + '') {
changes.push({
field: field,
oldValue: previous[field] + '',
newValue: current[field] + ''
});
}
});
if (changes.length === 0)
return; // No relevant field changed
var payload = this._buildPayload(current, changes);
this._sendWebhook(payload);
} catch (e) {
gs.error('[AuditFieldChangeNotifier] Error: ' + e.message);
}
},
/**
* Build payload for Slack/Teams message
*/
_buildPayload: function (current, changes) {
var shortDesc = current.short_description ? current.short_description + '' : '(No short description)';
var tableName = current.getTableName();
var recordUrl = gs.getProperty('glide.servlet.uri') + tableName + '.do?sys_id=' + current.sys_id;
var changeLines = changes.map(function (c) {
return `• *${c.field}* changed from _${c.oldValue}_ → *${c.newValue}*`;
}).join('\n');
return JSON.stringify({
text: `🛠️ *${this.appName}* — Field Update Notification\n\n*Record:* <${recordUrl}|${tableName}> \n*Description:* ${shortDesc}\n\n${changeLines}\n\n_Updated by ${gs.getUserDisplayName()}_`
});
},
/**
* Send payload to webhook
*/
_sendWebhook: function (payload) {
var r = new sn_ws.RESTMessageV2();
r.setEndpoint(this.webhookUrl);
r.setHttpMethod('POST');
r.setRequestHeader('Content-Type', 'application/json');
r.setRequestBody(payload);
var response = r.execute();
if (response.getStatusCode() >= 400)
gs.error('[AuditFieldChangeNotifier] Webhook error: ' + response.getBody());
},
type: 'AuditFieldChangeNotifier'
};