- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2019 06:20 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2019 07:10 AM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2019 06:44 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2019 06:54 AM
Thank you so much for the code let me test it , do i still need script include ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2019 06:56 AM
I would call this code from a script include, yes.
Please mark this as correct/helpful if it resolved your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2019 06:25 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2019 06:32 AM
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
}