Create choices from Global catItem for Custom Scoped App table

triciav
Kilo Sage

I am having an issue with creating choices from my script include for the Choice field on my custom table.

I have a Customer table with a field called event_name that is a choice type

I have a catItem that has a string field for the Event Name

When that field changes I have a GlideAjax Client script running a Script include to check if the name exists, if it does not exist create it. It is not creating the Choice in the Global Sys_Choice table.

If I go to sys_choice table from the Global Scope I do not see my table as a selection.

I have to change to my Scope and then I can se my table from sys_choice.

How can I do this???

I need to dynamically create choices if they do not already exist.

Thank you

 

CatItem is in Global

Catalog Client Script onChange of event_name variable

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('ChoiceAvailable');//x_bookings.ChoiceAvailable
ga.addParam('sysparm_name', 'checkChoice');
ga.addParam('sysparm_field', g_form.getValue("event_name").toString());
ga.addParam('sysparm_table', "x_bookings_reservation");
ga.addParam('sysparm_fieldLabel', g_form.getValue("event_name").toString());
ga.getXML(getresponse);
}


function getresponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer) {
g_form.clearValue('event_name');

}
}

Script Include (Global) Accessible from All application scopes

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

checkChoice: function() {
var event = this.getParameter('sysparm_field').toString();
// var table = this.getParameter('sysparm_table');
var table = 'x_bookings_reservation';
var label = this.getParameter('sysparm_fieldLabel').toString();
var elm = 'event_name';

//x_bookings.x_btig_bookings_reservation
gs.info("TRICIA these are the parms "+event +" table "+table+" label "+label+" elm"+elm);

This is in the Log TRICIA these are the parms Really me table x_bookings_reservation label Really me elmevent_namegs.info("TRICIA EVENT = " + event + " " + table);

var choicesGR = new GlideRecord('sys_choice');
choicesGR.addQuery('label', label);
choicesGR.addQuery('value', event);
choicesGR.addQuery('table', table);
choicesGR.addQuery('element', elm);
choicesGR.query();


gs.info("TRICIA CHOICES = " + choicesGR + " " + choicesGR.label);
if (!choicesGR.next()) {

var newChoice = new GlideRecord('sys_choice');
newChoice.newRecord();
newChoice.name = table;
newChoice.value = event;
newChoice.label = label;
newChoice.language = "en";
newChoice.element = elm;
newChoice.insert();

}
},
type: 'ChoiceAvailable'
});

 

 

 

find_real_file.png

3 REPLIES 3

Anil Lande
Kilo Patron

Hi,

Please check below configurations as well. Open Choice table configuration (dictionary) and allow create and Update (check highlighted checkbox). Also make sure A

find_real_file.png

 

If needed you may need to create some cross scope access policies.

https://community.servicenow.com/community?id=community_question&sys_id=4db8c361db5cdbc01dcaf3231f96...

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

I have done all the above if I am in the Booking scope it works

If I am in Global Scope it does not

The end users will be in GLobal scope so I am not sure what I am missing

find_real_file.png

Hi,

Please check sys_choice table and see if those choices are getting created in that table.

I tested your script on PDI from Global scope. After adding permissions on sys_choice table configuration it started working.

Also you can try creating same script include in Booking Application scope and make it accessible from all scope. You can call scoped app script include by using API name.

The scoped app script include would be having definition like below:

SI defination:

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

checkChoice: function() {

//your logic

},

 

and call it in client script like:

var ga = new GlideAjax('x_bookings.ChoiceAvailable'); // use API name of Script include here
ga.addParam('sysparm_name', 'checkChoice');

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande