Custom Menu Actions

Custom functions can be executed using Custom Menu to perform a variety of user-defined actions within ServiceDesk Plus Cloud and external applications.

The following document will explain how to write custom functions for the Requests module specifically. However, you can follow the same instructions to write custom functions for Assets and Changes modules. The only change will be replacing 'requestObj' with 'assetObj' or 'changeObj' in the script, depending on your requirements.

 

Steps to Write a Custom Function for Custom Menu Actions

1. Go to SetupDeveloper Space > Custom Functions > Requests > Custom Menu Actions and click New custom function. (You can alternately write a custom function while creating/editing the Custom Menu.)

Before you start writing a new custom function, check out the prebuilt custom functions by clicking Import Sample Functions.

 


 

2. Provide a name and description for your custom function.

3. Write the custom function on the Deluge Script Editor by using the following pointers and then save the function:

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 a map value or an empty map (). Let's say the map value is requestObj. Changes that are made to 'requestObj' values will be updated in the request to which the Custom Menu applies.
 

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 in the following sample script that can be used to auto-close linked requests upon the closure of the reference request:

/*Gets the request status*/
status = requestObj.get("status").get("name");
returnMap = Map();
/*Checks if the request is closed*/
if(status.equals("Closed"))
{
	/*Gets the name of the portal in which the request is logged*/
	portal_name = context.get("instance");
	request_url = "/app/" + portal_name + "/api/v3/requests/";
	/*Checks if the request has been linked to other requests*/
	if(requestObj.get("has_linked_requests").equals("true"))
	{
		/*Specifies criteria to fetch the linked requests*/
		linked_request_criteria = {"field":"linked_to_request.request.id","condition":"is","value":requestObj.get("id")};
		not_closed_criteria = {"field":"status.name","condition":"is not","value":"Closed","logical_operator":"and"};
		/*Makes an API call to fetch the 10 oldest linked requests*/
		fetch_response = zoho.sdp.invokeurl
		[
			url :request_url
			type :GET
			parameters:{"input_data":{"list_info":{"row_count":"10","sort_field":"display_id","sort_order":"asc","search_criteria":{linked_request_criteria,not_closed_criteria}}}}
		];
		requests = fetch_response.getJSON("requests");
		request_ids = "";
		/*Gets the IDs of the requests that are fetched*/
		for each  request in requests
		{
			request_id = request.get("id");
			if(request_ids == "")
			{
				request_ids = request_id;
			}
			else
			{
				request_ids = request_ids + "," + request_id;
			}
		}
		if(request_ids != "")
		{
			/*Makes an API call to close the requests that are fetched*/
			close_response = zoho.sdp.invokeurl
			[
				url :request_url + "close"
				type :PUT
				parameters:{"ids":request_ids}
			];
			if(close_response.get("response_status").get(0).get("status") == "success")
			{
				returnMap.put("success",true);
				returnMap.put("message","Child requests are changed successfully");
			}
			else
			{
				returnMap.put("success",false);
				returnMap.put("message",close_response.get("response_status").get("message"));
			}
		}
	}
}
return returnMap;

 

As demonstrated in the above sample script, after the custom function is executed, you can optionally prompt the user of the success or the failure of the action execution by including the following section in the script:

{"success": true/false, "message": "<custom message>"}


 

Test Execution   

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

  1. Click Save & Execute Script displayed at the bottom of the script editor.
  2. Choose a sample request from the list of requests displayed, choose whether you want to get values from the selected request or define new values, and click Next.
  3. After your click Execute, the script will run and the output will be displayed.

You can similarly test custom functions added to the Custom Menu of Assets and Changes.
 

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;


Similarly, you can use info to debug code and print output of custom functions used in Custom Menu of Assets and Changes.

 

Home Page Actions

On the custom function list view page, you can use the Actions menu to bulk disable/enable and delete custom functions.

You can also view a custom function by simply clicking it. To perform actions on individual custom functions, use the Settings icon beside each custom function.