The CreatorCon Call for Content is officially open! Get started here.

Sys id to display value in BR

ELiv
Tera Contributor

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);

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

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(', ');

 

 

View solution in original post

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

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(', ');

 

 

Hey, thanks for the reply. I'm getting some errors and not sure why. I moved the code to scripts - background to see what's going on. If I figure it out I'll update.

 

 

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

 

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.