Page 1 of 1

Show only records owned by logged in user?

Posted: Wed Feb 05, 2014 9:28 pm
by Joseph Dindinger

Using the standard Contact app as an example, how would I know which user is logged into Salesforce.com? The standard app returns all contacts that the user has access to, in my application I want to return only the records owned by the currently logged in user.

The login process happens in the SF_JSUtils Javascript provided, as shown below:

pre
function doLogin () {

Code: Select all

// as we render all pages into one file, let's check if this is a callback 
if (window.location.href.indexOf("access_token") == -1) { 

   var client_id = SF_Settings['client_id']; 
   var redirect_url = SF_Settings['redirect_url']; 
   var login_url = SF_Settings['login_url']; 

   var url = login_url + "?response_type=token&display=touch&display=touch&client_id=" +  
     client_id + "&redirect_uri=" + encodeURI(redirect_url); 

   // let's go to Salesforce to login 
   window.open(url, "_self");    
 } 
 else { // yes, this is a callback 
   // save token, url into local storage 
   saveAuthCodeInLocalStorage();  
   // navigate to page 
   Appery.navigateTo('mypage', {reverse: false}); 

 } 

}
/pre

I would imagine that the solution would be to grab the userid returned somewhere in the code above and store it in a local variable, which can then be used to create the SOQL call. But I cannot figure out how. I imagine this has been done before?


Show only records owned by logged in user?

Posted: Wed Feb 05, 2014 10:29 pm
by Maryna Brodina

Hello! Please check what data is returned by authorization service in Salesforce http://www.salesforce.com/us/develope.... If this data help you to do request you need, save it in saveAuthCodeInLocalStorage function in localStorage.


Show only records owned by logged in user?

Posted: Thu Feb 06, 2014 4:52 am
by Joseph Dindinger

No, unfortunately, that does not return the user ID. Has anybody done that before? I can't imagine I am the first to need this?


Show only records owned by logged in user?

Posted: Thu Feb 06, 2014 11:57 am
by Kateryna Grynko

Hi Joseph,

What errors do you see in console and in network tab in requests? Please post some screenshots.


Show only records owned by logged in user?

Posted: Thu Feb 06, 2014 3:49 pm
by Joseph Dindinger

Hi Katya,

No errors. The problem is that all of the users see the same records. I want to filter on the records that they own, so that each user sees different records. Does that make sense?


Show only records owned by logged in user?

Posted: Fri Feb 07, 2014 9:07 am
by Kateryna Grynko

Hi Joseph,

You can change plug-in and Facebook example as you need. This documentation may help you, there are some examples of what you need: https://developers.facebook.com/docs/...


Show only records owned by logged in user?

Posted: Wed Feb 12, 2014 12:58 am
by Joseph Dindinger

Thank you for all your suggestions. It turns out that Maryna was correct at the beginning of the post. It WAS returning the correct information, I just was not seeing it.

Here is the final code, in case anybody every runs into this issue like I did.

pre
function saveAuthCodeInLocalStorage () {

Code: Select all

var url_params = []; 
var params = window.location.href.slice(window.location.href.indexOf('#')+1); 
params = params.split('&');    

for (var i in params) { 

   var item = params[i].split('='); 
   url_params[item[0]] = item[1]; 

   // find the ID for the current user 
   if(i == 2) { 
       var sUserID = item[1].substr(item[1].length - 18); 
       localStorage.setItem('user_id', sUserID); 
   }  
} 

 // save token with type: Bearer 
localStorage.setItem('access_token', 'Bearer ' + decodeURI(url_params.access_token)); 
// save service URL, needed to invoke services 
localStorage.setItem('instance_url', decodeURIComponent(url_params.instance_url)); 
// clear the token from URL 
//window.location.replace(SF_Settings['redirect_url']+"#");  

}
/pre


Show only records owned by logged in user?

Posted: Wed Feb 12, 2014 1:10 am
by Alena Prykhodko

Joseph, thanks for sharing that.


Show only records owned by logged in user?

Posted: Thu Nov 13, 2014 4:49 am
by Adi Argov

can you provide how/when and where you call saveAuthCodeInLocalStorage () ?
I tried after salesforceLogin(), but I don't get the ID back to storage.
Thanks for your help,


Show only records owned by logged in user?

Posted: Thu Mar 19, 2015 9:32 am
by Pavan Varma

Hi,
May I know where to call saveAuthCodeInLocalStorage () method. Please help me..