how to check if the records in a table is updated or not

RudhraKAM
Tera Guru

Can some one  help me with this requirement 

I want to monitor sys_user table and check if there is any update or insert happens in every 4 hours 

How can i do that , Please help me with the code if  possible 

1 ACCEPTED SOLUTION

You'll need to make a couple of modifications to your script. I think caller_id may also be a required field on the incident, but just make sure all required fields are populated in the script. 

var users = new GlideRecord('sys_user');
users.addEncodedQuery('sys_created_onRELATIVEGE@hour@ago@4");
users.query();

if (!users.hasNext()){
    var gr = new GlideRecord('incident');
    gr.initialize();
    gr.short_description = 'Certificate renewal required';
    gr.assignment_group = '5e8550e90f6e3900f6e783fc22050ef3'; //Sys_id of x workgroup
    gr.description = //current will not work for this scheduled job, you'll need to use data from one of the glide records 
    gr.cmdb_ci = 'Certificate Services (Corporate Internal)'); //I don't know whether setDisplayValue will do the same
    gr.insert();
}

View solution in original post

13 REPLIES 13

You'd need a scheduled job that runs ever 4 hours. That job can call a script include containing the following code: 

var users = new GlideRecord('sys_user');
users.addEncodedQuery("sys_created_onRELATIVEGE@hour@ago@4^ORsys_updated_onRELATIVEGE@hour@ago@4")
users.query();
if(!users.next()) {
  //create incident
  var incident = new GlideRecord("incident"); 
  incident.initialize();
  incident.short_description = "Users are not being updated or created";
  incident.description = "Users are not being updated or created";
  incident.insert(); 
}

 

Please mark this as correct/helpful if it resolved your question.

Thank you so much for the code let me test it , do i still need script include ?

I would call this code from a script include, yes.

Please mark this as correct/helpful if it resolved your question.

Marcio Jos_
Mega Guru

Hello Kam 

You can make an update businness rule on the table, and if the business rule runs, you can also put some log with date and time, if you wanna do this every 4 hours you can build a schedule job

If you see that my answer is correct,please, mark as correct

Thanks

Weston Wilson
Tera Expert

In order to check for an update/insert, you could either do a business rule that runs every time the user record is inserted or updated or you could create a scheduled job to check sys_updated_on that runs periodically every 4 hours. The script to check if the update was within the past 4 hours is: 

var user = new GlideRecord('sys_user');
//Add a query for the user being updated in the past 4 hours
user.addEncodedQuery('sys_updated_onRELATIVEGE@hour@ago@4');
user.query();

while (user.next()){
	//do something
}