Ankur Bawiskar
Tera Patron
Tera Patron

Many a times there is a need to show dynamic choices in workspace form pane choice type field along with other fields.

Supported fields in Workspace form panes

There are different field types such as String, Date, Date/Time, Email etc.

One of those type is Choice where I will be adding the choices dynamically using GlideAjax.

Here is a sample UI action script which would fetch the choices from Change Request table and close_code field. I am also showing the Assignment Group field value on form pane.

You can refer this and enhance as per your requirement.

The key here is forming the JSON object containing the choice value and choice label.

UI Action:

Workspace Client Script:

function onClick(g_form) {

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

	var ga = new GlideAjax('GetMyChoices');
	ga.addParam('sysparm_name', 'getFieldChoices');
	ga.getXMLAnswer(function(answer1){
		var answer = JSON.parse(answer1);
		fields.push({
			type: 'choice',
			name: 'close_code',
			label: getMessage('Close code'),
			choices: answer.choices,
			mandatory: true
		});

		g_modal.showFields({
			title: "Choices for Change Request",
			fields: fields,
			size: 'lg'
		}).then(function(fieldValues) {

		});					

	});
}

UI action Configuration:

find_real_file.png

Script Include: Client callable

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

	getFieldChoices: function(){

		var arr = [];
		// use this to add None if you wish to show when choices you are adding doesn't have it
		/*arr.push({
			"value":"",
			"displayValue":"-- None --"
		});
		*/

		// you can even iterate sys_choice and get the choice labels and values
		var choices = GlideChoiceList.getChoiceList("change_request", "close_code");
		for (var i = 0; i < choices.getSize(); i++){
			var value = choices.getChoice(i).getValue();
			var label = choices.getChoice(i).getLabel();
			var obj = {};
			obj["value"] = value.toString();
			obj["displayValue"] = label.toString();
			arr.push(obj);
		}
		var result = {};
		result["choices"] = arr;
		return JSON.stringify(result);
	},

	type: 'GetMyChoices'
});

Output: It is showing choices from close_code field of Change Request

find_real_file.png

Thanks for reading the blog and do provide your inputs/suggestions if any.

Hope you find this article helpful. Don’t forget to Mark it Helpful, Bookmark.
Thanks,
Ankur Bawiskar

ServiceNow MVP 2022,2021,2020,2019,2018

Developer MVP 2021

My Articles & Blogs

 

10 Comments