How to automatically close requests that are in a specific status.

How to automatically close requests that are in a specific status.

This post describes the use of a python script to close requests that are put in a specific status using Custom Schedules.
This showcases the use of a feature called 'Custom Schedules' that was released in the build 9300 of ServiceDesk Plus.

What is 'Custom Schedules'?
The Custom Schedules feature enables you to access any data in ServiceDesk Plus to perform customized actions periodically. Each custom schedule can associate Query Reports as arguments to a script file.  You can even synchronize ServiceDesk Plus data with other third-party applications to perform various actions, such as send mailer campaigns to requesters at regular intervals or update various attributes of requests periodically. You will find more information about this feature in the document available here.

Use Case:
Let us assume a scenario where the technicians reply to a request and put it in a custom status called 'Waiting for User'.The rules of the business demand that these requests be closed once in three days. This is achieved by running a query report in ServiceDesk plus which will return the ids of such requests.These are provided as input to a python script in JSON format.The script then uses request API to update those request's status as 'closed'.

Execution steps:
1. Go to Reports--> New Query Report and run the below query and save this report.

SELECT wo.WORKORDERID AS "Request ID" FROM WorkOrder wo LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID LEFT JOIN StatusDefinition std ON wos.STATUSID=std.STATUSID WHERE  ((std.STATUSNAME = 'Waiting for User') AND (( wo.CREATEDTIME >= <from_thisweek> ) AND ( wo.CREATEDTIME <= <to_thisweek> )))  AND wo.ISPARENT='1'

If you want to close all Resolved requests irrespective of the created time use this query

SELECT wo.WORKORDERID AS "Request ID" FROM WorkOrder wo LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID LEFT JOIN StatusDefinition std ON wos.STATUSID=std.STATUSID WHERE  ((std.STATUSNAME = 'Resolved')) AND wo.ISPARENT='1'

Note: Make use of limit 500 to the select query if you are trying to close more than 500 requests and run the schedule every 1 hour so that 5000 requests will be closed within 10 hours 
(SELECT wo.WORKORDERID AS "Request ID" FROM WorkOrder wo LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID LEFT JOIN StatusDefinition std ON wos.STATUSID=std.STATUSID WHERE  ((std.STATUSNAME = 'Waiting for User') AND (( wo.CREATEDTIME >= <from_thisweek> ) AND ( wo.CREATEDTIME <= <to_thisweek> )))  AND wo.ISPARENT='1' limit 500

SELECT wo.WORKORDERID AS "Request ID" FROM WorkOrder wo LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID LEFT JOIN StatusDefinition std ON wos.STATUSID=std.STATUSID WHERE  ((std.STATUSNAME = 'Resolved')) AND wo.ISPARENT='1' limit 500
)

This will give you the ID of requests that were created this week and are in "Waiting for User" status. Please update the  STATUSNAME   based on your requirements.

2. Downloaded the attached Zip file and move the files to ManageEngine/ ServiceDesk/ integration/custom_scripts folder
(Note: Don't replace functions.py and configuration.json files if already exist in that location)
3. Edit configuration.json file and update the application URL and the Technician Key
4. Configure the schedule under Admin--> Custom Schedule

I've attached a screenshot showing a sample implementation below.



So, now the script will run every three days at 9 AM and will execute the query report to find out all the requests that are created this week and have the status of Waiting for User and will close the request.

Note: You need to ensure that the mandatory fields for request closure are filled when your technician moves the request to 'waiting for User', else the request cannot be closed.

A quick tip, using Field and Form rules (On Field Change), you can set up a rule to mandate the fields that have to filled in prior to request closing so that when the technician tries to change the status to Waiting for User, he cannot change the status until the fields are filled in.

A short guide on setting up python is available here.

For build above 11200 use the Auto_Close_v3_latest.zip

                    New to ADSelfService Plus?