Pass the choice list value from script include to client script. Display them on a choice list

Community Alums
Not applicable

I am trying to pass the value (dummy data made up by me '1') in the Script Include which is stored in an array. Then called into the Client Script where I am trying to loop according to the number of values(records table-script include). These values are passed to the choice list. The issue is that it displays only a single choice list option even though in the client script I have used the loop as mentioned above.

 

The scripts:

----------------Script Include---------------------

 

 

var DecomissionScriptInclude = Class.create();
DecomissionScriptInclude.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getChoices: function (){
		
		var record = new GlideRecord('u_cmdb_ci_info_system');
		record.query();
		var array = [];
		while(record.next()){
			
			array.push(1,1).toString();
		} 
		return array.toString();
	},
    type: 'DecomissionScriptInclude'
});

 

 

--------------Client Script------------------

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
  
    
    g_form.clearOptions('test');
    var script_include = new GlideAjax('DecomissionScriptInclude');
    script_include.addParam('sysparm_name', 'getChoices');
    script_include.getXMLAnswer(handle);

     function handle(answer) {
         var msg = answer.split(',');
        alert(msg);
          for (var i = 0; i < msg.length; i++) {
            g_form.addOption('test', msg[i],msg[i]);

        }
    }
}

 

Result output: 

choice list value servicenow.PNG

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Try something more like this to push the label and value as an object to the array, am I'm assuming these will be values from the GR, not always 1,1 for every choice.  I don't know if it only shows unique values if you add multiple options/choices with the same value, so test with unique values.

var DecomissionScriptInclude = Class.create();
DecomissionScriptInclude.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getChoices: function (){
		
        var optionsArr = [];
        var json = new JSON();		
        var record = new GlideRecord('u_cmdb_ci_info_system');
        record.query();
        while (record.next()) {
	    optionsArr.push({
                'label' : '1',  // record.field_name.toString(),
                'value' : '1'
            });
        } 
	return json.encode(optionsArr);
	},
    type: 'DecomissionScriptInclude'
});

Then your Client Script needs to parse the answer and use the object members like this:

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

    g_form.clearOptions('test');
    var script_include = new GlideAjax('DecomissionScriptInclude');
    script_include.addParam('sysparm_name', 'getChoices');
    script_include.getXMLAnswer(handle);

    function handle(answer) {
        var msgObj = JSON.parse(answer);
        g_form.addOption('test', '', ' -- None --');
        for (var i=0; i<msgObj.length; i++) {
            g_form.addOption('test', msgObj[i].value, msgObj[i].label);
        }
    }
}

 

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Try something more like this to push the label and value as an object to the array, am I'm assuming these will be values from the GR, not always 1,1 for every choice.  I don't know if it only shows unique values if you add multiple options/choices with the same value, so test with unique values.

var DecomissionScriptInclude = Class.create();
DecomissionScriptInclude.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getChoices: function (){
		
        var optionsArr = [];
        var json = new JSON();		
        var record = new GlideRecord('u_cmdb_ci_info_system');
        record.query();
        while (record.next()) {
	    optionsArr.push({
                'label' : '1',  // record.field_name.toString(),
                'value' : '1'
            });
        } 
	return json.encode(optionsArr);
	},
    type: 'DecomissionScriptInclude'
});

Then your Client Script needs to parse the answer and use the object members like this:

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

    g_form.clearOptions('test');
    var script_include = new GlideAjax('DecomissionScriptInclude');
    script_include.addParam('sysparm_name', 'getChoices');
    script_include.getXMLAnswer(handle);

    function handle(answer) {
        var msgObj = JSON.parse(answer);
        g_form.addOption('test', '', ' -- None --');
        for (var i=0; i<msgObj.length; i++) {
            g_form.addOption('test', msgObj[i].value, msgObj[i].label);
        }
    }
}

 

Community Alums
Not applicable

Thanks, @Brad Bowman  it works like a charm. I also tested with actual real-value GR, which functioned as I wanted. 

You are welcome.