Im currently having a play with the Salesforce JqueryMobile pack and have got stuck trying to get hold of the current user Id. (Pack: http://www2.developerforce.com/mobile/getting-started/html5/#jquery-heroku)

I should point out this is hosted externally to salesforce and using OAuth to connect.

The forcetk object contains a session_id that contains the main account id of the dev org I'm using but no details on who I am.

Its probably right in front of me but would appreciate any help / source code you guys have!


The way you can get userId, is via IdentityURL coming in the Oauth access/refresh token response.

Here is the sample JSON response:


If you notice the url for "id" key, its the identity url, and you can parse it to get both userId and orgId(if required). UserId is the one with prefix "005" i.e. 005x00000012Q9P

This is standard approach to save a webservice call to get userId, its used in salesforce mobile sdk as well, so you can be assured of the solution.

Now you need to figure out the place where your oauth flow, most probably you must be doing something like this

function onSalesforceOAuthLogin(event) {
logToConsole("onSalesforceOAuthLogin: Salesforce ready");
forcetkClient = new forcetk.Client(event.data.clientId, event.data.loginUrl);
forcetkClient.setSessionToken(event.data.accessToken, event.data.apiVersion, event.data.instanceUrl);

Add an extra method called setIdentityUrl() in forcetk.js, inspire from setRefreshToken, and parse the "id" param data in it.

I will try the same soon, but in next 1-2 days.

| improve this answer | |

Though this is kind of earlier post I'm posting this answer since it helped me.
Don't know whether earlier SDK versions didn't give this. I'm using SALESFORCE_MOBILE_SDK_VERSION = "2.1.0";
In the index.html file we have the function salesforceSessionRefreshed(creds) where it is called by onDeviceReady() function with the code line cordova.require("salesforce/plugin/oauth").getAuthCredentials(salesforceSessionRefreshed, getAuthCredentialsError);.

This function looks like,

function salesforceSessionRefreshed(creds) {
    forcetkClient = new forcetk.Client(credsData.clientId, credsData.loginUrl, null,
    forcetkClient.setSessionToken(credsData.accessToken, apiVersion, credsData.instanceUrl);

If you have a look at the calling function(getAuthCredentials in cordova.force.js) you can see there you have the userId as well available. So in above code snippet you can simply access it.


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.