While creating a message handler custom function, you can test the function to see if the desired output is received. For testing, you can provide the expected value and see if the expected output value is obtained.
For the message handler custom function, you can test two types of message inputs: Device Message and Device connection Status.
Here is the sample code that comes pre-populated in the code editor when you create a message handler custom function.
- headerMap = header.toMap();
- deviceid = headerMap.get("deviceid");
- receivedtime = headerMap.get("occurred_timestamp");
- topic = headerMap.get("topic");
- communication_status_flag = headerMap.get("data_internal");
- if(communication_status_flag == false && topic == "telemetry")
{ payload = message.toMap();
if(payload.containKey("data")) {
deviceMessage = payload.get("data");
}
else {
deviceMessage = payload;
}
else if(communication_status_flag == true)
{ deviceMessage = Map(); } return deviceMessage;
The above stated code directly fetches data from the payload of the incoming message and returns it as the output. First, we'll test to see if the code works as intended.
Click the Save and Execute button in the top right. This will open the Execute Function dialogue box.![](https://help.zoho.com/galleryDocuments/edbsn42c6cfae68ad4f811097104d1e7a596d88f449dc715d70e1b95b65488d590d64ee3fc7170fe808e97e60e82f54cc1eb4?inline=true)
Select Device Message in the Message Type field.
Provide {'temp':25} in the Message field.
Provide any value as Device ID since it is only for testing purpose.
Click Save.
The function will now be executed and the value {'temp':25} will be displayed as the output.
Now, we will handle the incoming data by adding 5 to the message value and test the code.
Add the following line of code inside the else statement while handling the payload.
- new_temp = payload.get('temp')+5;
- deviceMessage = Map();
- deviceMessage.put('temp', new_temp);
Updated Code:
- headerMap = header.toMap();
- deviceid = headerMap.get("deviceid");
- receivedtime = headerMap.get("occurred_timestamp");
- topic = headerMap.get("topic");
- communication_status_flag = headerMap.get("data_internal");
- if(communication_status_flag == false && topic == "telemetry")
{
payload = message.toMap(); if(payload.containKey("data"))
{ deviceMessage = payload.get("dat } else
{ new_temp = payload.get('temp')+5;
deviceMessage = Map();
deviceMessage.put('temp', new_temp); } else if(communication_status_flag == true)
{ deviceMessage = Map(); }
- return deviceMessage;
Once the code is updated,
Click Save and Execute in the top right.
Select Device Message in the Message Type field.
Provide {'temp':25} in the Message field.
Provide any value as Device ID since it is only for testing purpose.
Click Save.
![](https://help.zoho.com/galleryDocuments/edbsn8783366a5e5400d0085b81d43f21d439c302d43c2870fa691465877ec6f79ad6f9630696632ed5e48430e0937ed31cdc?inline=true)
The function will now be executed and the value {'temp':30} will be displayed as the output.