Zoho CRM 使用 JSON / Coldfusion 插入记录

Zoho CRM Insert Record using JSON / Coldfusion

在最终找到 Zoho OAuth 身份验证后,我现在面临着在尝试插入新记录时调试错误代码的问题。

<cfsavecontent variable="JSONData">
{
  "data": { 
    "Last_Name": "Smith",
"First_Name": "John",
    "Email": "john@smith.com",
    "Phone": "0123456789",
    "Lead_Source": "PPC",
"Build_Cost_text": "50",
"Estimated_Sales_Value": "100",
"Stage_of_Build_Work": "Not Started",
"Work_Starting_Date_text": "01/06/2021",
"Number_of_Units": "1",
"Address_of_Development": "123 Any Street",
"Gross_floor_area_m2": "100",
"Development_Use": "Self Build",
"Please_share_any_additional_information": "Just testing stuff",
    "Company": "Smith Widgets"
      }
  }
</cfsavecontent>


<cfhttp method="POST" url="https://www.zohoapis.com/crm/v2/Leads" result="zohopostresult">
<cfhttpparam type="header" name="Authorization" value="Zoho-oauthtoken #gettoken.access_token#">
<cfhttpparam type="header" name="content-type" value="application/json" />
<cfhttpparam type="body" value="#JSONData#" />
</cfhttp>

使用上述代码返回的错误是;

{"code":"INVALID_DATA","details":{"expected_data_type":"jsonarray","api_name":"data"},"message":"invalid data","status":"error"}

如果我使用 cfdump / deserializeJSON 查看 'JSONData' 变量,它的输出就很好。我整天都在看这个,所以我很可能会错过一些正盯着我看的东西。

为了理智,不要使用<cfsavecontent>,使用<cfset>

<cfset payload = {
    "data": {
        "Last_Name": "Smith",
        "First_Name": "John",
        "Email": "john@smith.com",
        "Phone": "0123456789",
        "Lead_Source": "PPC",
        "Build_Cost_text": "50",
        "Estimated_Sales_Value": "100",
        "Stage_of_Build_Work": "Not Started",
        "Work_Starting_Date_text": "01/06/2021",
        "Number_of_Units": "1",
        "Address_of_Development": "123 Any Street",
        "Gross_floor_area_m2": "100",
        "Development_Use": "Self Build",
        "Please_share_any_additional_information": "Just testing stuff",
        "Company": "Smith Widgets"
    }
}>

<cfdump var="#payload#">

<cfhttp method="POST" url="https://www.zohoapis.com/crm/v2/Leads" result="zohopostresult">
  <cfhttpparam type="header" name="Authorization" value="Zoho-oauthtoken #gettoken.access_token#">
  <cfhttpparam type="header" name="content-type" value="application/json" />
  <cfhttpparam type="body" value="#SerializeJSON(payload)#" />
</cfhttp>

请注意,payload 将是一个结构 - ColdFusion 支持结构文字语法,顺便说一下,它也接受 JSON 的格式。

为了将数据作为 JSON 发送,您需要将其从结构序列化为字符串。这就是 SerializeJSON(payload) 的目的。

错误很明显。

"expected_data_type":"jsonarray"

所以你需要发送一个数组,而不是一个对象。我不知道 Zoho 的 API 定义,但我的猜测是

<cfset payload = {
    "data": [{
        ...
    }]
}>

<cfset payload = [{
    "data": {
        ...
    }
}]>