Notification Profile Custom function sample code
Below is an example of how notification-profile custom functions can be used to automate actions when alarm-related events occur. In this scenario, the notification profile includes two custom functions: one that creates a new record whenever a gateway device is disconnected, and another that escalates a specific field value from High to Urgent when the alarm’s severity changes. Because the notification profile is triggered by these alarm events, both custom functions run automatically, ensuring timely record creation and proper escalation without requiring manual intervention.
This custom function creates a work order every time a gateway device disconnects. This is configured as the notification action.
json_string = {"workorder":{"Name":"Reconnect " + gateway_name,"Gateway_Name":gateway_name,"Gateway_Location":gateway_location,"Order_Status":"High","Alarm_ID":alarm_id},"Gateway_ID":gateway_id};
response = invokeurl
[
url :"https://app12345akyrw.zohoiot.com/iot/v1/workorder"
type :POST
parameters:json_string + ""
connection:"iot-connection"
];
info response;
This custom function updates work order status to Urgent if the issue isn't fixed within the define limit. This is configured as the escalation action.
filter = {"condition_text":"1","conditions":{"1":{"field":{"api_name":"Alarm_ID"},"comparator":"equal",
"value":alarm_id}}};
get_url = appUrl + "/iot/v1/workorder?filter=" + encodeUrl(filter);
response = invokeurl
[
url :get_url
type :GET
connection:"iot"
];
workorder_id = response.get("workorder").get(0).get("id");
status_response = response.get("workorder").get(0).get("Order_Status").get("display_value");
if(status_response == "High")
{
json_string = {"workorder":{{"id":workorder_id,"Order_Status":{"display_value":"Urgent"}}}};
response_put = invokeurl
[
type :PUT
parameters:json_string + ""
connection:"iot"
];
info "RESPONSE:: " + response_put;
}
Associating to a notification profile
Once the custom function is created, it becomes available for selection when configuring a notification profile. You can associate the custom function in the Choose Actions field while creating or editing a notification profile.