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

Amazing. Thank you so much! I assumed that's how adding the array was formatted automatically. Guess not!

You're welcome! I'm glad you got it working! 🙂