
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2017 12:14 PM
What is wrong with the last section of this script, where I'm trying to remove the company from business services? This is an after business rule on the core_company table, and runs on update when Active changes to false. The rest of the script works fine.
For some reason, the index is always -1 even though I know that the company is in the u_contracted_companies field. This code worked when I built a test script in scheduled jobs, but doesn't work in my business rule. What am I missing?
(function executeRule(current, previous /*null when async*/) {
var comp = current.sys_id;
gs.log('comp: ' + comp);
var compName = current.name;
var userCount = 0;
//deactivate users
var usrs = new GlideRecord('sys_user');
usrs.addActiveQuery();
usrs.addQuery('company',comp);
usrs.query();
while (usrs.next())
{
usrs.active = false;
usrs.update();
//gs.addInfoMessage('User ' + usrs.name + ' record deactivated based on company deactivation');
//remove user from all groups
var grps = new GlideRecord('sys_user_grmember');
grps.addQuery('user',usrs.sys_id);
grps.deleteMultiple();
//gs.addInfoMessage('User ' + usrs.name + ' removed from all security groups');
userCount++;
}
gs.addInfoMessage(userCount + ' users deactivated and removed from security groups');
//cancel all service contracts
var contractCount = 0;
var contract = new GlideRecord('ast_contract');
contract.addQuery('contract_model','8881e836c3102000b959fd251eba8f29');
contract.addQuery('u_customer',comp);
contract.addQuery('state','active');
contract.query();
while (contract.next())
{
contract.state = 'cancelled';
contract.substate = '';
contract.update();
contractCount++;
}
gs.addInfoMessage(contractCount + ' service contracts cancelled for ' + compName);
//remove from business services
var bs = new GlideRecord('cmdb_ci_service');
bs.query();
gs.log('bs query returned ' + bs.getRowCount() + ' records');
var count = 0;
while (bs.next())
{
var cList = bs.u_contracted_companies;
var cListArray = [];
cListArray = cList.split(',');
var i = cListArray.indexOf(comp);
gs.log('index: ' + i);
if (i >= 0)
{
count++;
cListArray.splice(i,1);
bs.u_contracted_companies = cListArray.join(',');
bs.update();
}
}
gs.addInfoMessage(compName + ' removed from ' + count + ' business services');
})(current, previous);
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2017 12:35 PM
Copy the script as is and you are good to go
var comp = current.sys_id.toString();
gs.log('comp: ' + comp);
var compName = current.name;
var userCount = 0;
//deactivate users
var usrs = new GlideRecord('sys_user');
usrs.addActiveQuery();
usrs.addQuery('company',comp);
usrs.query();
while (usrs.next())
{
usrs.active = false;
usrs.update();
//gs.addInfoMessage('User ' + usrs.name + ' record deactivated based on company deactivation');
//remove user from all groups
var grps = new GlideRecord('sys_user_grmember');
grps.addQuery('user',usrs.sys_id);
grps.deleteMultiple();
//gs.addInfoMessage('User ' + usrs.name + ' removed from all security groups');
userCount++;
}
gs.addInfoMessage(userCount + ' users deactivated and removed from security groups');
//cancel all service contracts
var contractCount = 0;
var contract = new GlideRecord('ast_contract');
contract.addQuery('contract_model','8881e836c3102000b959fd251eba8f29');
contract.addQuery('u_customer',comp);
contract.addQuery('state','active');
contract.query();
while (contract.next())
{
contract.state = 'cancelled';
contract.substate = '';
contract.update();
contractCount++;
}
gs.addInfoMessage(contractCount + ' service contracts cancelled for ' + compName);
//remove from business services
var bs = new GlideRecord('cmdb_ci_service');
bs.addQuery('u_contracted_companies','CONTAINS',comp);
bs.query();
gs.log('bs query returned ' + bs.getRowCount() + ' records');
var count = 0;
while (bs.next())
{
var arrayUtil = new ArrayUtil();
var cListArray = bs.u_contracted_companies.toString().split(',');
var arr1=[];
arr1.push(comp);
bs.u_contracted_companies = arrayUtil.diff(cListArray, arr1).join();
bs.update();
}
gs.addInfoMessage(compName + ' removed from ' + count + ' business services');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2017 12:31 PM
I found the right script:
(function executeRule(current, previous /*null when async*/) {
if(current.assignment_group != previous.assignment_group){
var wnList = current.work_notes_list.split(',');
//remove previous group from work notes list
if(previous.assignment_group.u_copy_on_notifications == true){
var pM = new GlideRecord('sys_user_grmember');
pM.addQuery('group',previous.assignment_group);
pM.query();
while(pM.next()){
var aU = new ArrayUtil();
var pos = wnList.indexOf(pM.user.sys_id.toString());
if(pos != -1){
wnList.splice(pos,1);
}
}
current.work_notes_list = wnList.toString();
}
//add new group to work notes list
var c = new GlideRecord('sys_user_group');
c.addQuery('sys_id',current.assignment_group);
c.query();
if(c.next()){
if(c.u_copy_on_notifications == true){
var cu = new GlideRecord('sys_user_grmember');
cu.addQuery('group',c.sys_id);
cu.query();
while(cu.next()){
current.work_notes_list += ',' + cu.user.sys_id;
}
}
}
}
})(current, previous);