We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Script Include returning sys id's but resulting reference field blank

PranavSuj96
Mega Expert

Hello all,

I have posted iterations of this questions but I have finally gotten the right script include but the results are not showing...

I have a requirement to filter the application field based on the server selected. If this results in no records, I am to display all applications in the reference field. For now, the first part is working. The part when no results are found and to display all applications instead is finnicky. I checked this second part using a background script and it the query seems correct as the length of my array is equal to the total installed applications. Not sure where I am going wrong. I have included the script include below... Any help would be great! Thanks

 

 

findAppsInRelationship: function(server) {
  
  var g=[];
  var gr_cmdb_rel_ci = new GlideRecord("cmdb_rel_ci");
  gr_cmdb_rel_ci.addQuery('child',server);
  gr_cmdb_rel_ci.query();
  if(gr_cmdb_rel_ci.hasNext()) {
   while (gr_cmdb_rel_ci.next()) {
    g.push(gr_cmdb_rel_ci.parent.toString());
   }
    return 'install_status=1^sys_idIN'+g;
   }
  else {
   var h = [];
   var gr_cmdb_rel_ci2 = new GlideRecord("u_cmdb_ci_business_app");
   gr_cmdb_rel_ci2.addQuery('install_status','1');
   gr_cmdb_rel_ci2.query();
   if(gr_cmdb_rel_ci2.hasNext()) {
    while(gr_cmdb_rel_ci2.next()) {
     h.push(gr_cmdb_rel_ci2.sys_id.toString());
    }
   }
   return 'sys_idIN'+h;
  }
  },

1 ACCEPTED SOLUTION

Phuong Nguyen
Kilo Guru

Hi, PranavSuj96

Your h is an array so to convert it to a comma separated string, use .join(','), so instead of return 'sys_idIN'+h; try return 'sys_idIN' + h.join(',');

Let me know how it goes.

 

Thanks,

Phuong Nguyen

View solution in original post

6 REPLIES 6

PranavSuj96
Mega Expert

javascript: new findApps().findAppsInRelationship(current.variables.ref_ci); here is my reference qualifier btw

Brent Sutton
Mega Sage

Your array needs to be converted to a comma separated string. Can you change your return lines to the following:

return 'install_status=1^sys_idIN' + g.toString();


return 'sys_idIN' + h.toString();

It should then work. Let me know how you get along.

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information. 

Brent Sutton
Mega Sage

I've reconfigured your code a little to use a single array and single return. I've also converted your arrays to return as a comma separated string. Give the following a go.

Code:

findAppsInRelationship: function(server) {

	var results = [];
	var answer = "";
	
	var gr_cmdb_rel_ci = new GlideRecord("cmdb_rel_ci");
	gr_cmdb_rel_ci.addQuery('child',server);
	gr_cmdb_rel_ci.query();
	
	if(gr_cmdb_rel_ci.hasNext()) {
		while (gr_cmdb_rel_ci.next()) {
			results.push(gr_cmdb_rel_ci.parent.toString());
		}
		answer = 'install_status=1^sys_idIN' + results.toString();
	}
	else {
		var h = [];
		var gr_cmdb_rel_ci2 = new GlideRecord("u_cmdb_ci_business_app");
		gr_cmdb_rel_ci2.addQuery('install_status','1');
		gr_cmdb_rel_ci2.query();
		if(gr_cmdb_rel_ci2.hasNext()) {
			while(gr_cmdb_rel_ci2.next()) {
				results.push(gr_cmdb_rel_ci2.sys_id.toString());
			}
		}
		answer = 'sys_idIN' + results.toString();
	}
	return answer;
},

Let me know if this works for you.

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information. 

Phuong Nguyen
Kilo Guru

Hi, PranavSuj96

Your h is an array so to convert it to a comma separated string, use .join(','), so instead of return 'sys_idIN'+h; try return 'sys_idIN' + h.join(',');

Let me know how it goes.

 

Thanks,

Phuong Nguyen