A separate article as a follow-on from my previous article in this series Zoho Survey & Zoho Analytics: Query to generate individual responses and grouped pages which may have grown a little but just wanted to record how to receive a Zoho Survey response in a Zoho CRM REST API function.
Why?
As above, didn't want to overload a single article with all the answers of one development but this one may be referred to separately. I want a Zoho Survey when submitted to be parsed by a Zoho CRM REST API function.
How?
First, I'll go through the steps of setting up the Zoho CRM function, we'll convert it to a REST API function which will give us a URL and then we'll configure the Zoho Survey to trigger a webhook when submitted.
Pre-Amble
Just need a few fields on the CRM contact record to store the survey metadata
- Login to ZohoCRM with permissions to edit modules
- Go to Setup > Modules and Fields > Contacts > Layout > Standard
- Add the section: "Latest Survey"
- Add to it the fields "Response ID", "Survey ID", "Scheduled Report Generation Time"
- Save and close
the CRM function
You should know how to set up a CRM function as a REST API function but here goes a quick recap:
- Login to ZohoCRM as a user who has permission to create functions
- Create a CRM function, I'm calling mine "Fn - Submit Suspect Survey - Webhook" with internal name as fn_SubmitSuspectSurvey_Webhook and the parameter is a string called crmAPIRequest
- Give it the following code:
copyraw
string standalone.fn_SubmitSuspectSurvey_Webhook(String crmAPIRequest) { /* ******************************************************************************* Function: string standalone.fn_SubmitSuspectSurvey_Webhook(String crmAPIRequest) Label: Fn - Submit Suspect Survey - Webhook Trigger: Webhook when a suvey is submitted in Zoho Survey Purpose: Push the calculated score and results from Analytics into the corresponding Zoho CRM record. Inputs: String crmAPIRequest Outputs: - Date Created: 2025-09-19 (Joel Lipman) - Initial release - Identifies a survey by email Date Modified: ??? - ??? More Information: Within Zoho Surveys, configure the webhook to return the Response ID, Email, and Survey ID: 1. Login to Zoho Surveys 2. Click on the Survey > Builder > Hub > Triggers > Webhook > Manage 3. Post URL is the REST API URL of the ZohoCRM function (this function) 4. Set Request Body to JSON with name 'Survey' and within specify Response ID, Email, and Survey ID mapping as appropriate. 5. Click on 'Save' button at the bottom of the page CAVEAT: If you have multiple contacts in your CRM with the same email address, then you're stuffed. ******************************************************************************* */ v_WebhookBody = ifnull(crmAPIRequest.toMap().get("body"),""); v_WebhookBodyDecode = zoho.encryption.urlDecode(v_WebhookBody); v_WebhookBodyString = v_WebhookBodyDecode.getSuffix("Survey="); m_SurveyBody = v_WebhookBodyString.toMap(); // // find contact l_SearchContacts = zoho.crm.searchRecords("Contacts","Email:equals:" + m_SurveyBody.get("Email")); for each m_ContactResult in l_SearchContacts { if(!isNull(m_ContactResult.get("id"))) { m_UpdateContact = Map(); m_UpdateContact.put("Response_ID",m_SurveyBody.get("Response_ID")); m_UpdateContact.put("Survey_ID",m_SurveyBody.get("Survey_ID")); m_UpdateContact.put("Scheduled_Report_Generation_Time",zoho.currenttime.addHour(1).toString("yyyy-MM-dd'T'HH:mm:ss","Europe/London")); r_UpdateContact = zoho.crm.updateRecord("Contacts",m_ContactResult.get("id"),m_UpdateContact); info r_UpdateContact; } } /* OPTIONAL: Send yourself an email sendmail [ from :zoho.adminuserid to :"This email address is being protected from spambots. You need JavaScript enabled to view it." subject :"WEBHOOK: Survey has been submitted" message :crmAPIRequest ] */ // // build response to Zoho Survey (hard-coded a ok) m_ResponseHeader = Map(); m_ResponseHeader.put("status_code",200); return {"crmAPIResponse":m_ResponseHeader}; }
- string standalone.fn_SubmitSuspectSurvey_Webhook(String crmAPIRequest)
- {
- /* *******************************************************************************
- Function: string standalone.fn_SubmitSuspectSurvey_Webhook(String crmAPIRequest)
- Label: Fn - Submit Suspect Survey - Webhook
- Trigger: Webhook when a suvey is submitted in Zoho Survey
- Purpose: Push the calculated score and results from Analytics into the corresponding Zoho CRM record.
- Inputs: String crmAPIRequest
- Outputs: -
- Date Created: 2025-09-19 (Joel Lipman)
- - Initial release
- - Identifies a survey by email
- Date Modified: ???
- - ???
- More Information:
- Within Zoho Surveys, configure the webhook to return the Response ID, Email, and Survey ID:
- 1. Login to Zoho Surveys
- 2. Click on the Survey > Builder > Hub > Triggers > Webhook > Manage
- 3. Post URL is the REST API URL of the ZohoCRM function (this function)
- 4. Set Request Body to JSON with name 'Survey' and within specify Response ID, Email, and Survey ID mapping as appropriate.
- 5. Click on 'Save' button at the bottom of the page
- CAVEAT: If you have multiple contacts in your CRM with the same email address, then you're stuffed.
- ******************************************************************************* */
- v_WebhookBody = ifnull(crmAPIRequest.toMap().get("body"),"");
- v_WebhookBodyDecode = zoho.encryption.urlDecode(v_WebhookBody);
- v_WebhookBodyString = v_WebhookBodyDecode.getSuffix("Survey=");
- m_SurveyBody = v_WebhookBodyString.toMap();
- //
- // find contact
- l_SearchContacts = zoho.crm.searchRecords("Contacts","Email:equals:" + m_SurveyBody.get("Email"));
- for each m_ContactResult in l_SearchContacts
- {
- if(!isNull(m_ContactResult.get("id")))
- {
- m_UpdateContact = Map();
- m_UpdateContact.put("Response_ID",m_SurveyBody.get("Response_ID"));
- m_UpdateContact.put("Survey_ID",m_SurveyBody.get("Survey_ID"));
- m_UpdateContact.put("Scheduled_Report_Generation_Time",zoho.currenttime.addHour(1).toString("yyyy-MM-dd'T'HH:mm:ss","Europe/London"));
- r_UpdateContact = zoho.crm.updateRecord("Contacts",m_ContactResult.get("id"),m_UpdateContact);
- info r_UpdateContact;
- }
- }
- /* OPTIONAL: Send yourself an email
- sendmail
- [
- from :zoho.adminuserid
- to :"This email address is being protected from spambots. You need JavaScript enabled to view it."
- subject :"WEBHOOK: Survey has been submitted"
- message :crmAPIRequest
- ]
- */
- //
- // build response to Zoho Survey (hard-coded a ok)
- m_ResponseHeader = Map();
- m_ResponseHeader.put("status_code",200);
- return {"crmAPIResponse":m_ResponseHeader};
- }
- Save the function, hover over it and select "REST API", enable both options and copy the URL for the REST API
Configuring Zoho Survey
- Login to Zoho Surveys
- Click on the Survey > Builder > Hub > Triggers > Webhook > Manage
- Post URL is the REST API URL of the ZohoCRM function (this function)
- Set Request Body to JSON with name 'Survey' and within specify Response ID, Email, and Survey ID mapping as appropriate.
- Click on 'Save' button at the bottom of the page
Category: Zoho :: Article: 911
Add comment