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.

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

  • But how would it work if I am already authorized as a admin? – kurunve Aug 9 at 9:35
  • added in answer – salesforce-sas Aug 9 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 at 11:06
  • You will need integration user created and store those credentials in .net. Records accessibility depends on profile of integration user – salesforce-sas Aug 9 at 11:29
  • 1
    But in this case, I still won't be able to get records for a specific user, respecting records visibility. – Lyroys Aug 9 at 11:36

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.