Getting Logged-in userId in Salesforce Android SDK – Mobile App

If you are a developer who works with Salesforce, you are well aware that using the user Id of the currently logged in user is vital to a lot of custom development.  The same could be said for mobile applications that are built on the Salesforce platform.  I have already expressed myself over the lack of documentation for something that seems so important for both iOS and Hybrid applications.  Now comes Android’s turn.

My previous Android applications were built before the SDK using the REST API or did not have a need for the current user Id.  Today I found myself looking for where it was by fumbling my way through the SDK code after looking around Google for a while.  Here is how you access the user Id in your application.

I’m assuming you have an instance of RestClient within your activity.  Use that instance to create a new instance of ClientInfo.  ClientInfo has a property for the userId.  Your code would look something like this (client is the instance of RestClient):

ClientInfo ci = this.client.getClientInfo();
String userId = ci.userId;
Log.d(“YOURAPP”, “userId:  “ + userId);

There you have it!  Not very difficult but somewhat of a pain if you don’t know where to find it or where to look.

This entry was posted in Technology and tagged , , . Bookmark the permalink.