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.

Script Include does not change field value

Alon Grod
Tera Expert

Hi,

 

I have the field u_record_changed on incident table. I have a Business Rule that should change the value of the field u_record_change to True on Update.

I have this Script Include: (Even though Im getting the logs Alon1 and Alon2, the field value stay on True but it should be False) Does anyone know why?

var SetRecordChangedFalse = Class.create();

SetRecordChangedFalse.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isHamal: function() {

        var std = this.getParameter('sysparm_std');
        var num = this.getParameter('sysparm_num');

        var standardFields = new GlideRecord('sys_user_grmember');
        standardFields.addQuery('user', std);
        standardFields.query();

        while (standardFields.next()) {

            var group_name = standardFields.group.toString();

            var gr = new GlideRecord('sys_user_group');
            gr.addEncodedQuery('sys_id=' + group_name + '^u_hamal=true');
            gr.query();
            if (gr.next()) {
                gs.log('ALon1');
                var inc = new GlideRecord('incident');
                inc.addEncodedQuery('sys_id=' + num);
                inc.query();
                if (inc.next()) {
                    gs.log('Alon2');
                    inc.setValue('u_record_changed', false);
                    inc.update();
                }
            }
        }

    },
    type: 'SetRecordChangedFalse'
});
6 REPLIES 6

Brad Bowman
Kilo Patron
Kilo Patron

I would add to the log to confirm that the incident record that you expect/are checking is the one being updated, and maybe try a different way of setting the field value:

if (inc.next()) {
    gs.log('Alon2 inc numb ' + inc.number);
    inc.u_record_changed = false;
    inc.update();
}

If it still doesn't work, confirm of course that u_record_changed is exactly the name of the field, and it is a True/False type, then try inc.u_record_changed = 'false';

@Brad Bowman  Hi, unfortunately im still getting this field as true after checking and adjusting everything. 

AnveshKumar M
Tera Sage
Tera Sage

Hi @Alon Grod ,

Can you try wrapping the update block in try..Catch.. like below.

 

if (inc.next()) {
	gs.log('Alon2');
	try{
		inc.setValue('u_record_changed', false);
		inc.update();
	} catch(ex) {
		gs.log('INC Debug: ' + ex.message);
	}
}

 

 

Thanks,
Anvesh

@AnveshKumar M  hi, i wrote it like this and Im just getting Alon1 and Alon2 in log

var SetRecordChangedFalse = Class.create();

SetRecordChangedFalse.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isHamal: function() {

        var std = this.getParameter('sysparm_std');
        var num = this.getParameter('sysparm_num');

        var standardFields = new GlideRecord('sys_user_grmember');
        standardFields.addQuery('user', std);
        standardFields.query();

        while (standardFields.next()) {

            var group_name = standardFields.group.toString();

            var gr = new GlideRecord('sys_user_group');
            gr.addEncodedQuery('sys_id=' + group_name + '^u_hamal=true');
            gr.query();
            if (gr.next()) {
                gs.log('ALon1');
                var inc = new GlideRecord('incident');
                inc.addEncodedQuery('sys_id=' + num);
                inc.query();
                if (inc.next()) {
                    
                    try {
						gs.log('Alon2');
                        inc.setValue('u_record_changed', false);
                        inc.update();
                    } catch (ex) {
                        gs.log('INC Debug: ' + ex.message);
                    }
                }
            }
        }

    },
    type: 'SetRecordChangedFalse'
});