- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2022 04:40 PM
Hi,
I am using a before business rule to post changes to variables on sc_req_item to the activity log the code works but posts sys_id and I am looking to show the values. Any suggestions?
Code:
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
if (previous.value != current.value) {
var var_owner = new GlideRecord('sc_item_option_mtom');
var_owner.addQuery('sc_item_option', current.sys_id);
var_owner.query();
if (var_owner.next()) {
var itm = new GlideRecord('sc_req_item');
itm.addQuery('sys_id', var_owner.request_item);
itm.query();
if (itm.next()) {
itm.comments = 'Variable updated: ' + '"' + current.item_option_new.getDisplayValue() + '"' + '<br/><br/>' + ' Value changed FROM: ' + previous.value.getDisplayValue() + '<br/><br/>' + ' TO: ' + current.value.getDisplayValue() + '<br/><br/>' + ' BY: ' + gs.getUserDisplayName();
itm.update();
}
}
}
})(current, previous);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2022 03:15 AM - edited 11-18-2022 08:39 AM
It seems like there's a better way to do this, but going with what you have started, this will work too. I'm assuming the BR is running on the sc_item_option table. You can change your itm results block to this:
if (itm.next()) {
var pVal = previous.value.getDisplayValue();
var cVal = current.value.getDisplayValue();
if (current.item_option_new.type == "8" ) { //Reference
var pref = new GlideRecord(current.item_option_new.reference);
if (pref.get(previous.value) {
pVal = pref.getDisplayValue(); //this will work here because you're getting it from the table record, not the value in an unrelated table
}
var cref = new GlideRecord(current.item_option_new.reference);
if (cref.get(current.value) {
cVal = cref.getDisplayValue();
}
}
itm.comments = 'Variable updated: ' + '"' + current.item_option_new.getDisplayValue() + '"' + '<br/><br/>' + ' Value changed FROM: ' + pVal + '<br/><br/>' + ' TO: ' + cVal + '<br/><br/>' + ' BY: ' + gs.getUserDisplayName();
itm.update();
}
You'll have to do something similar for any List Collector variables, but since this will be a comma-separated list of sys_ids then the GR would look more like this:
var prefArr = [];
var pref = new GlideRecord(current.item_option_new.reference);
pref.addQuery('sys_id', 'IN', previous.value);
pref.query();
while (pref.next()) {
prefArr.push(pref.getDisplayValue());
}
pVal = prefArr.join(', ');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2022 03:15 AM - edited 11-18-2022 08:39 AM
It seems like there's a better way to do this, but going with what you have started, this will work too. I'm assuming the BR is running on the sc_item_option table. You can change your itm results block to this:
if (itm.next()) {
var pVal = previous.value.getDisplayValue();
var cVal = current.value.getDisplayValue();
if (current.item_option_new.type == "8" ) { //Reference
var pref = new GlideRecord(current.item_option_new.reference);
if (pref.get(previous.value) {
pVal = pref.getDisplayValue(); //this will work here because you're getting it from the table record, not the value in an unrelated table
}
var cref = new GlideRecord(current.item_option_new.reference);
if (cref.get(current.value) {
cVal = cref.getDisplayValue();
}
}
itm.comments = 'Variable updated: ' + '"' + current.item_option_new.getDisplayValue() + '"' + '<br/><br/>' + ' Value changed FROM: ' + pVal + '<br/><br/>' + ' TO: ' + cVal + '<br/><br/>' + ' BY: ' + gs.getUserDisplayName();
itm.update();
}
You'll have to do something similar for any List Collector variables, but since this will be a comma-separated list of sys_ids then the GR would look more like this:
var prefArr = [];
var pref = new GlideRecord(current.item_option_new.reference);
pref.addQuery('sys_id', 'IN', previous.value);
pref.query();
while (pref.next()) {
prefArr.push(pref.getDisplayValue());
}
pVal = prefArr.join(', ');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2022 07:52 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2022 02:59 PM
There seems to be a value missing in the script, can you tell me what this is supposed to be equal to?
if (current.item_option_new.type == { //Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2022 04:37 AM
LOL my value in the script I posted was auto-replaced with an emoji for some reason. I used '8' which is the value for the type of reference out of the box.