Pass values from "List Type" field from client script to script include

Aaviii
Tera Contributor

I have a requirement to pass value from onChange client script for list type field to script include. Am using the below code but getting undefined value.

 

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var ga = new GlideAjax('Test');
    ga.addParam('sysparm_name', 'getChoices');
    ga.addParam('sysparm_first_value', newValue);
    ga.getXML(callback);

    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var sysIds = JSON.parse(answer);
        if (sysIds.length > 0) {
            var query = 'sys_idIN' + sysIds.join(',');
            g_form.setReferenceQualifier('u_1', query);
        } else {
            g_form.setReferenceQualifier('u_1', '');
        }
    };
}
 
Script Include:
var Test = Class.create();
Test.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getChoices: function() {
        var firstFieldValue = this.getParameter('sysparm_first_value');
        var result = [];
        var gr = new GlideRecord('sys_choice');
        gr.addQuery('sys_id', 'firstFieldValue');
        gr.query();
        if (gr.next()) {
            result_value = gr.getValue('value');
            var gm = new GlideRecord('sys_choice');
            gm.addQuery('dependent_value', result_value);
            gm.query();
            while (gm.next()) {
                result.push(gm.sys_id + '');
            }
            return "sys_idIN" + result;
        }
    },
    type: 'Test'
});
1 ACCEPTED SOLUTION

@Aaviii  

 

Reference qualifier wont work using "g_form.setReferenceQualifier" . Instead of using onChange client script you will need to use Advanced Reference Qualifier for the field where you want the reference qualifier to be applied. You can change the function "getChoices" to accept the parameter as follows:

 

getChoices: function(firstFieldValue) {
 
var result = [];
var firstFieldValueArr = firstFieldValue.split(',');
for(var i = 0; i < firstFieldValueArr.length ; i++){
var gr = new GlideRecord('sys_choice');
gr.addQuery('sys_id', firstFieldValueArr[i]);
gr.query();
while(gr.next()) {
result_value = gr.getDisplayValue('value');
var gm = new GlideRecord('sys_choice');
gm.addQuery('dependent_value', result_value);
gm.query();
while (gm.next()) {
 result.push(gm.sys_id +'');
}
}
}
return "sys_idIN" + result;
},
 
then call the function from advanced reference qualifier as getChoices "javascript&colon;new Test().getChoices(current.<onChange field Name>);"
 
Note: Replace &colon; with an actual colon. Comment keeps mangling the character.
 
I got carried away with the script instead of focusing on the solution.
 
-Shantanu

View solution in original post

9 REPLIES 9

Arun_Manoj
Mega Sage

Hi @Aaviii ,

 

 

You're querying the sys_choice table by sys_id, but sys_choice usually doesn't use sys_id directly as a reference from form selections. You may actually be wanting to get choices for a specific field, where the element, name, and value columns are more relevant.

If you're trying to pass the selected value from a choice list field and want to retrieve other dependent choices based on that value, you might do better using the value field, not sys_id.


Corrected Script Include:

 

 

var Test = Class.create();
Test.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getChoices: function() {
var firstFieldValue = this.getParameter('sysparm_first_value');
var result = [];

// Assuming you're trying to use the 'value' field, not sys_id
var gm = new GlideRecord('sys_choice');
gm.addQuery('dependent_value', firstFieldValue);
gm.query();
while (gm.next()) {
result.push(gm.getValue('sys_id'));
}

return JSON.stringify(result);
},
type: 'Test'
});


Client Script (No Change Needed for Ajax Call):

Just make sure your newValue is the actual value of the selected choice, which is typically true for choice fields.

 

 

If the solution is correct, please give Helpful

 

thanks

Arun

Ankur Bawiskar
Tera Patron
Tera Patron

@Aaviii 

what's your requirement here?

are you trying to apply reference qualifier via g_form object?

share some details and screenshots.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Yes, I have 2 fields of list type. Based on selection in first field, I want dependent value in second field.

shantanu_patel8
Mega Guru

Hi @Aaviii ,

 

As the type of field is list then you must be getting comma-separated list of sys_ids rather then just a single sys_id so you can use the following script:

 

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var ga = new GlideAjax('Test');
    ga.addParam('sysparm_name''getChoices');
    ga.addParam('sysparm_first_value', newValue);
    ga.getXML(callback);

    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var sysIds = JSON.parse(answer);
        if (sysIds.length > 0) {
            var query = 'sys_idIN' + sysIds.join(',');
            g_form.setReferenceQualifier('u_1', query);
        } else {
            g_form.setReferenceQualifier('u_1''');
        }
    };
}
 
Script Include:
var Test = Class.create();
Test.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
   
getChoices: function() {
var firstFieldValue = this.getParameter('sysparm_first_value');
var result = [];
var firstFieldValueArr = firstFieldValue.split(',');
for(var i = 0; i < firstFieldValueArr.length ; i++){
var gr = new GlideRecord('sys_choice');
gr.addQuery('sys_id', firstFieldValueArr[i]);
gr.query();
while(gr.next()) {
result_value = gr.getDisplayValue('value');
var gm = new GlideRecord('sys_choice');
gm.addQuery('dependent_value', result_value);
gm.query();
while (gm.next()) {
 result.push(gm.sys_id +'');
}
}
}
return "sys_idIN" + result;
},
    type: 'Test'
});
 

Please mark the answer helpful and correct if it resolves the issue. Happy scripting 

shantanu_patel8_0-1747285439226.png

 

 

-Shantanu