Help with script in getting choices show in the dropdown through Dictionary calculated value

Sam198
Mega Guru

Hi,

I have three choice field where dropdown 1 and dropdown 2 field choices remains same all the time, however, the dropdown 3 choices changes based on 1&2 - the addOption and removeOption does not work as it has too many choices. So, i thought use the "calculated value" section of the dictionary. I have created choices on the sys_choice table and using the "hint" and "dependent field" to sort which choice is related to dropdown 1 and dropdown 2. In the dropdown 3 field dictionary - I have below script to show the calculated value but it does not show the option:

 

(function calculatedFieldValue(current) {

// Add your code here
var choices = new GlideRecord('sys_choice');
//choices.addQuery('name', 'question_choice');
choices.addQuery('hint', current.u_type);//dropdown 1
choices.addQuery('dependent_value', current.u_category);//dropdown 2
choices.query();

var choicesArr = [];
while (choices.next()) {
choicesArr.push(choices.sys_id + '');
}

var returnQuery = 'sys_idIN' + choicesArr.join();
return returnQuery;

})(current);

 

This is what shown with above - Please help or should i use this as a script include and call through CS?

Sam198_0-1686745579125.png

 

1 REPLY 1

Sam198
Mega Guru

I tried with Script include and below CS - but does not work:

Script include:

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

MyFunction: function() {
var choices = new GlideRecord('sys_choice');
//choices.addQuery('name', 'question_choice');
choices.addQuery('hint', current.u_type); //dropdown 1
choices.addQuery('dependent_value', current.u_category); //dropdown 2
choices.query();

var choicesArr = [];
while (choices.next()) {
choicesArr.push(choices.name + '');
}
return JSON.stringify(choicesArr);
},
type: 'IssueChoices'
});

 

I only need this as Onload of the form - so the Client script onLoad is below:

function onLoad() {
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('IssueChoices');
ga.addParam('sysparm_name', g_form.getValue('u_type'));
ga.addParam('sysparm_category', g_form.getValue('u_category'));
ga.getXML(MyFunction);

}

function MyFunction(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
// alert('Answer is: '+answer);
answer = answer.slice(0, -1);
var inAnswer = answer.split(';');
var outData = '';
for (var i = 0; i < inAnswer.length; i++) {
outData = outData + '\n' + inAnswer[i].toString();
g_form.addOption('u_issue_new', inAnswer[i].toString(), inAnswer[i].toString());
}


}