0

Rest API - SOQL Query : When I Query i am able to get all user's record instead of mine only. Is there any way to filter for get records of current user only.

7
  • did you try filtering out the records using "createdBy"? Commented Apr 23, 2015 at 6:53
  • I do not know how to get User ID for filtering. I have tried like this : "SELECT Id FROM User Where Id=CreatedById". But its not working. Commented Apr 23, 2015 at 7:02
  • can you please elaborate your example? Are you querying on standard/custom object? Do you need to filter the records by the current logged in user id or by some specific user? Commented Apr 23, 2015 at 7:08
  • Query : SELECT Id, IsPrivate, Name, OwnerId, CreatedDate, CreatedById, LeadSource, Type FROM Opportunity. When I Query I get Thousands of record. And that records is not mine. I want only My records. Commented Apr 23, 2015 at 7:11
  • The problem is that using rest api we can get only 2000 records at time. In 2000 records only 5 records is mine. Commented Apr 23, 2015 at 7:13

4 Answers 4

1

While doing authentication with salesforce, will return response as per bellow :

{"id":"https://login.salesforce.com/id/00D28000000HpKw***/00528000000HsA7***","issued_at":"1429777622492","scope":"api","instance_url":"https://ap2.salesforce.com","token_type":"Bearer","signature":"HHORb4H9yZ6+fjYtn6OLBbeRvof9n4TuaXpYVh2qgck=","access_token":"00D28000000HpKw!AQcAQA8emoHQ8upI19zT_Yr4ctNAyE5lxc8yv6uFZbnszyNJOiF1ONC1ZsTcvoOtFduxxSEEquJdAoyq2E1Sn5mugFWqqUrJ"}

Response contains "Userid" at the end of "id" string (starts with 005).

Userid : "00528000000HsA7***" Use that UserID in filter as per bellow :

/services/data/v29.0/query?q=SELECT+Name+from+Opportunity+where+OwnerId=+'00528000000HsA7***'
2
  • excellent! and where did you get this info from?! Commented Apr 23, 2015 at 11:04
  • Obviously from you..! @RajivBhatt Commented Apr 23, 2015 at 11:54
0

If you want the user record then for standard fields no soql query required. you can directly use UserInfo class and its appropriate methods.

UserInfo Methods: http://www.salesforce.com/us/developer/docs/dbcom_apex230/Content/apex_methods_system_userinfo.htm

If you want other object records which are created by current user then try this:

Id currentUserId = UserInfo.getUserId();

List oppList = [SELECT Id, IsPrivate, Name, OwnerId, CreatedDate, CreatedById, LeadSource, Type FROM Opportunity where createdById=:currentUserId ];

1
  • Fields,Object Name And Filter Query is dynamically generated from .net program as per user's selection and user-wise its different-different. So, i think SOQL query is most convenient way.. Commented Apr 23, 2015 at 10:21
0

CreateBy wont help. Try this instead. List testList = [SELECT Id, IsPrivate, Name, OwnerId, CreatedDate, CreatedById, LeadSource, Type FROM Opportunity where createdById=:currentUserId ];

-1
SELECT AccountId
 From User
 WHERE username=:UserInfo.getUsername())

Select accountId then check if these records were created by current user in apropriate WHERE clause in your query (you can nest it)

1
  • 1
    SOQL query is not support "UserInfo.getUsername()". Only Apex class/code support like this. I want to do exactly like this in SOQL query. I am fetching record throw Rest API Query call from .net web application. Commented Apr 23, 2015 at 7:05

You must log in to answer this question.

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