Zoho CRM custom function API request syntax - calling from Google Apps Script with custom arguments in Body/Payload

Zoho CRM custom function API request syntax - calling from Google Apps Script with custom arguments in Body/Payload

Putting this down here as it's taken me ages to figure out - hopefully can save time for somebody else.

Objective: call custom Zoho CRM function from Google Apps script, with custom arguments in the Body/Payload

Sample Google Apps script code
function zTestAPIJSONPayload(){
  
  // Make a POST request with a JSON payload.
  
  var formData = {"input1":"goes here", "input2":"56"};
  var options =
      {
        "method" : "post",
        "contentType" : "application/json",
        // this also works fine as an alternative format        "payload" : '{ "input1": "hereis the first input", "input2": " here is the second input"}'
        "payload" : JSON.stringify(formData) //doing this JSON.stringify conversion is ESSENTIAL or the Zoho CRM function doesn't recognise the input as JSON
      };
  try{
    
    var response = UrlFetchApp.fetch('https://www.zohoapis.com/crm/v2/functions/[FUNCTION NAME HERE]/actions/execute?auth_type=apikey&zapikey=1003[KEY HERE]', options);
    
    contentText = response.getContentText();
    console.log("contentText: " + contentText);  
  }
  catch(e)
  {
    console.log("Error is " + e);
  }
}

Sample Zoho custom function
body = crmAPIRequest.get("body");
i1 = body.get("input1");
i2 = body.get("input2");
sendmail
[
from :zoho.adminuserid
to :zoho.adminuserid
subject :"API function call crmAPIRequest"
message :"crmAPIRequest is " + crmAPIRequest
]
return "i1 = " + i1 + " and i2 = " + i2;

Things to ensure
  • The single argument for the custom function needs to be crmAPIrequest, type = string, like this:



CR