Removing value from list field via business rule

kchorny
Tera Guru

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);

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

kchorny


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');


View solution in original post

5 REPLIES 5

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

The last portion of your script is searching through every business service.   Have you considered searching for just business services where u_contracted_companies contains the company instead?   Then you can take action only on those records.



var bs = new GlideRecord('cmdb_ci_service');


bs.addQuery('u_contracted_companies', 'CONTAINS', comp);


bs.query();


Abhinay Erra
Giga Sage

kchorny


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');


Thank you!!!


Hi Abhinay - I'm trying to do splice on work notes list and failing to get it to work. I ran across this and attempted to use it, but my code still isn't working. Do you have any idea what I've done wrong in the various attempts listed here (notes added to explain the results I'm getting)?



if(current.assignment_group != previous.assignment_group){



  var workNotesList = current.work_notes_list;



  if(previous.assignment_group){



  var oldGroupMembers = memberArrayBuilder(previous.assignment_group);


  oldGroupMembers.split(',');



  for(var i = 0; i < oldGroupMembers.length; i++){



  var pos = workNotesList.indexOf(oldGroupMembers[i]);


  gs.info('EEEEE position is ' + pos);



  if(pos != -1){


  var arrayUtil = new ArrayUtil();


  var arr1 = [];


  arr1.push(oldGroupMembers[i]);


  var wnl = workNotesList.toString().split(','); //added this to combine with line immediately below


  workNotesList = arrayUtil.diff(wnl,arr1).join(); //this results in no change to my work notes list


  //workNotesList = arrayUtil.diff(workNotesList,arr1).join(); //this removed all members


  //workNotesList.splice(pos,1); //this is the line that isn't working - I'm getting 0 and tested that it's not a string. No change to workNotesList


  gs.info('EEEEE workNotesList 1.5 removed member new list is ' + workNotesList);


  }


  }


  }