Populating a Soup

To add Salesforce records to a soup for offline access, use the REST API in conjunction with SmartStore APIs.

When you register a soup, you create an empty named structure in memory that’s waiting for data. You typically initialize the soup with data from a Salesforce organization. To obtain the Salesforce data, you use Mobile SDK’s standard REST request mechanism. When a successful REST response arrives, you extract the data from the response object and then upsert it into your soup.

Hybrid apps use SmartStore functions defined in the force.js library. In this example, the click handler for the Fetch Contacts button calls force.query() to send a simple SOQL query ("SELECT Name, Id FROM Contact") to Salesforce. This call designates onSuccessSfdcContacts(response) as the callback function that receives the REST response. The onSuccessSfdcContacts(response) function iterates through the returned records in response and populates UI controls with Salesforce values. Finally, it upserts all records from the response into the sample soup.

iOS native apps use the SFRestAPI protocol for REST API interaction. The following code creates and sends a REST request for the SOQL query SELECT Name, Id, OwnerId FROM Account. If the request is successful, Salesforce sends the REST response to the requestForQuery:send:delegate: delegate method. The response is parsed, and each returned record is upserted into the SmartStore soup.

For REST API interaction, Android native apps typically use the RestClient.sendAsync() method with an anonymous inline definition of the AsyncRequestCallback interface. The following code creates and sends a REST request for the SOQL query SELECT Name, Id, OwnerId FROM Account. If the request is successful, Salesforce sends the REST response to the provided AsyncRequestCallback.onSuccess() callback method. The response is parsed, and each returned record is upserted into the SmartStore soup.