Copy email history from original HR case on the transferred HR case
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
The requirement is that - full email history to transfer with an HR case when it is reassigned or recategorized (transferred), so that all communications related to the case remain visible, accurate, and centralized in one record. I am trying to consolidate the email logs from the original case and print them in work notes using a business rule.
Table: HR Case [sn_hr_core_case]
When to run: After insert, Transferred from is not empty AND Transferred from changes.
I am open to any suggestions on how it could be done in any other/better way as well.
Thanks much in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
hey @ArpitaVK
Condition:
current.transferred_from.changes() && !gs.nil(current.transferred_from)
Script
(function executeRule(current, previous /*null when async*/) {
if (!current.transferred_from) {
return;
}
var oldCaseId = current.getValue('transferred_from');
gs.info('AK07 oldCaseSysID = ' + oldCaseId);
var consolidatedLog = "--- CONSOLIDATED EMAIL HISTORY FROM " +
current.transferred_from.getDisplayValue() + " ---\n\n";
var emailGr = new GlideRecord('sys_email');
emailGr.addQuery('instance', oldCaseId); // fetch emails from OLD case
emailGr.addQuery('target_table', 'sn_hr_core_case');
emailGr.orderBy('sys_created_on');
emailGr.query();
if (!emailGr.hasNext()) {
gs.info('AK07 No emails found for old case');
return;
}
while (emailGr.next()) {
consolidatedLog += "Date: " + emailGr.getValue('sys_created_on') + "\n";
var fromUser = emailGr.getValue('user_id') ?
emailGr.user_id.getDisplayValue() :
emailGr.getValue('user');
consolidatedLog += "From: " + fromUser + "\n";
consolidatedLog += "Subject: " + emailGr.getValue('subject') + "\n";
var bodyText = emailGr.getValue('body_text') || '';
bodyText = bodyText.replace(/<[^>]*>/g, '');
consolidatedLog += "Body:\n" + bodyText + "\n";
consolidatedLog += "--------------------------------------------------\n\n";
}
gs.info('AK07 Consolidated email log created');
// Do NOT use current.update() in After BR
current.work_notes = consolidatedLog;
})(current, previous);
*************************************************************************************************************************************
If this response helps, please mark it as Accept as Solution and Helpful.
Doing so helps others in the community and encourages me to keep contributing.
Regards
Vaishali Singh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago - last edited 3 hours ago
Hello @vaishali231, Thanks for your reply, but this script did not work for me. Can you also specify when to run conditions?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hello Arpita,
Could you try -
(function executeRule(current, previous) {
var oldCaseId = current.getValue('transferred_from');
if (!oldCaseId) {
gs.info('AK07 No transferred_from value found, exiting.');
return;
}
gs.info('AK07 oldCaseId = ' + oldCaseId);
var consolidatedLog = "--- CONSOLIDATED EMAIL HISTORY FROM "
+ current.transferred_from.getDisplayValue() + " ---\n\n";
var emailGr = new GlideRecord('sys_email');
emailGr.addQuery('instance', oldCaseId); // ✅ old case
emailGr.addEncodedQuery('target_tableSTARTSWITHsn_hr_core_case'); // ✅ HR tables only
emailGr.orderBy('sys_created_on');
emailGr.query();
gs.info('AK07 Queried sys_email table');
if (!emailGr.hasNext()) {
gs.info('AK07 No emails found for old case: ' + oldCaseId);
return;
}
while (emailGr.next()) {
consolidatedLog += "Date: " + emailGr.getValue('sys_created_on') + "\n";
if (emailGr.user_id) {
consolidatedLog += "From: " + emailGr.getValue('user_name')
+ " (" + emailGr.user_id.getDisplayValue() + ")\n";
} else {
consolidatedLog += "From: " + emailGr.getValue('user_name')
+ " (" + emailGr.getValue('user') + ")\n";
}
consolidatedLog += "Subject: " + emailGr.getValue('subject') + "\n";
var rawBody = emailGr.getValue('body') || ''; // ✅ null guard
var bodyText = rawBody.replace(/<[^>]*>?/gm, '');
consolidatedLog += "Body:\n" + bodyText + "\n";
consolidatedLog += "--------------------------------------------------\n\n";
}
gs.info('AK07 Consolidated log built, length = ' + consolidatedLog.length);
// ✅ Safe update without recursion
var updateGr = new GlideRecord('sn_hr_core_case');
if (updateGr.get(current.sys_id)) {
updateGr.work_notes = consolidatedLog;
updateGr.autoSysFields(false);
updateGr.setWorkflow(false);
updateGr.update();
gs.info('AK07 HR case ' + current.number + ' updated with email logs');
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago - last edited 3 hours ago
Hello @Pavan Srivastav , Thanks for your reply, but this script did not work for me. Can you also specify when to run conditions?
