- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 10-17-2018 05:46 AM
Suppose that you have set up HR Core, and your HR peeps want to send emails to outside vendors using the built-in email client. This keeps the emails and replies (in theory) contained within the platform and respective "tickets". However, due to strict controls on HR Core, the replies from the vendors result in email errors along the lines of "Unable to locate sn_hr_core_case c51917bb2f81ebggd9e14b8e0210c715 for inbound email processing". What to do?
There is an OOB Business Rule in HR Core named "Restrict query" that sets criteria for who can query the Cases. For example, it has "opened_by" and "watch_list" conditions. However, this assumes that the vendor emailing back into the system has a user account. How cumbersome to have to keep creating accounts for everyone that HR wants to email.
What I did:
First, I created a Business Rule on the "sys_email" table. This will automatically add any address in the To, Cc, or Bcc lines to the Work notes list.
Name: HR Add Recipients to Worknotes List
Advanced: true
When: after
Insert: true
Update: true
Order: 10,000
Filter Conditions:
[Target table] [is] [sn_hr_core_case]
[Recipients] [is not empty]
Script:
(function executeRule(current, previous /*null when async*/) {
var arr_recipients = [];
var arr_copied = [];
var arr_blind_copied = [];
var arr_all = [];
arr_recipients = current.recipients.split(",");
arr_copied = current.copied.split(",");
arr_blind_copied = current.blind_copied.split(",");
arr_all = arr_all.concat(arr_recipients, arr_copied, arr_blind_copied);
var hr = new GlideRecord('sn_hr_core_case');
hr.get('sys_id', current.instance);
var arr_wl = [];
arr_wl = hr.work_notes_list.split(",");
arr_all = arr_all.concat(arr_wl);
arr_all = arr_all.filter(onlyUnique);
hr.work_notes_list = arr_all.toString();
hr.update();
function onlyUnique(value, index, self){
return self.indexOf(value) === index;
}
})(current, previous);
Next, I created a Business Rule on the "sn_hr_core_case" table. This will look at the Work notes list, and automatically create a new user for any that doesn't already exist.
Name: Auto Create External Users
Advanced: true
When: after
Insert: true
Update: true
Order: 10,000
Filter Conditions:
[Work notes list] [changes]
[Work notes list] [is not empty]
Script:
(function executeRule(current, previous /*null when async*/) {
var arr_wl = [];
arr_wl = current.work_notes_list.split(',');
for (var i = 0; i < arr_wl.length; i++){
if (arr_wl[i].indexOf("@") != -1){
var user = new GlideRecord('sys_user');
user.addQuery('email', arr_wl[i]);
user.query();
if (user.next()){
arr_wl[i] = user.sys_id.toString();
}
else{
user.initialize();
user.user_name = "HRC_" + arr_wl[i];
user.first_name = "HR Contact";
user.last_name = arr_wl[i];
user.email = arr_wl[i];
var new_pass = new global.Password_Generator();
user.user_password = new_pass.run(16);
arr_wl[i] = user.insert();
}
}
}
arr_wl = arr_wl.filter(onlyUnique);
current.work_notes_list = arr_wl.toString();
current.update();
function onlyUnique(value, index, self){
return self.indexOf(value) === index;
}
})(current, previous);
Note that "global.Password_Generator()" is a Script Include that I created to generate random passwords.
Last, because we want the vendors to be added to the Work notes list and not the Watchlist, I had a reason to edit the OOB Business Rule on the "sn_hr_core_case" table named "Restrict query" to include ".addOrCondition("work_notes_list", "CONTAINS", userId)". However, I think it is best practice to disable the OOB Business Rule and make a copy of it that I edit.
Name: Restrict query 2
Advanced: true
When: before
Query: true
Order: 100
Filter Conditions:none
Condition: gs.getUser().getName() != 'system' && gs.getUser().getRoles().indexOf(hr.ROLE_HR_CASE_READER) == -1
Script:
if (gs.isInteractive() || gs.getCallerScopeName() != 'sn_hr_le') {
var userId = gs.getUserID();
var hrUtils = new hr_Utils();
current.addQuery("opened_by", userId)
.addOrCondition("opened_for", userId)
.addOrCondition("parent.ref_sn_hr_core_case.opened_for", userId)
.addOrCondition("watch_list", "CONTAINS", userId)
.addOrCondition("work_notes_list", "CONTAINS", userId)
.addOrCondition("sys_id", "IN", hrUtils.getCaseSysIdListForApprovals(userId) + ','
+ hrUtils.getCaseSysIdForTaskAssignee(userId));
}
With all of this in place, vendor replies are appended to the Work Notes as desired. The vendor's user account in ServiceNow is almost useless since it has a random 16 character password, and has no Roles.