How to add Choice values

surajsironi
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];
                       
                    }
                }
            }
        }

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


       
    });
}

 

Not applicable

Hi @Sironi ,

You can do this in the client with syntax like:

var arr = ['value1','value2','value3'];

for(var i=0; i< arr.length; i++){
g_form.addOption('field_name', arr[i]);
}

You can use the addOption method to add a choice value and position as well but you would need more data available in your array.

You can also refer to this thread:

https://community.servicenow.com/community?id=community_question&sys_id=162a35a3db600810190dfb24399619bc

 

Mark my answer correct & Helpful, if Applicable.

Thanks,

Sandeep

HI Sandeep,

this is Workspace client script and we are creating field in Workspace g_modal form only. 

if we use below line it will check the field from interaction form right ?. 

g_form.addOption('field_name', arr[i]);

 

But we need to push values into workspace field .

type: 'choice',
name: 'invoice_type',
label: getMessage('invoice type'),
value: getMessage(' -- Select -- '),
choices: [{
displayValue: '',
value: ''
}

Ankur Bawiskar
Tera Patron

Hi,

from where are those choices coming?

will it be always those 4 choices?

why not add them 1 by 1? why for loop is required?

Regards
Ankur

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

from where are those choices coming? : From script-Include

will it be always those 4 choices? : values coming from Script-include so in future values may increase or decrease as per Query condition in script include.

why not add them 1 by 1? why for loop is required? : values coming from script include.