0

I have to retrieve data from Salesforce (using default endpoint for Rest Api SOQL queries) for a specific user. Now for getting Access_token I'm using "grant_type: password" with "System User" who has full privileges. Unfortunately, in this approach I can only retrieve all data. I could use specific user credentials but I don't want to store them in my database. Is there any workaround, which let me retrieve data on behalf of a specific user, respecting roles and sharing settings?

Thanks in advance.

2

1 Answer 1

1

You can use authorization as 'Authorization: Bearer <your_session_id>'. You can get session id using UserInfo class

httpReq.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());

added based on comments

As you said SOQL through REST:

HttpRequest req = new HttpRequest();
req.setEndpoint('https://ap2.salesforce.com/services/data/v35.0/query/?q=select+name+,+type+from+account');
req.setMethod('GET');

string autho = 'Bearer '+ userInfo.getsessionId();
req.setHeader('Authorization', autho);

Http http = new Http();
HTTPResponse res = http.send(req);

Here the records will be retrieved based on logged in user.

When you login as another user, only records accessable to that user will be retrieved

7
  • But how would it work if I am already authorized as a admin?
    – kurunve
    Aug 9, 2019 at 9:35
  • added in answer Aug 9, 2019 at 9:46
  • Thank you for the response. I should precise one thing: the main problem here is a fact, that I have to retrieve data in Microservices (.Net), so I can't use UserInfo class. I'm trying to find an implementation which I could use in ASP.NET Core, so outside Salesforce Org.
    – Lyroys
    Aug 9, 2019 at 11:06
  • 1
    But in this case, I still won't be able to get records for a specific user, respecting records visibility.
    – Lyroys
    Aug 9, 2019 at 11:36
  • 1
    Sorry about that. I mean, that I'm building a web and mobile app where users (who use salesforce) can log in and check their data (for example Accounts). Because of that, I'm creating API which should retrieve proper data. In this API I'm using grant_type: password authentication. The point is, that I want to retrieve only data which should be displayed to a logged user, like in Salesforce, respecting Role hierarchy, Sharing Rules etc. The easiest way would be using credentials of currently logged user, to retrieve his Access_Token, but I don't want to store their passwords.
    – Lyroys
    Aug 9, 2019 at 12:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .