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

Chuck Tomasi
Tera Patron

Hi Brian,



If u_technical_owner is a reference field, then you don't want to use current.sys_id.name. That is a bogus query and you're probably getting too many records.



Try this. I also added an output statement before the loop to show how many records were found.



var techOwner = new GlideRecord ('cmdb_ci_appl');  


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


techOwner.query();


gs.log(techOwner.getRowCount() + ' records found');


while (techOwner.next()){  


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


  techOwner.u_technical_owner = '';  


  techOwner.update();  


}  


When I use current.sys_id instead of current.sys_id.name it never goes into the while loop.


Check one again, if it is a reference field so need to pass sys_id only. IF it won't pass to the while loop means may be he did not have any records.



for trouble shoot, check the log statement what is returning



  • var techOwner = new GlideRecord ('cmdb_ci_appl');
  • techOwner.addQuery('u_technical_owner', current.sys_id);     // give the user name of back end name and check
  • techOwner.query();
  • gs.log('Total Records:'+techOwner.getRowCount());
  • while (techOwner.next()){
  •   //gs.log('in while');
  •   techOwner.u_technical_owner = '';
  •   techOwner.update();
  • }

Is the BR running on the user (sys_user) table?



Is the u_technical_owner a reference field to sys_user?



Those were my two assumptions. Correct me if I'm wrong on this.



FWIW, sys_id does not have a property called name. current.sys_id.name is returning 'undefined'.