Visbility control on a SI collides a before insert/update script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2025 05:31 AM - edited 01-16-2025 05:33 AM
Hi All,
We have a Script Include that controls the visibility of CI on all catalog items
var CYz = Class.create();
CYz.prototype = {
initialize: function() {},
filter: function(name) {
if (name == 'Y') {
name = "DC 1.0";
} else if (name == "O ops") {
name = "Ops";
var ciArr1 = [];
var gr1 = new GlideRecord('cmdb_ci_service');
gr1.addEncodedQuery('u_businessIN' + name);
gr1.query();
while (gr1.next()) {
ciArr1.push(gr1.sys_id.toString());
}
return 'sys_idIN' + ciArr1.join();
} else if (name == "Res") {
name = "XYZ";
} else if (name == "Res2") {
name = "XYZ";
} else if (name == "Digital Platform") {
name = "ABC";
} else if (name == "CBN") {
name = "Asy";
}
var ciArr = [];
var gr = new GlideRecord('cmdb_ci_service');
gr.addEncodedQuery('u_businessIN' + name);
gr.addEncodedQuery('install_status!=7');
gr.query();
while (gr.next()) {
ciArr.push(gr.sys_id.toString());
}
return 'sys_idIN' + ciArr.join();
},
filterVariable: function(name) {
if (name == 'DC') {
name = "DC 1.0";
} else if (name == "Res") {
name = "XYZ";
} else if (name == "Res2") {
name = "XYZ";
} else if (name == "Digital Platform") {
name = "ABC";
} else if (name == "CBN") {
name = "Asy";
}
var ciArr = [];
var gr = new GlideRecord('cmdb_ci_service');
gr.addEncodedQuery('u_businessIN' + name);
gr.addEncodedQuery('install_status!=7');
gr.query();
while (gr.next()) {
ciArr.push(gr.sys_id.toString());
}
return 'sys_idIN' + ciArr.join();
},
getcallerRecords: function() {
var ciArr = [];
var gr = new GlideRecord('cmdb_ci_service');
gr.addEncodedQuery('install_status!=7^ORinstall_status=^u_business=Digital Platform^service_classification=Application Service');
gr.query();
while (gr.next()) {
var gr1 = new GlideRecord("cmdb_rel_person");
gr1.addEncodedQuery('ci=' + gr.sys_id + '^user.active=true');
gr1.query();
if (!gr1.next()) {
ciArr.push(gr.sys_id.toString());
}
}
return 'sys_idIN' + ciArr.join();
},
getauthRecords: function() {
var ciArr = [];
var gr = new GlideRecord('cmdb_ci_service');
gr.addEncodedQuery('install_status!=7^ORinstall_status=^u_business=Digital Platform');
gr.query();
while (gr.next()) {
var gr1 = new GlideRecord("u_m2m_sys_user_cmdb_ci_service");
gr1.addEncodedQuery('u_cmdb_ci_service=' + gr.sys_id + '^sys_user.active=true');
gr1.query();
if (!gr1.next()) {
ciArr.push(gr.sys_id.toString());
}
}
return 'sys_idIN' + ciArr.join();
},
type: 'CYz'
};
I have a variable set on few of the catalog items on which this script include is applied in advanced cond as:
javascript: new CYz().filterVariable(current.cat_item.category.title)
Now I have written a BR on RITM table(sc_req_item) on before insert/update with script as below
(function executeRule(current, previous /*null when async*/ ) {
var user_ids = [];
var grWatchlist = new GlideRecord('u_m2m_from_cmdb_ci_service_watchlist');
grWatchlist.addQuery('u_cmdb_ci_service', current.getValue('business_service'));
grWatchlist.query();
while (grWatchlist.next()) {
user_ids.push(grWatchlist.getValue('u_user'));
}
current.watch_list = user_ids.join(',');
})(current, previous);
Condition applied to BR: When <CI changes or is not empty>
No other cond applied.
So when I open any existing RITM from the backend and try to update the CI field it works. However when a catalog item is submitted via Portal or from Cat item view page it does not work. Does it have something to do with this SI configured to that variable set?
Help on the above much appreciated!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2025 05:50 AM
when catalog item is submitted are you sure the field business_service is populated?
if yes then only your logic will work for the BR
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2025 06:00 AM
I just noticed I have this on the BR
var grWatchlist = new GlideRecord('u_m2m_from_cmdb_ci_service_watchlist'); grWatchlist.addQuery('u_cmdb_ci_service', current.getValue('business_service'));
Should it be current.variables.getValue('business_service') instead?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2025 06:20 AM
Actually I figured the issue. But have a new issue now.
I updated the script as below and it works!
(function executeRule(current, previous /*null when async*/ ) {
var user_ids = [];
var grWatchlist = new GlideRecord('u_m2m_from_cmdb_ci_service_watchlist');
grWatchlist.addQuery('u_cmdb_ci_service', current.getValue('business_service'));
grWatchlist.query();
while (grWatchlist.next()) {
user_ids.push(grWatchlist.getValue('u_user'));
}
current.watch_list = user_ids.join(',');
})(current, previous);
Now the problem is it works on insert or update but since I have same BR for both Insert/Update it does not allow to add a new user to watchlist manually. It does not get updated
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2025 06:44 AM
simply enhance with this so that it supports adding manual user and the earlier value is retained
(function executeRule(current, previous /*null when async*/ ) {
var user_ids = [];
var grWatchlist = new GlideRecord('u_m2m_from_cmdb_ci_service_watchlist');
grWatchlist.addQuery('u_cmdb_ci_service', current.getValue('business_service'));
grWatchlist.query();
while (grWatchlist.next()) {
user_ids.push(grWatchlist.getValue('u_user'));
}
// Retain manually entered watch list values
var existing_watch_list = current.watch_list.split(',');
for (var i = 0; i < existing_watch_list.length; i++) {
if (user_ids.indexOf(existing_watch_list[i]) == -1) {
user_ids.push(existing_watch_list[i]);
}
}
current.watch_list = user_ids.join(',');
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader