How to add Choice values

Sironi
Kilo Sage

Hi All,

Please suggest me how to add choice values from array.

find_real_file.png

Array CHOICEVALUES[value,label]

[chat,Chat,voice_call,Voice Call,walk-in,Walk In,email_fire,Email Fire]

 

Issue : Now How to add choices from Workspace client script using with for-loop

function onClick(g_form) {

    var fields = [{
        type: 'choice',
        name: 'invoice_type',
        label: getMessage('invoice type'),
        value: getMessage(' -- Select -- '),
        choices: [{
            displayValue: '',
            value: ''
        }],
        mandatory: true
    }, {
        type: 'string',
        label: getMessage('Invoicer'),
        value: g_form.getDisplayValue('consumer'),
        readonly: true,
    }];

    g_modal.showFields({
        title: "Product for Invoice",
        fields: fields,
        size: 'lg'
    }).then(function(fieldValues) {
        g_form.setValue('short_description', fieldValues.updatedFields[1].value);
        //<--------------------------------------------------------------------------------------->//	
        var ga = new GlideAjax('CDAInvoiceProcessor');
        ga.addParam('sysparm_name', 'getInvoiceTypes');
        ga.getXML(populateChoiceeField);

        function populateChoiceeField(response) {
            var msg = response.responseXML.documentElement.getAttribute('answer').toString();
            if (msg != '') {
                msg = msg.split(',');
                if (msg.length > 0) {
                    g_form.clearOptions(fieldValues.updatedFields[0].name);
                    g_form.addOption(fieldValues.updatedFields[0].name, '', '-- None --');
                    for (var i = 0; i < msg.length; i = i + 2) {
						fieldValues.updatedFields[0].value= msg[i];
						fieldValues.updatedFields[0].displayValue= msg[i+1];
                       
                    }
                }
            }
        }

        //<---------------------------------------------------------------------------------------->//	


       
    });
}

 

1 ACCEPTED SOLUTION

Hi,

if you wish to add None then do this

Script Include:

var arr = [];

// to add None
arr.push({
	"value":"",
	"displayValue":"-- None --"
});

var DWN = new GlideRecord("u_customer");
DWN.addQuery("u_active", true);
DWN.addQuery("u_name", "Customer Specific Type");
DWN.addQuery('u_customer',' b70c0f830f37330013a831ef68767e33');
DWN.query();
if (DWN.next()) {
	PARMS = DWN.u_parms.split(',');
	gs.info("PARMS " + PARMS.length + "--" + PARMS);
	for (var i = 0; i < PARMS.length; i++) {
		var obj = {};
		var choice = new GlideRecord('sys_choice');
		choice.addQuery('name', 'incident');
		choice.addQuery('language', 'en');
		choice.addQuery('element', 'contact_type');
		choice.addQuery('inactive', false);
		choice.addQuery('value', PARMS[i]);
		choice.query();
		if (choice.next()) {
			obj["value"] = choice.value.toString();
			obj["displayValue"] = choice.label.toString();
			arr.push(obj);
		}
	}
}

var result = {};
result["choices"] = arr;

return JSON.stringify(result);
},

Workspace client script:

function onClick(g_form) {

	var fields = [];
	fields.push({
		type: 'string',
		label: getMessage('Invoicer'),
		value: g_form.getDisplayValue('consumer'),
		readonly: true,
	});

	var ga = new GlideAjax('CDAInvoiceProcessor');
	ga.addParam('sysparm_name', 'getInvoiceTypes');
	ga.getXML(populateChoiceeField);
	ga.getXMLAnswer(populateChoiceeField);

	function populateChoiceeField(response) {
		var msg = response;
		var answer = JSON.parse(msg);
		fields.push({
			type: 'choice',
			name: 'invoice_type',
			label: getMessage('invoice type'),
			choices: answer.choices,
			mandatory: true
		});

		g_modal.showFields({
			title: "Product for Invoice",
			fields: fields,
			size: 'lg'
		}).then(function(fieldValues) {
			g_form.setValue('short_description', fieldValues.updatedFields[1].value);
			//<--------------------------------------------------------------------------------------->//	


			//<---------------------------------------------------------------------------------------->//	

		});
	}
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

20 REPLIES 20

Hi,

what are you returning from script include?

are you returning array OR array of JSON object containing label and value?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

script-include returning array

[chat,Chat,voice_call,Voice Call,walk-in,Walk In,email_fire,Email Fire]

 

var DWN = new GlideRecord("u_customer");
DWN.addQuery("u_active", true);
DWN.addQuery("u_name", "Customer Specific Type");
DWN.addQuery('u_customer',' b70c0f830f37330013a831ef68767e33');
DWN.query();
if (DWN.next()) {
PARMS = DWN.u_parms.split(',');
gs.info("PARMS " + PARMS.length + "--" + PARMS);
for (var i = 0; i < PARMS.length; i++) {
var choice = new GlideRecord('sys_choice');
choice.addQuery('name', 'incident');
choice.addQuery('language', 'en');
choice.addQuery('element', 'contact_type');
choice.addQuery('inactive', false);
choice.addQuery('value', PARMS[i]);
choice.query();
if (choice.next()) {
value = choice.value.toString();
label = choice.label.toString();
CHOICEVALUES.push([value, label]);
}
}
}

return CHOICEVALUES.toString();
},

