Interface for invoking Salesforce.com REST calls over SSL with OAUTH authentication. This interface is designed to simplify the interaction between mbed devices and salesforce.com web services.

Dependencies:   HTTPClient-SSL MbedJSONValue

Dependents:   StatusReporter

Revision:
17:6c774354b599
Parent:
16:3d160f224084
Child:
18:7dc9b949bbc3
--- a/SalesforceInterface.h	Tue Sep 23 20:26:24 2014 +0000
+++ b/SalesforceInterface.h	Tue Sep 23 22:01:43 2014 +0000
@@ -202,12 +202,42 @@
      logger->turnLEDGreen();
  }
  
+ void Test_create_external_id(ErrorHandler *logger,SalesforceInterface *sf) {
+     logger->log("\r\n\r\nExecuting create(ExternalID)");
+     logger->turnLEDPurple();
+     
+     // create a new record
+     MbedJSONValue new_record;
+     new_record[external_id_field_name] = external_id_field_value;
+     
+     // DEBUG
+     logger->log("create(ExternalID): new record: %s",new_record.serialize().c_str());
+     
+     // create...
+     MbedJSONValue response = sf->createRecord(object_name,new_record);
+     
+     // display the result
+     char *result = (char *)response.serialize().c_str();
+     if (result != NULL && strlen(result) > 0 && strcmp(result,"null") != 0) {
+        // save off the token if we succeeded
+        logger->log("create(ExternalID): result: %s",result);
+        logger->log("create(ExternalID): http_code=%d",sf->httpResponseCode());
+        RESET_SML_BUFFER(record_id);
+        strcpy(record_id,(char *)response["id"].get<std::string>().c_str());
+     }
+     else {
+        // failure
+        logger->log("create(ExternalID): FAILED http_code=%d",sf->httpResponseCode());
+     }
+     logger->turnLEDGreen();
+ }
+ 
  void Test_read_by_external_id_and_value(ErrorHandler *logger,SalesforceInterface *sf) {
-     logger->log("\r\n\r\nExecuting Read(externalID)...");
+     logger->log("\r\n\r\nExecuting read(externalID)...");
      logger->turnLEDPurple();
           
      // DEBUG
-     logger->log("Read: reading: %s from %s with value %s",object_name,external_id_field_name,external_id_field_value);
+     logger->log("read(externalID): reading: %s from %s with value %s",object_name,external_id_field_name,external_id_field_value);
      
      // read (external ID)...
      MbedJSONValue response = sf->readRecord(object_name,external_id_field_name,external_id_field_value);
@@ -216,12 +246,12 @@
      char *result = (char *)response.serialize().c_str();
      if (result != NULL && strlen(result) > 0 && strcmp(result,"null") != 0) {
         // save off the token if we succeeded
-        logger->log("Read(externalID): result: %s",result);
-        logger->log("Read(externalID): http_code=%d",sf->httpResponseCode());
+        logger->log("read(externalID): result: %s",result);
+        logger->log("read(externalID): http_code=%d",sf->httpResponseCode());
      }
      else {
         // failure
-        logger->log("Read(externalID): FAILED http_code=%d",sf->httpResponseCode());
+        logger->log("read(externalID): FAILED http_code=%d",sf->httpResponseCode());
      }
      
      logger->turnLEDGreen();
@@ -253,8 +283,8 @@
      logger->turnLEDGreen();
  }
  
- void Test_upsert(ErrorHandler *logger,SalesforceInterface *sf) {
-     logger->log("\r\n\r\nExecuting upsert()");
+ void Test_upsert_external_id(ErrorHandler *logger,SalesforceInterface *sf) {
+     logger->log("\r\n\r\nExecuting upsert(ExternalID)");
      logger->turnLEDPurple();
      
      // update am existing record - assume "name" is the proper key for the record you wish to update...
@@ -262,7 +292,7 @@
      changed_record["name"] = updated_account_name;
      
      // DEBUG
-     logger->log("Upsert: upserted record: %s",changed_record.serialize().c_str());
+     logger->log("upsert(ExternalID): upserted record: %s",changed_record.serialize().c_str());
      
      // Upsert...
      bool updated = sf->upsertRecord(object_name,external_id_field_name,external_id_field_value,changed_record);
@@ -270,11 +300,11 @@
      // display the result
      if (updated) {
         // SUCCESS
-        logger->log("Upsert: successful! http_code=%d",sf->httpResponseCode());
+        logger->log("upsert(ExternalID): successful! http_code=%d",sf->httpResponseCode());
      }
      else {
         // failure
-        logger->log("Upsert: FAILED http_code=%d",sf->httpResponseCode());
+        logger->log("upsert(ExternalID): FAILED http_code=%d",sf->httpResponseCode());
      }
      logger->turnLEDGreen();
  }
@@ -346,7 +376,7 @@
         object_name             = "Account";       // use the account object
         account_name            = "ARM";           // add this record (name)
         updated_account_name    = "ARM Holdings";  // update the existing record's name to this
-        external_id_field_name  = "Device_c";      // External ID field name
+        external_id_field_name  = "Device__c";     // External ID field name
         external_id_field_value = "ABC123";        // External ID field value
         RESET_SML_BUFFER(record_id);               // buffer for the record's token
         
@@ -365,8 +395,11 @@
         // Perform a second Read to visually confirm the update above...
         Test_read(&logger,sf);
         
+        // Perform a Create (External ID)
+        Test_create_external_id(&logger,sf);
+        
         // Perform an Upsert
-        Test_update(&logger,sf);
+        Test_upsert_external_id(&logger,sf);
         
         // Perform a read of the external ID'ed specified by a given value
         Test_read_by_external_id_and_value(&logger,sf);
@@ -410,7 +443,7 @@
         Thread::wait(10*WAIT_TIME_MS);
      }
   }
- 
+
  * @endcode
  *
  */       
@@ -589,6 +622,9 @@
         
         // needed to replace substrings within std::string
         void replace(string& line, string& oldString, string& newString);
+        
+        // validate that http status is in the "n" range
+        bool httpResponseCodeInRange(int n);
  };
  
  #endif // _SALESFORCE_INTERFACE_H_
\ No newline at end of file