Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

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

Swapna Abburi
Mega Sage
Mega Sage

Hi @ServiceNow Use6 

Update your script include script as below. The one you have written works only for one server and moreover, you need to get child CI sys_ids instead of name of the CI.

 

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

 

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

Hi,

It works perfectly. is it possible for us to make the script more easier? I am not that good with scripting.

 

Regards

Suman P.

Hi,

You cant make it simpler than this. 

Mark my response as accepted solution since your issue is resolved.

T

Thank you,
Palani