function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pooja9426Pooja9426 

Query to get all the records owned by a user

Hi All,
I am trying to get all the records owned by a user. I trying below SOSL query.
List<String> listOfObjects = new List<String>();
listOfObjects.add('Account'+'(id,OwnerId where OwnerId = '+userId+')')
listOfObjects.add('Candidate__c'+'(id,OwnerId where OwnerId = '+userId+')')
FIND {Username} IN ALL FIELDS RETURNING listOfObjects

but this query is returning no records. Please help me with this.

Thanks in advance
AbhishekAbhishek (Salesforce Developers) 
When it comes to transferring ownership of records (https://help.salesforce.com/articleView?id=data_about_transfer.htm&language=en_us&type=5), you will want to ensure that you properly get child objects and be aware of some of the side effects of transferring, such as:


When transferring accounts and their related data in Professional, Enterprise, Unlimited, Performance, and Developer Editions, all previous access granted by manual sharing, Apex managed to share, or sharing rules is removed. New sharing rules are then applied to the data based on the new owner. The new owner may need to manually share the transferred accounts and opportunities as necessary to grant access to certain users.


Spend some time reading up on some of that so you don't have any issues. Once you are ready to do the transfer, you will be much better served using the built-in Salesforce tool for mass transferring records (https://help.salesforce.com/articleView?id=admin_transfer.htm&language=en_us&type=5) then attempting to do this through direct data manipulation with SOQL and DML. Some objects will have additional options when changing ownership (https://help.salesforce.com/HTViewHelpDoc?id=account_owner.htm&language=en_US) that SOQL/DML will not take into account as well.


This post on StackExchange recommends putting the ID filter within the returned data - you might want to try this approach out:

FIND {term} IN ALL FIELDS RETURNING Account(Id, Name, Owner.Name WHERE Owner.Name LIKE 'Mark%')

http://salesforce.stackexchange.com/questions/92435/sosl-on-lookup-fields-not-working


For reference check this too,

https://developer.salesforce.com/forums/?id=906F000000095PbIAI


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
Pooja9426Pooja9426
Hi Abhishek,

Thanks for your reply. I tried below approach 

FIND {term} IN ALL FIELDS RETURNING Account(Id, Name, Owner.Name WHERE Owner.Name LIKE 'Mark%')

I used owner name as a term in above query, but it returned 0 records. Could you please help me with what needs to be used as a term in query. I need to get all the records from all sObjects which are owned by a user.

Thanks