Hi,

update as this and it should work

share the updates

Script Include:

var arr = [];
var DWN = new GlideRecord("u_customer");
DWN.addQuery("u_active", true);
DWN.addQuery("u_name", "Customer Specific Type");
DWN.addQuery('u_customer',' b70c0f830f37330013a831ef68767e33');
DWN.query();
if (DWN.next()) {
	PARMS = DWN.u_parms.split(',');
	gs.info("PARMS " + PARMS.length + "--" + PARMS);
	for (var i = 0; i < PARMS.length; i++) {
		var choice = new GlideRecord('sys_choice');
		choice.addQuery('name', 'incident');
		choice.addQuery('language', 'en');
		choice.addQuery('element', 'contact_type');
		choice.addQuery('inactive', false);
		choice.addQuery('value', PARMS[i]);
		choice.query();
		if (choice.next()) {
			var obj = {};
			obj["value"] = choice.value.toString();
			obj["displayValue"] = choice.label.toString();
			arr.push(obj);
		}
	}
}

var result = {};
result["choices"] = arr;

return JSON.stringify(result);
},

Workspace client script:

function onClick(g_form) {

	var fields = [];
	fields.push({
		type: 'string',
		label: getMessage('Invoicer'),
		value: g_form.getDisplayValue('consumer'),
		readonly: true,
	});

	var ga = new GlideAjax('CDAInvoiceProcessor');
	ga.addParam('sysparm_name', 'getInvoiceTypes');
	ga.getXML(populateChoiceeField);
	function populateChoiceeField(response) {
		var msg = response.responseXML.documentElement.getAttribute('answer').toString();

		var answer = JSON.parse(msg);

		fields.push({
			type: 'choice',
			name: 'invoice_type',
			label: getMessage('invoice type'),
			value: (answer.choices && answer.choices.length > 0) ? answer.choices[0].value : '',
			choices: answer.choices,
			mandatory: true
		});

		g_modal.showFields({
			title: "Product for Invoice",
			fields: fields,
			size: 'lg'
		}).then(function(fieldValues) {
			g_form.setValue('short_description', fieldValues.updatedFields[1].value);
			//<--------------------------------------------------------------------------------------->//	


			//<---------------------------------------------------------------------------------------->//	

		});
	}
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

i have applied your script in Onchange client script first to check what values it returing , It is Returning "Null"  , but not sure what was the error.

find_real_file.png

g_modal window not opening , i have tried multple times

find_real_file.png

Hi,

did you debug?

what came in alert? share that

update as this

function onClick(g_form) {

	var fields = [];
	fields.push({
		type: 'string',
		label: getMessage('Invoicer'),
		value: g_form.getDisplayValue('consumer'),
		readonly: true,
	});

	var ga = new GlideAjax('CDAInvoiceProcessor');
	ga.addParam('sysparm_name', 'getInvoiceTypes');
	ga.getXML(populateChoiceeField);
	ga.getXMLAnswer(populateChoiceeField);
	
	function populateChoiceeField(response) {
		alert(response);
		var msg = response;

		var answer = JSON.parse(msg);

		fields.push({
			type: 'choice',
			name: 'invoice_type',
			label: getMessage('invoice type'),
			value: (answer.choices && answer.choices.length > 0) ? answer.choices[0].value : '',
			choices: answer.choices,
			mandatory: true
		});

		g_modal.showFields({
			title: "Product for Invoice",
			fields: fields,
			size: 'lg'
		}).then(function(fieldValues) {
			g_form.setValue('short_description', fieldValues.updatedFields[1].value);
			//<--------------------------------------------------------------------------------------->//	


			//<---------------------------------------------------------------------------------------->//	

		});
	}
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader