Set reference file to null with a business rule

Brian Lancaster
Tera Sage

I have a requirement that when a users account in ServiceNow is set to active = false I need to check the CMDB application table for a custom reference filed and remove their name.   I setup a Business rule on the users table that fires when the active changes to false.   I have the following code and with some logging I know it is finding all the records but it is just not updating them.   Any thoughts on why this would be?   Also any thoughts on a better way to do this as there are multiple custom reference files I need to check?

var techOwner = new GlideRecord ('cmdb_ci_appl');

techOwner.addQuery('u_technical_owner', current.sys_id.name);

techOwner.query();

while (techOwner.next()){

  //gs.log('in while');

  techOwner.u_technical_owner = "NULL";

  techOwner.update();

}

I have also tried techOwner.u_technical_owner = '';

12 REPLIES 12

Yes the BR is running on the Sys_user table and yes u_technical_owner is a reference to sys_user.


HI Brain,



Then please try this code



  1. var techOwner = new GlideRecord ('cmdb_ci_appl');
  2. techOwner.addQuery('u_technical_owner', current.sys_id);
  3. techOwner.query();
  4. while(techOwner.next()){
  5.   techOwner.u_technical_owner = "";
  6.   techOwner.update();
  7. }


Please validate when to run condition of the BR


I invite you to try your script in scripts background, using the "faux current" method listed here to debug what could be the issue. As others have pointed out (using the exact same script I wrote), the solution is there.



Faster Server Side Script Development and Test


Thanks Chuck, ended up that this specific filed I build into the cmdb_ci table so I could use it on any of the CMDB tables.


You're welcome.



If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.



If you are viewing this from the community inbox you will not see the correct answer button.   If so, please review How to Mark Answers Correct From Inbox View.



Thank you