Aare Pajuste
Tera Contributor

Hi All!
Since I didn't see any articles about replicating choice fields in a client script g_modal, I thought I'd make this after my efforts succeeded!

You'll need two things for this 1) AJAX Script Include 2) UI Action with Workspace tickbox enabled

AJAX Script Include:

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

    getChoices: function() {
        var elementsparm = String(this.getParameter('sysparm_elements'));
        var elements = elementsparm.split(",");
        var table = this.getParameter('sysparm_table');

        choicesObj = {};

        for (var i = 0; i < elements.length; i++) {

            var choiceRecord = new GlideRecord('sys_choice');
            choiceRecord.addEncodedQuery('name=' + table + '^element=' + elements[i] + '^inactive=false');
            choiceRecord.query();
            choiceRecord.setLimit(30);

            var name = elements[i];
            choicesObj[name] = {};
			var count = 0;
			
            while (choiceRecord.next()) {
				
                choicesObj[name][count] = {
					
                    displayValue: choiceRecord.label.toString(),
                    value: choiceRecord.value.toString()

                }
				count++;
            }
        }

        var result = JSON.stringify(choicesObj);
        return result;

    },
    type: 'getChoicesAJAX'
});

So this does the following:

a) gets the choices for table and element of your choice (example is incident)

b) creates a structured object with all that info (you can comma separate multiple elements like close_code,impact,priority,incident_state etc

c) creates a json string from this to send back to the front end

 

Client script:

function onClick(g_form) {

    var gA = new GlideAjax("getChoicesAJAX");
    gA.addParam('sysparm_elements', 'impact,close_code');
    gA.addParam('sysparm_table', 'incident');
    gA.addParam('sysparm_name', 'getChoices');
    gA.getXML(getChoices);

    function getChoices(response) {
        choicesObj = {};
        var answer = response.responseXML.documentElement.getAttribute("answer");
        choicesObj = JSON.parse(answer);


        impactChoices = [];
        closeCodeChoices = [];
        for (var key in choicesObj.impact) {

            if (choicesObj.impact.hasOwnProperty(key)) {

                var impactSet = {
                    displayValue: choicesObj.impact[key].displayValue,
                    value: choicesObj.impact[key].value
                };
                impactChoices.push(impactSet);

            }

        }
        for (var key in choicesObj.close_code) {
            if (choicesObj.close_code.hasOwnProperty(key)) {

                var closeCodeSet = {
                    displayValue: choicesObj.close_code[key].displayValue,
                    value: choicesObj.close_code[key].value
                };
                closeCodeChoices.push(closeCodeSet);

            }
        }

        var fields = [
			{
                type: 'choice',
                name: 'impact',
                label: getMessage('Impact'),
                value: g_form.getValue('impact'),
				displayValue: g_form.getDisplayValue('impact'),
                choices: impactChoices,
                mandatory: true
            },
            {
                type: 'choice',
                name: 'close_code',
                label: getMessage('Close code'),
                value: g_form.getValue('close_code'),
				displayValue: g_form.getDisplayValue('close_code'),
                choices: closeCodeChoices,
                mandatory: true
            },
			{
                type: 'textarea',
                name: 'work_notes',
                label: getMessage('Work notes'),
                mandatory: true
            },
			{
                type: 'reference',
                name: 'service_offering',
                label: getMessage('Service Offering'),
				value:  g_form.getValue('service_offering'),
				displayValue: g_form.getDisplayValue('service_offering'),
				reference: 'cmdb_ci_service',
				referringTable: 'incident',
				referringRecordId: g_form.getUniqueValue(),
                mandatory: true
            },
			
			{
                type: 'boolean',
                name: 'knowledge',
                label: getMessage('Knowledge - optional.'),
				value: g_form.getValue('knowledge')

            },
			
			
        ];
        g_modal.showFields({
            title: "Resolve Incident ticket",
			description: "test",
            fields: fields,
            size: 'xl'
        }).then(function(fieldValues) {
		
        g_form.setValue('impact', fieldValues.updatedFields[0].value);
		g_form.setValue('close_code', fieldValues.updatedFields[1].value);
		g_form.setValue('close_notes', fieldValues.updatedFields[2].value);

		g_form.setValue('service_offering', fieldValues.updatedFields[3].value);
			
		if (fieldValues.updatedFields[4].value != "") g_form.setValue('service_offering', fieldValues.updatedFields[4].value);
		if (fieldValues.updatedFields[5].value != "false") g_form.setValue('knowledge', fieldValues.updatedFields[5].value);
			
		g_form.setValue('incident_state', 6);
        g_form.save();
    });;
    }
}

What happens in the client script:
a) NB! This will only work in the callback function of the AJAX response. Otherwise outside of the callback the result will be unavailable. That's why the entire modals code is in it.

b) the GlideAjax requests for incident tables close_code,impact from sys_choice and then creates array objects out of those in the client script
c) we use those in the type choice fields in the fields array 

NB! in the fields youll have to specify the displayValue and Value separately 
d) the fields array is then used in the g_modal execution to form the modal
e) the ".then" is executed upon hitting submit on the modal 🙂

 

Hopefully the explanations make sense 🙂
Let me know if any questions

Comments
ryanlitwiller
Giga Guru

Great article this works beautifully in Rome. Unfortunately, it does not seem to work in San Diego, I have been unable to get any glideAjax to work on the San Diego workspace.

It is throwing a couple of errors:

find_real_file.png

find_real_file.png

Aare Pajuste
Tera Contributor

Hi Ryan! This seems to work fine on our San Diego instance. Are you sure the scope of your Script Include allows all applications? 🙂

Aare Pajuste
Tera Contributor

Actually @ryanlitwiller I think there's a bug with San Diego workspaces that is very difficult to pinpoint. The scope is sometimes lost and it's best that you use the "global" tag. This ensures it works.

global.getChoicesAJAX
Version history
Last update:
‎04-20-2022 08:03 AM
Updated by: