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 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.
-> Query for one week data after the status change: 
SELECT wo.WORKORDERID AS "Request ID",wo.helpdeskid "portalid" 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'

-> Query for 3 days data after the change of status:
MSSQL:
SELECT wo.WORKORDERID AS "Request ID",max(wo.helpdeskid) "portalid"   FROM WorkOrder wo 
LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID 
LEFT JOIN StatusDefinition std ON wos.STATUSID=std.STATUSID  
LEFT JOIN WorkOrderHistory woh ON wo.WORKORDERID = woh.WORKORDERID
LEFT JOIN WorkOrderHistoryDiff wohd ON woh.HISTORYID = wohd.HISTORYID 
where  wohd.COLUMNNAME in ( 'STATUSID' )  and std.statusname ='Awaiting Response'   and (wo.ISPARENT='1')
group by wo.workorderid having DATEDIFF(day, dateadd(s,datediff(s,GETUTCDATE() ,getdate()) +(max(woh.OPERATIONTIME)/1000),'1970-01-01 00:00:00'),GETDATE()) > 3
PGSQL:
SELECT wo.WORKORDERID AS "Request ID",max(wo.helpdeskid) "portalid"   FROM WorkOrder wo 
LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID 
LEFT JOIN StatusDefinition std ON wos.STATUSID=std.STATUSID  
LEFT JOIN WorkOrderHistory woh ON wo.WORKORDERID = woh.WORKORDERID
LEFT JOIN WorkOrderHistoryDiff wohd ON woh.HISTORYID = wohd.HISTORYID 
where  wohd.COLUMNNAME in ( 'STATUSID' )  and std.statusname ='Awaiting Response'   and (wo.ISPARENT='1')
group by wo.workorderid having extract(epoch from(now()::TIMESTAMP -  to_timestamp(max(woh.OPERATIONTIME)/1000)::TIMESTAMP))/3600/24 > 3

This will give you the ID of requests that were created in this week and are in "Waiting for User" status.Please update the   STATUSNAME   based on your requirement.
2. Downloaded and place the script file in the ManageEngine/ ServiceDesk/ integration/custom_scripts folder.
3. Update the  application URL and the Technician Key  in the script with the help of the comments provided within.
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 setup 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.
                  New to ADManager Plus?

                    New to ADSelfService Plus?