Help with script to show relevant choices onload based on two fields

Sam198
Mega Guru

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:

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('hint', dropdown1); //dropdown 1
        choices.addQuery('dependent_value', dropdown2); //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', 'MyFunction');
    ga.addParam('sysparm_category', g_form.getValue('u_category'));
ga.addParam('sysparm_type', g_form.getValue('u_type'));
    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());
    }
 
 
}
1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

HI @Sam198 ,
I trust you are doing great.

To achieve this, you can follow the steps below:

  1. 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'
});

 

  1. 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



View solution in original post

1 REPLY 1

Amit Gujarathi
Giga Sage
Giga Sage

HI @Sam198 ,
I trust you are doing great.

To achieve this, you can follow the steps below:

  1. 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'
});

 

  1. 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