Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

changes() function is not working while giving it with a variable name

sruthi22
Tera Contributor

Hi All,

 

We have a requirement where we need to see if a field value is changed in business rule (using .changes()). But we cant give the field name directly, instead should get field name from a property. And this is giving us 'undefined'. Please find below a similar code snippet 

 

var gr = new GlideRecord('sn_customerservice_case');

if(gr.get(sys_id))

{

var field = 'number';

gs.info(gr.field)

}

 

 

And this gives undefined as a response. Is there a way to achieve this?

 

Thanks in advance,

Sruthi.

2 REPLIES 2

Vanderlei
Mega Sage

Hi @sruthi22 you can try this code

var field_names = ['number', 'short_description']

field_names.forEach(function(field_name){
gs.info(current[field_name]t.getLabel()+" changes? "+current[field_name].changes())
})

 

or this

 

var field_names = ['number', 'short_description']

field_names.forEach(function(field_name){
var element = current.getElement(field_name)
gs.info(element.getLabel()+" changes? "+element.changes())
})

 

Brad Bowman
Kilo Patron
Kilo Patron

Either of these methods should log the value of the number field on the returned record, assuming sys_id is supplied/defined:

var gr = new GlideRecord('sn_customerservice_case');
if (gr.get(sys_id)) {
    var field = 'number';
    gs.info(gr.getValue(field));
}

var gr = new GlideRecord('sn_customerservice_case');
if (gr.get(sys_id)) {
    var field = 'number';
    gs.info(gr[field]);
}

var gr = new GlideRecord('sn_customerservice_case');
if (gr.get(sys_id)) {
    var field = 'number';
    gs.info(gr.getElement(field));
}