Deluge Scripting in Triggers and Life Cycle Actions

 

Trigger Actions

Trigger rules specify conditions under which predefined actions must be performed on incoming requests. These are useful for calling actions after the requests are created, especially for performing actions in other modules or in third-party applications. You can use trigger actions for Requests, Problems, Changes, Projects, Releases, Assets, CMDB.

Life Cycle Actions

Request Life Cycle in ServiceDesk Plus Cloud allows admins to formulate a request resolution process with built-in guidance for the help desk technician. A life cycle ensures efficient process adherence; you can establish a directional flow, minimize the scope for human errors, and provide privileged (role-based) access to status transitions. You can also configure life cycle for Problems and Assets.

To automate processes through request life cycle, you define actions that will be executed under pre-specified conditions. Custom function execution is one of the many different types of actions in request life cycle. 

 

Before you write custom functions for request triggers/life cycles, we recommend that you learn how to configure and use each of them.

Learn about Request Triggers

Learn about Request Life Cycle
 

Custom Function for Request Triggers and Life Cycle Actions 

Request triggers and life cycle actions use the same type of custom function with the input arguments and the return type alike. 

To write a custom function that can be used as a request trigger or life cycle action, follow the steps given below:

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


 

The custom function returns a 'void' value.

You cannot return any object from the custom function to modify the request. You can, however, modify the request by making API calls. 

 

Let's consider a sample script to create a new change with the subject of a request:

myInstance = context.get("instance");
subjectOfRequest = requestObj.get("subject");
resp = zoho.sdp.invokeurl[
    url: "/app/itdesk/api/v3/changes"
    type: POST
    parameters: {"input_data": {"change":{"title":subjectOfRequest}}}
]; 

 

Let's consider a scenario in which you must automate the creation of GoToMeeting events for specific requests. You can perform this action by using the following script in a Request Trigger. Before using this script, create a new service request called 'GoToMeeting Booking' with start and end times added as additional fields in the service request template, and change the same in the script.
 

Note that you can perform the same action by using Request Life Cycle.

 

gotomeeting_token = "<your goto meeting token>";
portal_name = "<your portal>";
start_time_udf_key = "<your adiitional date field key - to note start time>";
end_time_udf_key = "<your adiitional date field key - to note end time>";
/*Code logic from the below*/
template = requestObj.get("template").get("name");
if(template == "GoToMeeting Booking")
{
    subject = requestObj.get("subject");
    request_id = requestObj.get("id");
    start_time = requestObj.get("udf_fields").get(start_time_udf_key).get("value");
    end_time = requestObj.get("udf_fields").get(end_time_udf_key).get("value");
    meeting_start_time = start_time.toTime().toString("yyyy-MM-dd'T'HH:mm:ss'.000Z'");
    meeting_end_time = end_time.toTime().toString("yyyy-MM-dd'T'HH:mm:ss'.000Z'");
    gotomeeting_data = Map();
    gotomeeting_data.put("subject",subject);
    gotomeeting_data.put("starttime",meeting_start_time);
    gotomeeting_data.put("endtime",meeting_end_time);
    gotomeeting_data.put("passwordrequired","false");
    gotomeeting_data.put("conferencecallinfo","--from sdpod--");
    gotomeeting_data.put("timezonekey","string");
    gotomeeting_data.put("meetingtype","immediate");
    header = {"Authorization":gotomeeting_token,"Accept":"application/json","Content-Type":"application/json"};
    /*Below line triggers api call to gotomeeting and response provided by gotomeeting is captured in response_from_goto*/
    response_from_goto = postUrl("https://api.getgo.com/G2M/rest/meetings",gotomeeting_data + "",header + "",true);
    join_url = response_from_goto.toMap().get("joinURL");
    /*Below code creates notes in sdpod with the joinurl, which was got from gotomeeting's response*/
    sdpod_response = zoho.sdp.invokeurl
    [
        url :"/app/" + portal_name + "/api/v3/requests/" + request_id + "/notes"
        type :POST
        parameters:{"input_data":{"request_note":{"description":"A meeting has been scheduled in gotomeeting by the system. Join Url for gotomeeting is <a href='" + join_url + "'>" + join_url + "</a>"}}}
    ];
}

 

Let's consider another scenario in which you must automate the creation of change requests for service/incident requests. You can do so by defining a request trigger. The following script creates a change for a request by copying the request subject to the change title and associates the change with the request.
 

Note that you can perform the same action by using Request Life Cycle.

 

/*Provide your portal name*/
portal_name = "<your portal>";
/*Triggers an API call to create a change with a title copied from the corresponding request's subject.*/
new_change_response = zoho.sdp.invokeurl
[
    url :"/app/" + portal_name + "/api/v3/changes"
    type :POST
    parameters:{"input_data":{"change":{"title":requestObj.get("subject")}}}
];
/*Triggers an API call to associate the change with the corresponding request */
associate_request = zoho.sdp.invokeurl
[
    url :"/app/" + portal_name + "/api/v3/changes/" + new_change_response.get("change").get("id") + "/initiated_by_requests"
    type :POST
    parameters:{"input_data":{"change_initiated_by_request":{{"request":{"id":requestObj.get("id")}}}}}
];

 

 

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;   

 

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