I am trying to use the Salesforce Mobile SDK for native Android. Requirement:
- Allow any user with Salesforce account to login
- Fetch his/her contacts list
- Fetch particular contact details
Please let me know if those are not part of the Mobile SDK.
I followed https://github.com/forcedotcom/SalesforceMobileSDK-Android:
- Cloned it
- Installed NPM and installed the required submodules
- Created a new Android application using native forcedroid
- Changed the bootconfig.xml values with the values of my connected app.
It logs in fine but when I try to fetch the contacts, it says:
The Rest API is not enabled for this Organization
The login response seems to be fine. Client object json credentials:
{
"clientId": "*******",
"loginUrl": "https://login.salesforce.com",
"identityUrl": "https://login.salesforce.com/id/99VVHJHGF5688/00548000003yOKeAAM",
"instanceUrl": "https://ap2.salesforce.com",
"userId": "**********",
"orgId": "*********",
"communityUrl": null,
"refreshToken": "*************",
"accessToken": "************",
"communityId": null,
"userAgent": "SalesforceMobileSDK/4.3.0 android mobile/7.0 (Nexus 6P) SFTest/1.0 Native uid_32aea9bdde1b8b7e"
}
Code to get Contacts list:
public void onFetchContactsClick(View v) throws UnsupportedEncodingException {
sendRequest("SELECT Name FROM Contact");
}
private void sendRequest(String soql) throws UnsupportedEncodingException {
RestRequest restRequest = RestRequest.getRequestForQuery(ApiVersionStrings.getVersionNumber(this), soql);
client.sendAsync(restRequest, new AsyncRequestCallback() {
@Override
public void onSuccess(RestRequest request, final RestResponse result) {
result.consumeQuietly(); // consume before going back to main thread
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
listAdapter.clear();
JSONArray records = result.asJSONObject().getJSONArray("records");
for (int i = 0; i < records.length(); i++) {
listAdapter.add(records.getJSONObject(i).getString("Name"));
}
} catch (Exception e) {
onError(e);
}
}
});
}
@Override
public void onError(final Exception exception) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,
MainActivity.this.getString(SalesforceSDKManager.getInstance().getSalesforceR().stringGenericError(), exception.toString()),
Toast.LENGTH_LONG).show();
}
});
}
});
}
BTW I am currently using a trial version of salesforce.