Trying to auto-close cases in the Customer Service application

shawnbaar
Giga Expert

Hello,

I am attempting to create a scheduled job to auto-close Cases from the Customer Service application.  I tried to model it after the incident autoclose business rule and the Autoclose Incident scheduled job.  I don't seem to be able to get it to work, and I was wondering if anyone could provide some insight as to why it doesn't work? Here's the scheduled job:

find_real_file.png

Anyone able to help?

 

Thanks,

Shawn

1 ACCEPTED SOLUTION

parag-mone
ServiceNow Employee
ServiceNow Employee

Hello Shawn,

Below is the sample script to close Cases which are in "Resolved'" state and are not updated in the specified number of days. 

// This script automatically closes case that are resolved
// and haven't been updated in the specified number of days
// This number is a property in System Properties.
// To place a comment in the incident, uncomment the "gr.comments" line.

autoCloseCases();

function autoCloseCases() {
	var ps = gs.getProperty('glide.ui.autoclose.time');
	var pn = parseInt(ps);
	var queryTime = new GlideDateTime();
	queryTime.addDaysUTC(-pn);

	if (pn > 0) {
		var gr = new GlideRecord('sn_customerservice_case');
		gr.addQuery('state', 6);//Resolved
		gr.addQuery('sys_updated_on', '<', queryTime);
		gr.query();
		while(gr.next()) {
			gs.print("Case:"+gr.number);
			gr.close_notes = 'Autoclose';
			gr.active = false;
			gr.closed_by = gr.resolved_by;
			if((new global.StateFlow().validFlow(gr, 'd8069501c33231005f76b2c712d3aead', 'automatic'))){
				new global.StateFlow().processFlow(gr, 'd8069501c33231005f76b2c712d3aead', 'automatic');
				//d8069501c33231005f76b2c712d3aead : ID of state flow "Close Case"
			}
			gr.update(); 
		}
	}
}

Please also take a look at the state flow - "Close Case" defined for the Case (sn_customerservice_case), you can capture additional validations over here. For example, close_notes should be "Autoclose" or closed_by should be specific user etc.

 

find_real_file.png

Thanks,

Parag Mone

View solution in original post

5 REPLIES 5

parag-mone
ServiceNow Employee
ServiceNow Employee

Hello Shawn,

Below is the sample script to close Cases which are in "Resolved'" state and are not updated in the specified number of days. 

// This script automatically closes case that are resolved
// and haven't been updated in the specified number of days
// This number is a property in System Properties.
// To place a comment in the incident, uncomment the "gr.comments" line.

autoCloseCases();

function autoCloseCases() {
	var ps = gs.getProperty('glide.ui.autoclose.time');
	var pn = parseInt(ps);
	var queryTime = new GlideDateTime();
	queryTime.addDaysUTC(-pn);

	if (pn > 0) {
		var gr = new GlideRecord('sn_customerservice_case');
		gr.addQuery('state', 6);//Resolved
		gr.addQuery('sys_updated_on', '<', queryTime);
		gr.query();
		while(gr.next()) {
			gs.print("Case:"+gr.number);
			gr.close_notes = 'Autoclose';
			gr.active = false;
			gr.closed_by = gr.resolved_by;
			if((new global.StateFlow().validFlow(gr, 'd8069501c33231005f76b2c712d3aead', 'automatic'))){
				new global.StateFlow().processFlow(gr, 'd8069501c33231005f76b2c712d3aead', 'automatic');
				//d8069501c33231005f76b2c712d3aead : ID of state flow "Close Case"
			}
			gr.update(); 
		}
	}
}

Please also take a look at the state flow - "Close Case" defined for the Case (sn_customerservice_case), you can capture additional validations over here. For example, close_notes should be "Autoclose" or closed_by should be specific user etc.

 

find_real_file.png

Thanks,

Parag Mone

Parag,

 

Does the user need to have the role sn_esm_agent for this to work?  And do I need to update the state value on the condition to match my values for the state?

 

find_real_file.png

parag-mone
ServiceNow Employee
ServiceNow Employee

Hi,

You will need to use correct state IDs based on the state flows that you have defined.

Alternatively, if you are on Kingston, you can make use of the Flow Designer to auto-close resolved cases. Flow Designer enables process owners to use natural language to automate approvals, tasks, notifications, and record operations without having to code  Please take a look at the product documentation.

https://docs.servicenow.com/bundle/kingston-servicenow-platform/page/administer/flow-designer/concept/flow-designer.html

 

find_real_file.png

 

Thanks,

Parag

We had a similar issue where the scheduled job set up by our implementation vendor stopped working.  I wasn't able to successfully troubleshoot the scheduled job issue but using the flow designer instead worked and was simple to implement. Thanks!