- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2023 05:44 PM - edited ‎06-14-2023 08:44 PM
Hi all,
I have three dropdown field - the data is mapped from record producer > however, on the dropdown 3 I need to only show options (onload) based on dropdown 1 and dropdown 2 - all are choice type field and data is sitting on sys_choice table. I tried with below script include and client script but need help in fiixng it as i am not sure how to pass two param:
I tried with Script include and below CS - but does not work - Updated script below:
Script include:
I only need this as Onload of the form - so the Client script onLoad is below:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2023 09:36 PM
HI @Sam198 ,
I trust you are doing great.
To achieve this, you can follow the steps below:
- Create a Script Include:
- Navigate to "System Definition" -> "Script Includes" in ServiceNow.
- Create a new Script Include with the name "IssueChoices".
- Use the following code:
var IssueChoices = Class.create();
IssueChoices.prototype = Object.extendsObject(AbstractAjaxProcessor, {
MyFunction: function() {
var choices = new GlideRecord('sys_choice');
var dropdown1 = this.getParameter('sysparm_type');
var dropdown2 = this.getParameter('sysparm_category');
choices.addQuery('name', 'question_choice');
choices.addQuery('dependent_value', dropdown1); // Dropdown 1
choices.addQuery('dependent_field', dropdown2); // Dropdown 2
choices.query();
var choicesArr = [];
while (choices.next()) {
choicesArr.push(choices.value + '');
}
return JSON.stringify(choicesArr);
},
type: 'IssueChoices'
});
- Create a Client Script:
- Navigate to "System Definition" -> "Client Scripts" in ServiceNow.
- Create a new Client Script and set the "Applies to" field to the appropriate table or form.
- Set the "Type" field to "onLoad" to execute the script when the form loads.
- Use the following code:
function onLoad() {
var ga = new GlideAjax('IssueChoices');
ga.addParam('sysparm_name', 'MyFunction');
ga.addParam('sysparm_category', g_form.getValue('dropdown_field1'));
ga.addParam('sysparm_type', g_form.getValue('dropdown_field2'));
ga.getXML(MyFunction);
}
function MyFunction(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = answer.slice(0, -1);
var inAnswer = answer.split(';');
for (var i = 0; i < inAnswer.length; i++) {
g_form.addOption('dropdown_field3', inAnswer[i], inAnswer[i]);
}
}
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2023 09:36 PM
HI @Sam198 ,
I trust you are doing great.
To achieve this, you can follow the steps below:
- Create a Script Include:
- Navigate to "System Definition" -> "Script Includes" in ServiceNow.
- Create a new Script Include with the name "IssueChoices".
- Use the following code:
var IssueChoices = Class.create();
IssueChoices.prototype = Object.extendsObject(AbstractAjaxProcessor, {
MyFunction: function() {
var choices = new GlideRecord('sys_choice');
var dropdown1 = this.getParameter('sysparm_type');
var dropdown2 = this.getParameter('sysparm_category');
choices.addQuery('name', 'question_choice');
choices.addQuery('dependent_value', dropdown1); // Dropdown 1
choices.addQuery('dependent_field', dropdown2); // Dropdown 2
choices.query();
var choicesArr = [];
while (choices.next()) {
choicesArr.push(choices.value + '');
}
return JSON.stringify(choicesArr);
},
type: 'IssueChoices'
});
- Create a Client Script:
- Navigate to "System Definition" -> "Client Scripts" in ServiceNow.
- Create a new Client Script and set the "Applies to" field to the appropriate table or form.
- Set the "Type" field to "onLoad" to execute the script when the form loads.
- Use the following code:
function onLoad() {
var ga = new GlideAjax('IssueChoices');
ga.addParam('sysparm_name', 'MyFunction');
ga.addParam('sysparm_category', g_form.getValue('dropdown_field1'));
ga.addParam('sysparm_type', g_form.getValue('dropdown_field2'));
ga.getXML(MyFunction);
}
function MyFunction(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = answer.slice(0, -1);
var inAnswer = answer.split(';');
for (var i = 0; i < inAnswer.length; i++) {
g_form.addOption('dropdown_field3', inAnswer[i], inAnswer[i]);
}
}
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi