简体   繁体   中英

SOQL query doesn't retrieve all contacts

I am working on a code snippet that is supposed to import all my contacts from Salesforce via SOQL.

Here is what I do when I want to import all Salesforce contacts for my user 00520000001IbXZAA0

First SOQL:

SELECT Id FROM account WHERE ownerid = ‘00520000001IbXZAA0’

Then I get all the account id where I am a team member:

SELECT AccountId FROM accountteammember WHERE UserId = ‘00520000001IbXZAA0’

Then I build an array with my ids and from my 2 previous requests and get all contacts this way:

SELECT FirstName, LastName, Account.Name FROM Contact WHERE AccountId IN myarrayofids ORDER BY CreatedDate ASC

This approach seems to get most of my contacts but I can still see some contacts appear in Salesforce but that are not retrieved with these 3 queries. Am I missing something here?

I have only very few knowledge of how Salesforce work but I suppose that if I retrieve all contacts where I am a team member or owner, I should retrieve all contacts I am able to see on Salesforce or am I missing something here?

Many thanks

The reason you are not able to see all records otherwise is as some might be shared with you via OWD or using role based sharing, manual sharing or apex sharing. If you are just looking for a list in csv or anything, the easiest thing you can do here is create a report to display all contacts and share it with this user. Login into Salesforce as this "00520000001IbXZAA0" user. Run the report and export into csv/excel. If you wish to do this in code, write a class using "with sharing" keyword and run the following query "Select Id, FirstName, LastName .... From Contact". This will return this user all the records she/he has access to.

Try these above solutions and let me know if they work for you. Happy to help..!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM