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.

Unable to rerun script when selecting reference item

user_ms
Tera Expert

When a record is selected from a reference field, a script is executed to copy each record's value.
If you select the reference item again, the script does not run.
Do you know why?
Do I need some other script to run the script again?
Again, if you know how the script is executed when I select the reference item, please let me know.

4 REPLIES 4

Dhananjay Pawar
Kilo Sage

Hi,

I believe you are handling this using onChange client script and Script inlcude. 

Please share your code here.

Thanks,

Dhananjay.

user_ms
Tera Expert
//script include

var test1 = Class.create();
test1.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    copy: function() {
        var set = this.getParameter('sysparm_set');

        var tb1 = new GlideRecord("tb_1");
        if (!tb1.get(tb1)) {
            return '';
        }

		var tb2 = new GlideRecord("tb_2");
        tb2.get('data2', tb1.test2);

		var tb3 = new GlideRecord("tb_3");
		tb3.get('data3', tb1.test3);

        var data = {
			'item_1': tb1.value_1.toString(),
			'item_2': tb1.value_2.toString(),
			'item_3': tb1.value_3.toString(),
			'item_4': tb1.value_4.toString(),
			'item_5': tb1.value_5.toString(),
			'item_6': tb1.value_6.toString(),
			'item_7': tb1.value_7.toString(),
			'item_8': tb1.value_8.toString(), 
			'item_10': tb2.sys_id.toString(),
			'item_11': tb3.sys_id.toString(),
        };
        return JSON.stringify(data);
    },

});

//clientscript

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

    var ga = new GlideAjax('x_test.test1');
    ga.addParam('sysparm_name', 'copy');
    ga.addParam('sysparm_set', newValue);
    ga.getXMLAnswer(function(response) {
        var data = JSON.parse(response);
        if (data) {
			g_form.setValue('item_1', data.item_1);
			g_form.setValue('item_2', data.item_2);
			g_form.setValue('item_3', data.item_3);
			g_form.setValue('item_4', Number(data.item_4.slice(0,2)));
			g_form.setValue('item_5', data.item_5);
			g_form.setValue('item_6', data.item_6);
			g_form.setValue('item_7', data.item_7);
			g_form.setValue('item_8', data.item_8);
			g_form.setValue('item_9', 0);
			g_form.setValue('item_10', data.item_10);
			g_form.setValue('item_11', data.item_11);

		}
    });   
}

@Dhananjay Pawar 

Thank you for your reply.

Here is the script.