Salesforce.com interface to directly access Salesforce.com

Dependencies:   HTTPClient-SSL MbedJSONValue

Dependents:   df-2014-salesforce-hrm-k64f

Fork of SalesforceInterface by Doug Anson

Revision:
16:3d160f224084
Parent:
15:89044c68ad36
Child:
17:6c774354b599
--- a/SalesforceInterface.cpp	Tue Sep 23 17:35:34 2014 +0000
+++ b/SalesforceInterface.cpp	Tue Sep 23 20:26:24 2014 +0000
@@ -11,7 +11,7 @@
  * The above copyright notice and this permission notice shall be included in all copies or
  * substantial portions of the Software.
  *
- * THE SOFTWARE IS PROVtokenED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
@@ -298,9 +298,9 @@
  }
 
  // READ: a specific record in Salesforce.com
- MbedJSONValue SalesforceInterface::readRecord(char *object_name,char *record_id) {
+ MbedJSONValue SalesforceInterface::readRecord(char *object_name,char *record_id,char *record_value) {
     ALLOC_BUFFER(output_buffer);
-    char *reply = this->readRecord(object_name,record_id,output_buffer,MAX_BUFFER_LENGTH);
+    char *reply = this->readRecord(object_name,record_id,record_value,output_buffer,MAX_BUFFER_LENGTH);
     MbedJSONValue response;
     if (reply != NULL && strlen(reply) > 0) parse(response,reply);
     return response; 
@@ -311,6 +311,11 @@
     return this->updateRecord(object_name,record_id,(char *)record.serialize().c_str());
  }
  
+ // UPSERT: update/insert an External ID record in Salesforce.com
+ bool SalesforceInterface::upsertRecord(char *object_name,char *external_id_field_name,char *external_id_field_value,MbedJSONValue &record) {
+     return this->upsertRecord(object_name,external_id_field_name,external_id_field_value,(char *)record.serialize().c_str());
+ }
+ 
  // CREATE: a record in Salesforce.com
  char *SalesforceInterface::createRecord(char *object_name,char *json_data,char *output_buffer,int output_buffer_length) {
      // parameter check
@@ -347,7 +352,7 @@
  }
 
  // READ: a specific record in Salesforce.com
- char *SalesforceInterface::readRecord(char *object_name,char *record_id,char *output_buffer,int output_buffer_length) {
+ char *SalesforceInterface::readRecord(char *object_name,char *record_id,char *record_value,char *output_buffer,int output_buffer_length) {
      // parameter check
      if (object_name != NULL && strlen(object_name) > 0 && record_id != NULL && strlen(record_id) > 0 && output_buffer != NULL && output_buffer_length > 0) {
          // first we have to ensure that we have valid salesforce token
@@ -366,6 +371,12 @@
                 str_url += "/";
                 str_url += record_id;
                 
+                // add the record value (if present)
+                if (record_value != NULL && strlen(record_value) > 0) {
+                    str_url += "/";
+                    str_url += record_value;
+                }
+                
                 // DEBUG
                 DEBUG("readRecord: URL: %s",str_url.c_str());
                 
@@ -434,6 +445,62 @@
      }
      return false;  
  }
+ 
+ // UPSERT: update/insert a specific External record in Salesforce.com
+ bool SalesforceInterface::upsertRecord(char *object_name,char *external_id_field_name,char *external_id_field_value,char *json_data) {  
+     // parameter check
+     if (object_name != NULL && strlen(object_name) > 0 && json_data != NULL && strlen(json_data) > 0) {
+         // first we have to ensure that we have valid salesforce token
+         if (this->haveSalesforceToken()) {        
+            // get the sobjects url
+            ALLOC_BUFFER(url);
+            char *sf_url = this->getSalesforceURL("sobjects",url,MAX_BUFFER_LENGTH);
+            if (sf_url != NULL && strlen(sf_url) > 0) {   
+                // convert to string
+                string str_url(sf_url);
+                         
+                // add object name that we want to create a record in
+                str_url += object_name;
+                
+                // add the external field name token
+                str_url += "/";
+                str_url += external_id_field_name;
+                
+                // add the external field value token (if not NULL)
+                if (external_id_field_value != NULL && strlen(external_id_field_value) > 0) {
+                    str_url += "/";
+                    str_url += external_id_field_value;
+                }
+                
+                // HTTPClient does not support PATCH, so we have to use POST with a special added parameter
+                str_url += "?_HttpMethod=PATCH";
+                
+                // DEBUG
+                DEBUG("upsertRecord: URL: %s DATA: %s",str_url.c_str(),json_data);
+                
+                // now invoke with POST with JSON data type
+                ALLOC_SML_BUFFER(output_buffer);
+                char *reply = this->invoke(str_url.c_str(),json_data,strlen(json_data)+1,output_buffer,MAX_SMALL_BUFFER_LENGTH);
+                
+                // DEBUG
+                DEBUG("upsertRecord: http status=%d",this->httpResponseCode());
+                
+                // return our status
+                if (this->httpResponseCode() == 204) return true;
+                return false;
+            }
+         }
+         else {
+             // dont have a valid salesforce token
+             this->logger()->log("upsertRecord: error - no valid salesforced token was found...");
+         }
+     }
+     else {
+         // invalid or NULL parameters
+         this->logger()->log("upsertRecord: error - invalid or NULL parameters...");
+     }
+     return false;  
+ }
   
  // DELETE: a specific record in Salesforce.com
  bool SalesforceInterface::deleteRecord(char *object_name,char *record_id) {