How to check instantly , whether a Scheduled Job is working or not?

Kristina2
Tera Contributor

How to check instantly , whether a Scheduled Job is working or not?

11 REPLIES 11

I add gs.log to the top and bottom.   Then check your logs to ensure something was logged.


Hi Sree,



You can always put some gs.log() statements in your scheduled job and check under System Logs> Script Log Statements to see what's coming out. That will ensure your script is running (or at least part of it.)



The other way is to take the code and try it in Scripts - Background to ensure it does what you want it to do. This is effectively the same as running it in a scheduled job on demand and clicking Execute Now, but you can see if there are any errors (and output.)


Kristina2
Tera Contributor

I will just explain what i did . It will be helpful , if somebody can find the solution.



In users table ,


There is a director field (u_director)   , a manager field (manager) and a rank field (u_rank).


And when a user is created we manually enter the Rank and Manager .



So that we want to Auto populate the "director " field based on Users rank .



If User rank is 18 and above ,


It should check the user manager and set him as director .



If that particular manager is not above 18 , it should go down the loop and check the manager's manager and set him as director .



Code :



function fixUsersWithRanks (query) {


  var gr = new GlideRecord("sys_user");


  //gr.addEncodedQuery(query);


  gr.query();


  while(gr.next()) {


  updateUser([ { sys_id: gr.getValue("sys_id"),


      u_rank: gr.getValue("u_rank")


  } ]);


  }


}


function updateUser (userArray) {


  while(userArray[userArray.length-1].u_rank>= 18) {


  var managerWithRank = getMangerWithRank(userArray[userArray.length-1].sys_id);


  if(JSUtil.notNil(managerWithRank) ) {


  userArray.push(managerWithRank);


  } else   {


  return;


  }


  }


  updateUserRanks(userArray);


}


function getMangerWithRank (sysId) {


  var gr = new GlideRecord("sys_user");


  if(gr.get(sysId)) {


  if(JSUtil.notNil((gr.getValue("manager"))))


  {


  var managerGr = gr.manager.getRefRecord();


  if(JSUtil.notNil(managerGr.getValue("u_rank")) ) {


  return   { sys_id:managerGr.getValue("sys_id"),


    u_rank:managerGr.getValue("u_rank") };


  }


  }


  }


}


function updateUserRanks (userArray) {


  gs.log("updateUserRanks: " + new JSON().encode(userArray));


  var sysIds = [];


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


  sysIds.push(userArray[i].sys_id);


  }


  var mgrSysId = sysIds[sysIds.length-1];


  sysIds.splice(userArray.length-1, 1);


  var gr = new GlideRecord("sys_user");


  gr.addQuery("sys_id", "IN", sysIds.join(","));


  gr.query();


  gs.log("sysIds: " + sysIds.join(","));


  while(gr.next()) {


  gr.u_director   = mgrSysId;


  gr.update();


  }


}




fixUsersWithRanks("u_rankISNOTEMPTY");


Hi Sree,



The first thing I note is that your encoded query is commented out. For testing I presume...



Try running this in Scripts Background and use gs.log() or gs.print() statements to debug things like how many records you retrieved, what record is being updated, values of variables, etc. It's not pretty, but it's worked for decades. 🙂


Can you pls check if anything is wrong with the code?