Skip to content

Commit 26fff84

Browse files
Create GenericEmailUtility.js
1 parent 0af7289 commit 26fff84

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
var GenericEmailUtility = Class.create();
2+
GenericEmailUtility.prototype = {
3+
initialize: function() {},
4+
5+
// Generate an Outlook (mailto) link with watermark tracking
6+
get_Outlook_link: function() {
7+
try {
8+
const email_payload = JSON.stringify({
9+
"REQUESTOR_ID": "",
10+
"TITLE": "",
11+
"BODY": "",
12+
"REQUEST_ID": "",
13+
"TABLE_ID": ""
14+
});
15+
16+
var mailtoLink = false;
17+
const raw_data = this.getParameter("sysparm_email_body") || email_payload;
18+
19+
if (global.JSUtil.notNil(raw_data)) {
20+
var email_data = JSON.parse(raw_data);
21+
22+
const to = this.getEmail(email_data.REQUESTOR_ID);
23+
const cc = gs.getProperty("instanceEmailAddress"); // instance default CC
24+
const subject = email_data.TITLE || '';
25+
const body = email_data.BODY || '';
26+
27+
const watermark = this.getWatermark(email_data.REQUEST_ID, email_data.TABLE_ID);
28+
29+
// Construct mailto link
30+
mailtoLink = 'mailto:' + to + '?cc=' + cc;
31+
32+
if (subject)
33+
mailtoLink += '&subject=' + encodeURIComponent(subject);
34+
35+
if (body)
36+
mailtoLink += '&body=' + encodeURIComponent(body);
37+
38+
if (watermark)
39+
mailtoLink += encodeURIComponent("\n\nRef: " + watermark);
40+
}
41+
42+
return mailtoLink;
43+
44+
} catch (ex) {
45+
gs.error("Error in get_Outlook_link(): " + ex.message);
46+
return false;
47+
}
48+
},
49+
50+
// Fetch watermark ID (creates one if missing)
51+
getWatermark: function(record_id, table_name) {
52+
var wm = new GlideRecord('sys_watermark');
53+
wm.addQuery('source_id', record_id);
54+
wm.orderByDesc('sys_created_on');
55+
wm.query();
56+
57+
if (wm.next()) {
58+
return wm.getValue('number');
59+
}
60+
61+
wm.initialize();
62+
wm.source_id = record_id;
63+
wm.source_table = table_name;
64+
wm.insert();
65+
66+
return wm.getValue('number');
67+
},
68+
69+
// Retrieve user’s email address
70+
getEmail: function(user_id) {
71+
if (global.JSUtil.notNil(user_id)) {
72+
var user = new GlideRecordSecure('sys_user');
73+
if (user.get(user_id))
74+
return user.email.toString();
75+
}
76+
return '';
77+
},
78+
79+
type: 'GenericEmailUtility'
80+
};

0 commit comments

Comments
 (0)