- 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-21-2023 07:13 AM
Hi Ratnakar,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2023 10:47 AM - edited 05-21-2023 11:03 AM
Hi thomas98,
I tested your script, and found that the addQuery() lines for 'state' and 'hold_reason' are using the Label values and not the values for those Choice fields. I got no results where I have data that match. In my instance, the 'state' value for "On Hold" is '3', and the hold_reason value for "Awaiting Caller" is '1'. When you query 'sct_task' the same is present in your script. but when you set the 'state' value you use the value '4' (for "Closed Complete" I have value '3'). Try using the Choice values for your addQuery(). And remove the 'gr.update();' in the if block:
if (gr.u_resolution_code.nil() || gr.u_resolution_notes.nil()) {
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2023 01:06 PM
Thank you for your quick response.
Here is the update I made:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2023 01:15 PM
Hi Thomas98,
I still see:
sctask.addQuery('state', 'Waiting customer');
I doubt that will return what you're looking for. But the other changes look good.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2023 01:30 PM
Sorry my bad!
Updated it