Form Change Handler
The change handler is triggered for an update made in any form field containing the boolean trigger_on_change as true. The change handler can be used to either add or remove fields in the form depending on the input made for a certain field.
A list of attributes is passed when a function is triggered. They are listed in the table below
Attribute Name | Data Type | Description |
user | map | Details of the user who has triggered the function. |
chat | map | Chat details in which the function is triggered. |
form | map | Details of the form object and the input values. |
target | map | Details of the field and the selected input that triggered the change |
Form Modification Object Syntax
{
"type": "form_modification",
"action": [
$action_object
]
}
Action Object Syntax
{
"type": "remove | clear | update | add_before | add_after | enable | disable",
"name" : reference_label_name
"input" : $input_element //applicable for add-before, add-after, update
}
Sample command code for the type remove|clear|update|add_before|add_after|enable|disable
inputs = list();
inputs.add({"type":"select","trigger_on_change":true,"name":"field","label":"Modifications","hint":"Pick a option","placeholder":"","mandatory":true,"options":{{"label":"Remove","value":"remove"},{"label":"Clear","value":"clear"},{"label":"Update","value":"update"},{"label":"Add Before","value":"add_before"},{"label":"Add After","value":"add_after"},{"label":"Enable","value":"enable"},{"label":"Disable","value":"disable"},{"label":"Update with Disabled","value":"updatewithdisabled"}},"value":"enable"});
inputs.add({"type":"select","name":"action","label":"Enable/Disable","hint":"Pick a action","placeholder":"","options":{{"label":"Enable","value":"enable"},{"label":"Disable","value":"disable"}},"value":"enable"});
response = {"text":"Hey"};
return {"type":"form","title":"Platform Demo","name":"ID","version":1,"actions":{"submit":{"type":"invoke.function","name":"form"}},"inputs":inputs};
Example: Get tasks assigned to a user in Zoho Projects
Let's consider a slash command /tasks that can pull up the list of assigned tasks to a user.
- Once a user executes the command, a form is displayed where they can select the project, task assignee and task status.
- code class="language-json">The task assignee form field is dependent on the project form field and will display the users who are a part of the chosen project.
- Once you choose these options, you will get the list of tasks assigned to the user and with the selected status.
/tasks command execution handler code
response = Map();
portal = zoho.projects.getPortals("[ENTER YOUR CONNECTION NAME]");
//info portal;
portalID = portal.get("portals").toMap().get("id");
allProjects = zoho.projects.getProjectDetails("[PORTAL ID]","active","[ENTER YOUR CONNECTION NAME]");
info allProjects;
projectlist = List();
for each project in allProjects
{
projectName = project.get("name");
id = project.get("id");
project = {"label":projectName,"value":id};
projectlist.add(project);
}
inputs = List();
inputs.add({"type":"select","name":"project","label":"Project Name","hint":"Choose a project","placeholder":"Pick a project","mandatory":true,"value":"projects","options":projectlist,"trigger_on_change":true});
return {"type":"form","title":"Get tasks","hint":"Get the list of tasks assigned to a user in a project!","name":"tasks","version":1,"button_label":"Get Tasks","action":{"type":"invoke.function","name":"projecttasks"},"inputs":inputs};
project tasks form function change handler
actions = list();
response = Map();
if(target.get("name").containsIgnoreCase("project"))
{
project_id = form.get("values").get("project").get("value");
getUsers = invokeurl
[
url :"https://projectsapi.zoho.com/restapi/portal/{Portal ID}/projects/" + project_id + "/users/"
type :GET
connection:"ENTER YOUR CONNECTION NAME"
];
info getUsers;
}
users = getUsers.get("users");
info users;
userList = List();
for each user in users
{
userName = user.get("name");
userId = user.get("id");
user = {"label":userName,"value":userId};
userList.add(user);
info userList;
actions.add({"type":"add_after","name":"project","input":{"type":"select","name":"taskstatus","label":"Task Status","hint":"Choose a task status","placeholder":"Choose a task status","mandatory":true,"value":"","options":{{"label":"Completed","value":"completed"},{"label":"Yet to Start","value":"notcompleted"},{"label":"All Tasks","value":"all"}}}});
actions.add({"type":"add_after","name":"project","input":{"type":"select","name":"taskassignee","label":"Task Owner","hint":"Choose a task owner","placeholder":"Choose a task owner","mandatory":true,"value":"","options":userList}});
}
return {"type":"form_modification","actions":actions};
project tasks function form submit handler code
response = Map();
projectID = form.get("values").get("project").get("value");
assignee = form.get("values").get("taskassignee").get("value");
status = form.get("values").get("taskstatus").get("value");
assignee_name = form.get("values").get("taskassignee").get("label");
taskParams = Map();
taskParams.put("status",status);
taskParams.put("owner",assignee);
getTasks = invokeurl
[
url :"https://projectsapi.zoho.com/restapi/portal/{Portal ID}/projects/" + projectID + "/tasks/"
type :GET
parameters:taskParams
connection:"ENTER YOUR CONNECTION NAME"
];
info getTasks;
taskInfo = getTasks.toMap().get("tasks");
info taskInfo;
rows = List();
for each task in taskInfo
{
row = Map();
row.put("Task ID",task.get("key"));
row.put("Task",task.get("name"));
row.put("Description",task.get("description"));
rows.add(row);
}
response = {"text":"Hey " + user.get("first_name") + " ! List of tasks assigned to " + assignee_name,"card":{"theme":"modern-inline","title":"Task List"},"slides":{{"type":"table","title":"","data":{"headers":{"Task ID","Task","Description"},"rows":rows}}}};
return response;
<