- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2023 12:33 PM
Auto ticket closure requirement for incidents and sctask.
I have a business requirement. If a ticket is in the "Waiting customer" state for 10 business days be it incident to sctask.
For Sctask: Assigned to is mandatory field, but in most cases, it will be updated, but if its empty I'm ok to over ride this data policy.
For incidents
Assigned to, resolution code and resolute notes are mandatory to resolve an incident. It's ok to over ride the policy for assigned to, but for resolution code should be updated to "closed by caller" and resolution notes should be " No response from caller"
what will be the best approach? If its business rule can someone please help me with the rule?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2023 02:37 AM
Hi @Thomas98 ,
You can create a Scheduled Job in ServiceNow that runs daily and checks for tickets that have been in the "Waiting customer" state for 10 business days.
Here are the steps you can follow:
- Create a new Scheduled Job
- Set the Interval /trigger type to "Daily".
- In the "Script" field, enter the name of a Script Include (new AutoTicketClosure.run()) that you will create in the next step.
- Save the Scheduled Job.
Now, you need to create the Script Include that will contain the logic to close the tickets that meet the criteria. Here's an example of what the Script Include could look like:
var AutoTicketClosure = Class.create();
AutoTicketClosure.prototype = {
initialize: function() {
// Set the duration for which the ticket should be in "Waiting customer" state before closing it (in business days)
this.waitingCustomerDuration = 10;
},
run: function() {
var gr = new GlideRecord('incident');
gr.addQuery('state', 'Waiting customer');
gr.addQuery('sys_created_on', '<=', gs.daysAgo(this.waitingCustomerDuration));
gr.query();
while (gr.next()) {
// Check if the incident has been assigned to someone
if (gr.assigned_to.nil()) {
// Do nothing if the incident has not been assigned to anyone
continue;
}
// Check if the incident has resolution information
if (gr.u_resolution_code.nil() || gr.u_resolution_notes.nil()) {
// Set the resolution information if it is missing
gr.u_resolution_code = 'Closed by caller';
gr.u_resolution_notes = 'No response from caller';
gr.update();
}
// Close the incident
gr.setValue('state', 7); // 7 is the value for "Closed"
gr.update();
}
var sctask = new GlideRecord('sc_task');
sctask.addQuery('state', 'Waiting customer');
sctask.addQuery('sys_created_on', '<=', gs.daysAgo(this.waitingCustomerDuration));
sctask.query();
while (sctask.next()) {
// Check if the sctask has been assigned to someone
if (sctask.assigned_to.nil()) {
// Do nothing if the sctask has not been assigned to anyone
continue;
}
// Close the sctask
sctask.setValue('state', 4); // 4 is the value for "Closed Complete"
sctask.update();
}
},
type: 'AutoTicketClosure'
};
If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the ✅Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.
Thank you!
Ratnakar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2023 03:20 PM - edited 05-22-2023 03:21 PM
Hello Thomas,
I see you have a javascript Class definition in your code for a Scheduled Job. I guess that is a Script Include? Anyway, I typically use the script in the scheduled job "Run this script" field. I'm not familiar with your usage.
Add some debug to your script. One example:
gs.info("AutoTicketClosure: Found " + gr.getRowCount() + " records to process");
after the line: "gr.query();" and before the while loop. Add similar debug messages inside the loop. Like:
gs.info("AutoTicketClosure: Closing incident: " + gr.number + ".");
just before "gr.update();"
then you can look in the Script Log Statements module and search message, starts with, AutoTicketClosure.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2023 05:48 AM
Hi @Thomas98
In you scheduled job try:
var acScript = new AutoTicketClosure();
acStript.run();
You can click the "Execute now" button to test. And if you add the gs.log() statements proposed, review the Scripts Log Statements to see those messages.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2023 12:53 PM
Hi Thomas98,
Business rules run when a table's records are accessed, updated, created, and deleted. You can use a Scheduled Job that queries the desired table and filters on "Updated on" and State. And then update the desired fields and update the record. Please review those in the 'sysauto' table for examples.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2023 02:37 AM
Hi @Thomas98 ,
You can create a Scheduled Job in ServiceNow that runs daily and checks for tickets that have been in the "Waiting customer" state for 10 business days.
Here are the steps you can follow:
- Create a new Scheduled Job
- Set the Interval /trigger type to "Daily".
- In the "Script" field, enter the name of a Script Include (new AutoTicketClosure.run()) that you will create in the next step.
- Save the Scheduled Job.
Now, you need to create the Script Include that will contain the logic to close the tickets that meet the criteria. Here's an example of what the Script Include could look like:
var AutoTicketClosure = Class.create();
AutoTicketClosure.prototype = {
initialize: function() {
// Set the duration for which the ticket should be in "Waiting customer" state before closing it (in business days)
this.waitingCustomerDuration = 10;
},
run: function() {
var gr = new GlideRecord('incident');
gr.addQuery('state', 'Waiting customer');
gr.addQuery('sys_created_on', '<=', gs.daysAgo(this.waitingCustomerDuration));
gr.query();
while (gr.next()) {
// Check if the incident has been assigned to someone
if (gr.assigned_to.nil()) {
// Do nothing if the incident has not been assigned to anyone
continue;
}
// Check if the incident has resolution information
if (gr.u_resolution_code.nil() || gr.u_resolution_notes.nil()) {
// Set the resolution information if it is missing
gr.u_resolution_code = 'Closed by caller';
gr.u_resolution_notes = 'No response from caller';
gr.update();
}
// Close the incident
gr.setValue('state', 7); // 7 is the value for "Closed"
gr.update();
}
var sctask = new GlideRecord('sc_task');
sctask.addQuery('state', 'Waiting customer');
sctask.addQuery('sys_created_on', '<=', gs.daysAgo(this.waitingCustomerDuration));
sctask.query();
while (sctask.next()) {
// Check if the sctask has been assigned to someone
if (sctask.assigned_to.nil()) {
// Do nothing if the sctask has not been assigned to anyone
continue;
}
// Close the sctask
sctask.setValue('state', 4); // 4 is the value for "Closed Complete"
sctask.update();
}
},
type: 'AutoTicketClosure'
};
If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the ✅Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.
Thank you!
Ratnakar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2023 06:20 PM
Hey....
Sorry, I was on unplanned leave and the requirement was put on hold. Thank you for helping. I will try it out this week and let you know 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2023 07:00 PM
Dear @Thomas98
I hope everything is fine for you.
Please let me know in case any queries.
- Kailas