By Igor Androsov, Technical Architect
The other day I was working on a mobile app and needed to call a custom APEX service. After looking through SDK documentation and searching the web, I realized that a lot is written about REST APIs, mobile development and Salesforce Mobile SDK, but none give good examples of how to call a custom service. Moreover most resources talk about hybrid mobile apps, so little is discussed on native Objective C side. So, I decided to write this post to restore the balance in the mobile universe.
The app defines several async delegate responder methods to process the results, success or error:
In the didLoadResponsemethod the code processes the result to set the data that will be populated in a Table View, if the query returned any data.
That is all works great for standard CRUD operations on sObjects, but now what happens if we have a custom REST service hosted in Force.com. In most projects, calling a custom service is a base requirement. Normally this is a service written in APEX language with annotation to expose this class and its methods as a REST service. To create an APEX REST service we need to create an APEX class in the Force.com org, as the following example describes (More about rest APEX can be found in REST Developer Guide). In this example I have chosen to query Contacts and return a list of custom records in common wrapper format:
Now we need to write a simple wrapper class. This is useful in many cases to mix information of custom fields and sObjects defined in Salesforce.
After we save this code, Salesforce will expose MyAPEXService class as REST endpoint at /mycontact. The actual endpoint URL will look something like this:
Now for the fun part of building an Objective-C code to actually invoke this service from your mobile application. To send actual request we will use the same REST API send method as the standard code does. However, before we invoke APEX we need to manually set up the request object SFRestRequest. Mobile SDK provides some helper methods to set up standard CRUD requests automatically, so in standard calls that detail is hidden from the programmer. Here is an example of how we can set up request manually:
- The endpoint for custom services is/services/apexrest/
- The method – is standard REST methods such as GET, POST, DELETE etc. but in SDK you’re required to have their corresponding definition, in this case SFRestMethodGET.
Here it is complete list of these definitions (saves you a trip to github to search through code):
There are also parameters you can pass with this request. For purposes of this post I did not setup parameters in APEX to pass. To do so you would use NSDictionary queryParams, a simple name/value pair defining the parameters you want to pass to your service. This will be an asynchronous call that will return a response to one of your delegate methods we outlined earlier. A normal response will return to the didLoadResponse SFRestAPIDelegate delegate method. In this method we can process the returned results and set something on the UI for user to interact with. Something like this code below:
This is all you need to call your custom APEX REST service and build mobile apps leveraging Salesforce.com Mobile SDK. In conclusion I want to say that while there is not a lot of documentation on Mobile SDK and Native mobile apps for iOS and Android, it is a great tool to connect your mobile apps to the “cloud” and Salesforce, as well as build mobile apps for the Enterprise. The code examples in this writing are based on SDK version 1.5. The new version of Salesforce Mobile SDK is being released shortly, so stay tuned for more on that.