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

@shantanu_patel8 @Ankur Bawiskar Am able to get correct data in script include now. But am passing this data to client script to make a query using g_form.setReferenceQualifier and that is not working.

 

Client Script code:

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(',');
            alert("query");
            g_form.setReferenceQualifier('u_1', query);
        } else {
            g_form.setReferenceQualifier('u_1', '');
        }
    };
}

@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

@Aaviii 

is this "g_form.setReferenceQualifier" working?

If yes then is it working in native and portal both or only native?

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

@Ankur Bawiskar  I didn't used g_form.setReferenceQualifier for this now. 

Ron28
Kilo Sage

Another thing to note is that unless your script include was created in the global app scope, you will need to add your own app scope in the client script:

var
ga = new GlideAjax('Test');
may need to change to:
var ga = new GlideAjax('x_mycompanyprefix_myapp.Test');

Ron