Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Autopopulating the two list collectors

ServiceNow Use6
Tera Guru

Hi,

I have a scenario where i have two catalog variables, Servers and Applications. Servers is pointing to cmdb_ci_server, while applications pointing to the cmdb_ci_appl tables. Servers selection should autopopulate the Applications. it is autopopulating for 1 selection, but when i select multiple Servers, it is not working. Please make it as simple as possible. Kindly help.

 

1.jpg

 

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

   var gr = new GlideAjax('cmdbServer');
   gr.addParam('sysparm_name', 'cmdbFunction');
   gr.addParam('sysparm_id', newValue);
   gr.getXML(callBackFunction);
   function callBackFunction(response){
	var answer = response.responseXML.documentElement.getAttribute('answer');	
	g_form.setValue('application', answer);
   }
   
}

 

2.jpg

 

var cmdbServer = Class.create();
cmdbServer.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    cmdbFunction: function() {
        var abc = this.getParameter('sysparm_id');
		var gr = new GlideRecord('cmdb_rel_ci');
		gr.addQuery('parent', abc);
		gr.addQuery('type.name', 'Runs on::Runs');
		gr.query();
		while(gr.next()){
			return gr.child.name;
		}
    },

    type: 'cmdbServer'
});

 

Regards

Suman P.

 

1 ACCEPTED SOLUTION

palanikumar
Giga Sage
Giga Sage

Hope your variables are list collector. Modify your script include as below

var cmdbServer = Class.create();
cmdbServer.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    cmdbFunction: function() {
        var abc = this.getParameter('sysparm_id');
	var gr = new GlideRecord('cmdb_rel_ci');
	gr.addQuery('parent.sys_id', 'IN', abc);
	gr.addQuery('type.name', 'Runs on::Runs');
	gr.query();
	var returnArr = [];
	while(gr.next()){
		returnArr.push(gr.child.name.toString());
	}
	return returnArr.join(",");
    },

    type: 'cmdbServer'
});
Thank you,
Palani

View solution in original post

9 REPLIES 9

ServiceNow Use6
Tera Guru

Hi,

 

I am still not getting the output as required.

 

3.jpg

 

4.jpg

 

Regards

Suman P.

@ServiceNow Use6 

add alert message in the client script and see what data is coming in answer variable.

var gr = new GlideAjax('cmdbServer');
   gr.addParam('sysparm_name', 'cmdbFunction');
   gr.addParam('sysparm_id', newValue);
   gr.getXML(callBackFunction);
   function callBackFunction(response){
	var answer = response.responseXML.documentElement.getAttribute('answer');
alert(answer);	
	g_form.setValue('application', answer.toString());
   }

Hi,

It shows only second one.

 

5.jpg

 

Regards

Suman P.

Can you try this:

var cmdbServer = Class.create();
cmdbServer.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    cmdbFunction: function() {
        var abc = this.getParameter('sysparm_id');
        var gr = new GlideRecord('cmdb_rel_ci');
        gr.addQuery('parent.sys_id', 'IN', abc);
        gr.addQuery('type.name', 'Runs on::Runs');
        gr.query();
        var returnArr = [];
        while(gr.next()){
                returnArr.push(gr.child.sys_id.toString());
        }
        return returnArr.join(",");
    },

    type: 'cmdbServer'
});
Thank you,
Palani

ServiceNow Use6
Tera Guru

Hi,

parent itself is a reference field in cmdb_rel_ci table right, why we need .sys_id again?

Regards

Suman P.