Hello everyone!
Welcome back to another week of Kaizen!
We released the Queries feature sometime back and hope you have tried it out.
A little gist about this feature before we move on to our post.
Zoho CRM's Queries feature enables dynamic data retrieval from both CRM modules and external services, facilitating seamless integration and informed decision-making.
Key Components
- Sources: Pre-configured sources like Modules and COQL are available, with the option to add custom sources via REST APIs.
- Queries: Fetch data by selecting modules, writing COQL statements, or specifying REST API endpoints, headers, and parameters.
- Variables: Incorporate variables in criteria, COQL statements, and endpoints to pass dynamic values during execution.
- Schema: Auto-generated schemas define the structure of query responses, with editable paths, field types, and labels for customization.
- Serializer: Utilize JavaScript to manipulate and customize query responses, ensuring data is in the desired format for further processing.
Types of Queries
- Modules: Retrieve records by selecting specific modules and fields, applying conditions to filter data as needed.
- COQL (CRM Object Query Language): Write SQL-like queries to fetch data, supporting complex operations like joins and aggregations.
- REST API: Fetch data from external services by specifying endpoints, headers, parameters, and connections.
With the Queries feature, you can efficiently access and display relevant data within Zoho CRM, enhancing workflow efficiency and decision-making capabilities.
A little insight into Functions before we dive into today's Kaizen!
Functions, Queries, you get the connection, right? Read on!
Many of us use Functions in Zoho CRM extensively to perform our business logic and customize the way things work in Zoho CRM. You can use Functions in blueprints, workflows, Circuits etc.
Let's say you have a function that gets the employee records from the Employees module. Technically, the function executes an API call or an integration task, and gives a response.
The response can be a string or map(JSON), depending on how the function is written and where it is used. Since functions can be used in many places, the same response format may not be the right one to be used in a circuit or a workflow.
This is where the genie 'crmAPIResponse' object comes into picture!
The power of the 'crmAPIResponse' object
The crmAPIResponse object to be returned in the CRM function should encapsulate the response in a way that it can be used in Queries, Circuit, workflow etc. The details needed are encapsulated as a map. It should include details like crmStatusCode, status, message, body in order to construct the desired format for handling data and customizing the error handling logic.
Let's consider the following example function where we use the getRecordByID integration task.
The response of the integration task contains all the fields in that module, but we want only certain parts of the response JSON and also add custom error messages, to be used in other components like Queries, Circuits, etc,.
Here is the code.
{ leadId = "3652397000018025772"; // Replace with a valid Lead ID
// Initialize customAPIResponse map customAPIResponse = map();
// Fetch lead details crmResponse = zoho.crm.getRecordById("Leads", leadId);
// Log the raw response for debugging info "CRM API Raw Response: " + crmResponse;
// Validate and process the response to include only the required fields if (crmResponse != null && crmResponse.containsKey("id")) { // Extract required fields filteredData = map(); filteredData.put("id", crmResponse.get("id")); filteredData.put("Last_Name", crmResponse.get("Last_Name")); filteredData.put("Email", crmResponse.get("Email"));
customAPIResponse.put("crmStatusCode", 200); customAPIResponse.put("status", "success"); customAPIResponse.put("message", "Lead data retrieved successfully."); customAPIResponse.put("body", filteredData); // Include only filtered data } else { customAPIResponse.put("crmStatusCode", null); customAPIResponse.put("status", "error"); customAPIResponse.put("message", "Invalid or null response from Zoho CRM API."); customAPIResponse.put("body", null); }
// Return the customAPIResponse return {"crmAPIResponse": customAPIResponse};
} |
Here, you can see that we have parsed the response of the integration task to get the Last_Name, Email, and record ID using the crmResponse.get("field_API_name") statement and constructed the customAPIresponse object using the customAPIResponse.put("key", "value") statement.
The statement return {"crmAPIResponse": customAPIResponse}; returns the response body as depicted in the previous statements.
Response to the info "CRM API Raw Response: " + crmResponse; statement
"CRM API Raw Response: {"Owner":{"name":"Patricia Boyle","id":"3652397000000186017","email":"p.boyle@zylker.com"},"$field_states":null,..}}} |
Response of the return {"crmAPIResponse": customAPIResponse}; statement
{ "crmAPIResponse": { "crmStatusCode": 200, "status": "success", "message": "Lead data retrieved successfully.", "body": { "id": "3652397000018025772", "Last_Name": "Math", } } } |
Error response
{ "crmAPIResponse": { "crmStatusCode": null, "status": "error", "message": "Invalid or null response from Zoho CRM API.", "body": null } } |
You can see that we have used the crmAPIResponse object to get only the required keys from the response and constructed a much simpler response.
Finally, to today's Kaizen!
We've established that you can use the crmAPIResponse object to construct responses in a way that's suitable to be consumed at another place like a circuit or a workflow.
We also know that Queries allows you to have sources of the REST API type to fetch data from various sources. This means that you can have a standalone function that is enabled as a REST API as a source in Queries.
Let's see an example.
There is a simple function that uses the getRecords integration task to get the records from the Employees module.
I have used the crmAPIresponse object to construct a response as shown in the following code.
string standalone.getRecords() { result = zoho.crm.getRecords("Employees"); response = Map(); response.put("status_code",200); response.put("body",{"code":"success","details":result,"message":"function executed successfully"}); return {"crmAPIResponse":response}; } |
The response of this function is a string in the crmAPIResponse object as shown in this image.
- Save this function and enable REST API.
- You can see that the domain is https://www.zohoapis.com. To be able to use this in a Query, register this domain in Trusted Domain.
- To create a REST API type source, you must add the source. Go to Setup > Developer Hub > Queries > Sources tab.
- Click Add Source and give the details like the name, base URL, headers, and parameters under the Information section.
- Click Save.
- Go to the Queries tab and click Add Query.
- For Source, choose the source you just added.
- Under Information, enter the name, API name of the query.
- For the endpoint, enter the API Key URL of the function.
- Enter the parameter name and values in the Parameter field.
Click Add Serializer if you want to serialize the response. In this example, I have serialized the response to include only the Name, Email, and Position fields in the output. The result contains the 'body' object that we returned in the crmAPIResponse object of the function.
- Click Save to save the serialization.
- Click Next to view the schema of the query. Make changes as required.
- Save the query.
You can now use this query in Canvas or associate it with Kiosk to solve your business needs.
Let us see how the crmAPIResponse object in the function affects the response of the query.
Query with the function without crmAPIResponse:
Let's consider that the function getRecords() does not use the crmAPIResponse object. In that case, the function returns a response that is a string. This response string cannot be serialized or used elsewhere.
Schema of the query without crmAPIResponse:
Query with the function with crmAPIResponse and serialization:
The same getRecords() function that uses the crmAPIResponse object allows you to construct the response as JSON. You can serialize this response easily and use it in a circuit, query, workflow etc.
You can see here that the response is now a JSON.
Schema of the query with crmAPIResponse:
In conclusion, you can use the crmAPIResponse object in Functions to construct the desired response and use the REST API-enabled function as a source in Queries.
Leveraging the advantage of the crmAPIResponse object in functions and using it in Queries increases the prospect of solving many more business cases easily and customize more efficiently.
We hope you liked this post and found it useful. Let us know your thoughts in the comments.
If you'd like us to cover any other topic in this series, feel free to comment or reach us at support@zohocrm.com.
Cheers!
------------------------------------------------------------------------------------------------------------------------------------------------
See Also