Salesforce REST Services

This recipe demonstrates how it’s possible to use Salesforce REST services, from within a MobileCaddy application.

Versions

  • Based off starter-app shell-ionic / shell-ionic-sidemenu v1.6
  • Requires MC_Resource v1.1.0

Context/Concept/Why

In some scenarios it may be possible to enrich a users experience by providing extra functionality, or intelligence, into your application through the consumption of the REST services that are served from Salesforce.com. An example might be providing a recent chatter feed, or enabling an online SFDC contact search.

In all instances, though, the designer/developer should be fully aware that online consumption of REST services should be approached with a progressive enhancement methodology in mind, as all MobileCaddy applications should fulfil all critical duties even when offline. This is the Offline First mantra.

How To

We can access the REST services using the McRestService angular service. As well as providing a simple promise-based wrapper for us to call, it will also take care of aligning our REST calls with our existing authenticated session with Salesforce. This allows the developer to call the REST services without worrying about setting up and managing authentication to Salesforce.com.

1) Include the McRestService

  1. To access Salesforce REST service from a MobileCaddy application, the McRestService angular service can be used. This service is available in the shell applications (v1.6+), and can be retrofitted or upgraded by copying the latest file from the MobileCaddy Ionic Shell repository into your project’s www/js/services directory.
  2. At the time of writing the McRestService makes use of a MobileCaddy module called appDataUtils. For this to be available the following needs to be in the www/js/services/service.module.js file, prior to the final line of the file. This appDataUtils is also needed to be added to the final line.
    This file can be used as a reference.

2) Using the McRestService

In our example we will implement a view and controller to query the chatter feeds on our Salesforce org, and also to enable a SOQL query of the contacts on Salesforce.com.

Our template is very basic, and consists of 2 buttons and one text input, that we can put our contact name search string into. We also have an ion-list to list out the contact results.

The controller for this view has two functions exposed. This is the first and is called to display the latest chatter post. This function uses the McRestService’s request method, and we pass in the HTTP method, URL to call, and Content Type.

We have a second function that uses the query method of McRestService. This method can be used to pass in SOQL queries. Our example will pass in a LIKE query against our Contact object.

These are examples are cut-down to highlight the interaction to the McRestService, and in reality we’d likely want to have further UX handling for failure scenarios, and the case where no matching contacts are found.

A full working implementation of this controller is as follows;

3) Configure the Platform

Although the REST calls will now work fine within CodeFlow, they will fail when running on the devices without some platform configuration.

Obtain the Correct Whitelist Pattern

  1. With your application running on device, use the Mobile Table Inspector (in the Settings –> Admin Functions area) to view the appSoup.
  2. Search for the startPageURL, and make a note of this value

Add the Whitelisted CORS Origin

  1. Go to Settings –> Security Controls –> Manage whitelisted CORS origins
  2. Add a new whitelist pattern using the value noted above for the Origin URL Pattern.

salesforce-cors-setting

Limitations

At time of writing the following call types are supported via the McRestService contained in the shell applications. Note, these are not updated during the lifetime of your apps, and to gain any extra support you may need to update the version in your project with the one at https://github.com/MobileCaddy/shell-ionic/blob/master/www/js/services/mcrest.service.js

Troubleshooting

If your application appears to be failing to call the platform successfully when deployed to a device, when it worked OK in CodeFlow, then run the app up with remote debugging enabled. Doing this will enable access to the Network tab in the Developer Tools. Doing this should enable you to see the raw HTTP call/response.

Warnings/Notes

REST calls will fail on device (not in CodeFlow) if the CORS setting has not been correctly setup. This can be diagnosed and confirmed via remote debugging, and the presence of the following type of error;
XMLHttpRequest cannot load https://eu11.salesforce.com/services/data/v36.0/chatter/feeds/news/me/feed-elements. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘https://c.eu11.visual.force.com’ is therefore not allowed access.

References