2

We are trying to work with the Sharpspring API to synch up some of the data we have on their system with our local database. They have some example code in PHP (which I'm only vaguely familiar with) but I would like to convert it to Coldfusion since that's the language I know much better. The requests also need to be submitted in JSON format (which I'm very new at).

I've been successful getting their "getleads" method to work in Coldfusion (so I know it can be done), however, I haven't been able to get their "updateleads" method to work in Coldfusion, more than likely because I have the structure of the JSON messed up. Here's the error I get:

{"result":{"updates":[{"success":false,"error":{"code":208,"message":"Object is malformed (expected array, got scalar)","data":[]}},{"success":false,"error":{"code":208,"message":"Object is malformed (expected array, got scalar)","data":[]}},{"success":false,"error":{"code":208,"message":"Object is malformed (expected array, got scalar)","data":[]}}]},"error":[{"code":208,"message":"Object is malformed (expected array, got scalar)","data":[]},{"code":208,"message":"Object is malformed (expected array, got scalar)","data":[]},{"code":208,"message":"Object is malformed (expected array, got scalar)","data":[]}],"id":"04E511DD-A430-D9B8-367D78679476F6A6","callCount":"2","queryLimit":"50000"}

From their documentation, the structure of the JSON for that method should be this (but note that the Lead ID is the record ID and the Request ID appears to be any unique ID so I send a GUID):

{
  "method":"updateLeads",
  "params":
    {"objects":
      [
        {"id":"<lead ID>","firstName":"fooUpdate","lastName":"barUpdate"}
      ]
    },
  "id":"<your request ID>"
}

Here is what I'm trying to do in Coldfusion to accomplish that:

<cfscript>
    variables.dataFields = {};
    variables.dataFields['id'] = CreateUUID();  // The API requires an ID for each request
    variables.dataFields['method'] = "updateLeads"; 
    variables.dataFields['id'] = "123456789";   // The ID of the record you want to update
    variables.dataFields['firstName'] = "John";  
    variables.dataFields['lastName'] = "Smith";  
    variables.dataFields = serializejson(variables.dataFields);
    writeoutput(dataFields);            // Write the JSON to see what's submitted
</cfscript> 

<cfhttp url="http://<the sharpspring API URL>" method="POST" result="resultName">
    <cfhttpparam name="accountID" type="url" value="<Our Account ID>">
    <cfhttpparam name="secretKey" type="url" value="<Our Account Key>">
    <cfhttpparam type="header" name="Content-Type" value="application/json" />
    <cfhttpparam type="body" value="#variables.dataFields#" />
</cfhttp>

<!--- Display the API return values --->
<br><br>
<cfoutput>
#resultName.filecontent#
</cfoutput>

Does anyone have any suggestions on how I need to tweak that code to get the correct JSON structure that the API is requiring? Any help would be appreciated!

For reference, here is the PHP example code (for getting the lead, not updating one like I need) that Sharpspring provides if that helps....

<?php
   /** Get all leads with a limit of 500 results */
   $limit = 500;                                                                         
   $offset = 0;                                                                          
                                                                                          
   $method = 'getLeads';                                                                 
   $params = array('where' => array(), 'limit' => $limit, 'offset' => $offset);          
   $requestID = session_id();       
   $accountID = '<account-id>';
   $secretKey = '<secret-key>';                                                     
   $data = array(                                                                                
       'method' => $method,                                                                      
       'params' => $params,                                                                      
       'id' => $requestID,                                                                       
   );                                                                                            
                                                                                                 
   $queryString = http_build_query(array('accountID' => $accountID, 'secretKey' => $secretKey)); 
   $url = "http://api.sharpspring.com/pubapi/v1/?$queryString";                             
                                                                                                 
   $data = json_encode($data);                                                                   
   $ch = curl_init($url);                                                                        
   curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                              
   curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                                                  
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                               
   curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                   
       'Content-Type: application/json',                                                         
       'Content-Length: ' . strlen($data),
       'Expect: '                                                        
   ));                                                                                           
                                                                                                 
   $result = curl_exec($ch);                                                                     
   curl_close($ch);                                                                              
                                                                                                 
   echo $result;                                                                                 
  
?>
Paul B
  • 93
  • 5
  • 1
    You set `variables.dataFields['firstName']` on the same level as f.e. `method`, but that’s not where that value belongs, according to your sample. You need to create `params` as an object on that level first, and then inside that under the key `objects` an array, that in turn gets filled with objects containing id, firstName and lastName. – CBroe Jun 17 '21 at 13:33
  • Also, since CF uses the same shortcut syntax for creating structures and arrays, just create the structure exactly the same way as the example, replacing the key values as needed. https://trycf.com/gist/bb04fd99164763eb97d98a4c6cb64deb/acf2018?theme=monokai – SOS Jun 17 '21 at 14:33
  • 1
    Thank you so much! What you sent in TryCF didn't work as is for me, however, the JSON structure fix was just enough for me to figure out the rest. Super appreciate it! – Paul B Jun 17 '21 at 15:28
  • Glad you got it working. You should post an answer with an example of what *does* work, in case someone else has the same question :) – SOS Jun 17 '21 at 15:47

0 Answers0