Auto populate reference variable based on another reference variable on a catalog item form. I have reference variable Application referring to 'Name' field on Applications (cmdb_ci_appl) table and another list field called 'Roles' in it. On catalog

jyotsna1
Giga Contributor

Auto populate reference variable based on another reference variable on a catalog item form. I have reference variable Application referring to 'Name' field on Applications (cmdb_ci_appl) table and there is a list field called 'Roles' on same table, which has multiple roles associated to each application (these roles are maintained in a separate custom table). On catalog Item I want to populate roles from this list field values in a variable based on the application selected on catalog form. 

Hope someone helps me get this! Thank you

16 REPLIES 16

Hi,

recommended is Client Script + GlideAjax

Script Include: It should be client callable

var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getDetails: function(){

        var sysIds = this.getParameter('sysparm_ci');            
        var arr = [];
        var gr = new GlideRecord('cmdb_ci_appl');
        gr.addQuery('sys_id', 'IN' , sysIds);
        gr.query();
        while(gr.next()){
            arr.push(gr.u_security_roles.toString());
        }
        return arr.toString();
    },
    
    type: 'checkRecords'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }

if(newValue == ''){
g_form.clearValue('security_roles');
}

var ga = new GlideAjax('checkRecords');
    ga.addParam('sysparm_name', "getDetails");
    ga.addParam('sysparm_ci', g_form.getValue('variableName')); // give here variable name
    ga.getXMLAnswer(function(answer){
        if(answer != ''){
         
            g_form.setValue('security_roles', answer); // give here user_id variable name
          
        }
    });
    //Type appropriate comment here, and begin script below

}

Regards
Ankur

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

jyotsna1
Giga Contributor

Hi Ankur,

Below are my script include and Client Script, its still the same shows all roles on portal when application is selected and shows there is a javascript error in this browser console

Script Include

var securityroles = Class.create();
securityroles.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRole: function() {
var name = this.getParameter('sysparm_appname'); // aapplication variable name
var arr = [];
// if (name != '') {
var gr = new GlideRecord('cmdb_ci_appl');
gr.addQuery('sys_id', 'IN', name);
gr.query();
while (gr.next()) {
arr.push(gr.u_security_roles.toString());
}
return arr.toString();
},

type: 'securityroles'

});

Client Script

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}

if (newValue == '') {
g_form.clearValue('security_roles');
}

var ga = new GlideAjax('securityroles');
ga.addParam('sysparm_name', "getRole");
ga.addParam('sysparm_appname', g_form.getValue('u_security_roles'));
ga.getXMLAnswer(function(answer) {
if (answer != '') {

g_form.setValue('security_roles', answer);
}
});

}

 

Hi,

what browser console error?

what came in the alert?

alert(answer);

if (answer != '') {

g_form.setValue('security_roles', answer);
}

Regards
Ankur

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

Hi,

can you update client script as this

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}

if (newValue == '') {
g_form.clearValue('security_roles');
}

var ga = new GlideAjax('securityroles');
ga.addParam('sysparm_name', "getRole");
ga.addParam('sysparm_appname', g_form.getValue('u_security_roles'));
ga.getXML(callBackMethod);

function callBackMethod(response){
        var answer = response.responseXML.documentElement.getAttribute("answer");
   if (answer != '') {
   g_form.setValue('security_roles', answer);
   }
}

}

Regards
Ankur

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

jyotsna1
Giga Contributor

Hi Ankur,

It's still the same, Security roles list collector field shows all values and throws 'There is a JavaScript error in your browser console' error.