Deluge Scripting in Business Rules

 

Business Rules

Business rules automate various tasks, such as delivering incident and service requests to technician support groups; assigning a technician, status, or priority to a request; or even sending email notifications.

Business rules are site-based, run on all incoming requests, and can be applied to requests when they are created, edited, or deleted. If an incoming request fulfills certain pre-specified conditions, business rules ensure that certain actions are performed automatically. 

Business rules consist of two main parts namely Conditions and Actions and you can use custom functions to configure both conditions and actions. 

Before you write custom functions for business rules, we recommend that you learn how to configure and use business rules in ServiceDesk Plus Cloud.

 

Custom Function for Business Rule Conditions 

To write a custom function that can be used as business rule condition, follow the steps given below:

As demonstrated in the following screenshot, 'requestObj' and 'context' will be passed as arguments for the custom function:
 

 

'requestObj' will store the details of the request on which the business rule must act. 'context' will store the current instance and the logged-in user details. 

If the custom function in the business rule is configured to execute upon request edit, then the context object will additionally contain 'initialObj' and 'modifiedFields'. 'initialObj' will store the original request values and 'modifiedFields' will store the fields whose values are modified. 

You must write the custom function such that it returns a boolean value. Actions that you configure in the business rule will be executed if the custom function returns 'true'.

 

Let's consider a sample script that returns 'true' if the request subject contains 'New issue':
 

subject = requestObj.get("subject");
if(subject.contains("New issue")){
    return true;
}
return false; 

 

Let's consider a scenario in which you must prevent request duplication by users and also restrict them to create only 3 requests of the same category in a span of 24 hours. You can automate this check by using the following script in a business rule condition.

 

if(requestObj.containsKey("category"))
{
    category_name = requestObj.get("category").get("name");
    request_creation_time = requestObj.get("created_time").get("value");
    request_creation_time = request_creation_time.toDate().toLong();
    check_upto_time = request_creation_time - 24 * 60 * 60 * 1000;
    requester_email = requestObj.get("requester").get("email_id");
    criteria = "{list_info:{search_criteria:{field:category.name,values:['" + category_name + "'],condition:like,children:[{field:created_time.value,condition:GT,logical_operator:and,value:'" + check_upto_time + "'},{field:requester.email_id,condition:like,logical_operator:and,values:['" + requester_email + "']}]}}}";
    response = zoho.sdp.invokeurl
    [
        url :"/app/itdesk/api/v3/requests"
        type :GET
        parameters:{"input_data":criteria}
    ];
    requests = response.get("requests");
    if(requests.toJSONList().size() > 2)
    {
        return true;
    }
}
return false;

 

 

Test Execution of Scripts 

After writing the custom function, you can test it by following the steps given below: 

Test execution of a custom function does not affect any data in your production account. However, if you make any API calls (by using zoho.sdp.invokeurl or invokeurl) while testing the custom function, the API will be invoked. Make sure you don't invoke an API that might result in unintended consequences.

 

 

Debugging Tip 

Deluge allows debugging through an action called info. You can use this action to print the output of your custom function. 

For example, to understand the structure of requestObj and context, you can simply run the following script and study the response.

info requestObj;  
info context;
return true; 

 

 

Custom Function for Business Rule Actions 

To write a custom function that can be used as business rule action, follow the steps given below:

 As demonstrated in the following screenshot, 'requestObj' and 'context' will be passed as arguments for the custom function:
 

 

You must write the custom function such that it returns 'requestObj'. Also, the returned value will be of the Map data type.

Changes that are made to 'requestObj' values will be updated in the request on which the business rule acts. 

 

Note that you can update only the following request fields in requestObj: subject, resolution, mode, group, item, level, impact, service_category, update_reason, priority, udf_fields, impact_details, subcategory, status, request_type, description, urgency, technician, category.

 

The new field values must be returned from the custom function in a specific format as demonstrated below: 

Let's consider a sample script to update request subject to 'Firewall Upgrade' and Priority to 'High'.
 

requestObj.put("subject", "Firewall Upgrade");
requestObj.put("priority", {"name": "High"});
return requestObj;

 

Let's say you want to assign high priority to requests from Network/Internet category. You can automate this action by using the following script in a business rule action:
 

if(requestObj.containsKey("category"))
{
    if(requestObj.get("category").get("name") == "Internet" || requestObj.get("category").get("name") == "Network")
    {
        requestObj.put("priority",{"name":"High"});
    }
}
return requestObj;

 

 

Test Execution of Scripts 

After writing the custom function, you can test it by following the steps given below: 

 If you make any API calls by using zoho.sdp.invokeurl or invokeurl while testing the custom function, the API will be invoked. Make sure you don't invoke an API that might result in unintended consequences.

 

 

Debugging Tip 

When you test a custom function, you can debug the code and print the output by using a statement called info. For example, to understand the structure of requestObj and context, you can simply run the following script and study the response.

info requestObj;
info context;
return true; 

 

 

For a quick summary of how to use custom functions across different features in ServiceDesk Plus Cloud, visit this link.