SOQL query doesn't retrieve all contacts - salesforce

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..!!

Related

SOQL Profile Names, Permission Set, User Name

I am accustomed to using SQL, and have been thrown into the SOQL Salesforce realm quickly without much idea on how to use SOQL.
I have been trying to create a simple query to get the above fields all in one table so that I can see for each user their profile name, user name and the permission set that they have. This is a simple query but using workbench I have not been able to get it. Thanks!
SELECT FirstName,LastName,PortalRole, ProfileId, Profile.Name, Profile.userType, Username, UserRoleId,UserType, Profile.PermissionsAccessCMC, Profile.PermissionsActivateContract,Profile.PermissionsActivitiesAccess FROM User
That is you can get all the permission from profile table. In the above SOQL, I have added some of the permission fields from profile.

SalesForce SOQL join Leads to Contact

I'm trying to query data out of SalesForce Lead using SOQL and JOIN and obtain contact information where for the Lead Created through the contacts object.
I'm not sure which objects to use how to go about doing it.
Does anyone know of a detail schema of what can be used with SOQL relationship queries.
I've tried some examples from the following links but don't know how to do the same with Leads, contacts:
https://developer.salesforce.com/blogs/developer-relations/2013/05/basic-soql-relationship-queries.html
First of all you didnt mention properly what you want
List<lead> info = [Select * from Lead where Id = 'Id name of that Particular record of lead'];
system.debug(info);
now you can see all information of that particular lead in logs.

Returning all Salesforce contacts associated with an account via query from third-party site

I'm using Survey Gizmo and I want to pull in all contact emails associated with an account.
This query works, but it only pulls in the first contact and I need them all:
SELECT Id,Contact.Email FROM Contact WHERE AccountId = 'Insert_Account_Id_Here'
How can I pull in more than one contact?
If that's not possible, is it possible to specifically pull in contact "N"? Then, question 1 could pull in contact 1, question 2 could pull in contact 2 and so forth.
Thanks... I'm new to this so let me know if that was unclear.
Select id,(select Email from contacts) from Acount where id =:'acountLd
for (contact con:acc.Contacts){ con.Email....}

How to limit SOQL results to those accessible to a user

I have admin API access for my organization. I'd like to run the same SOQL query, but get back results as visible to various users in my org: running "SELECT Name FROM Account" for user A, should only return account names accessible to user A.
I know this is easy if each user provides my application with their password and security token, so I can log in as them and run the query, but I want to do this only using my admin account.
this is very similar to:
Salesforce: impersonation using the API
but in this case I do have access to the data, I just want to filter it as though the request came from a specific user. It looks like there's an Apex "unit testing" method called System.RunAs() which looks close, but I want to run this via REST.
I think that you can filter the first SOQL query using HasReadAccess from UserRecordAccess table.
You could try building a set of RecordIDs first and then using this to filter the Account query.
Set<ID> sRecordIDs = [SELECT RecordID FROM UserRecordAccess WHERE UserId = :u AND HasReadAccess = True];
Account[] accs =[SELECT ID,Name FROM Account WHERE Id in :sRecordIDs];
More details on the official documentation

Compare two views in salesforce

I am looking for a way to compare two views in salesforce. I want to create a visual force page that lets a user select two views associated with the Account object and show all the accounts that appear on both views.
I am struggling pretty hard here, I can't figure out how to get the results from the views, but I am hoping there is a way to get all accounts that match the filters for each view.
Here is my SOQL query:
Select Id, Name, Owner.Name FROM Account WHERE
Id IN ( SELECT AccountId FROM Opportunity WHERE RecordTypeId = :RecordType1ID AND StageName IN :StageOneList )
AND Id IN ( SELECT AccountId FROM Opportunity WHERE RecordTypeId = :RecordType2ID AND StageName IN :StageTwoList )
This is the basis of the VF page I have made so far. It is possible to filter the Account with Account Owner and a drop down list from province. The idea is, many people in the organization have already created views with the accounts filtered as they need it. Instead of including every possible account field as a filter, I would like a drop down list of the active users views associated with Account, and then they can select Opportunity 1 and Opportunity 2 and have a list of Accounts matching.
I assume you mean views as in the available views in the dropdown box on a standard tab for an object? If so I don't believe you can query the results from them directly although you can query the Account object using a SOQL statement where you provide the filter.
My suggestion would be either create a set VF page that has 2 drop downs to switch the SOQL query that is used to return the list of accounts being displayed (would mean you have a set of predetermined views and updates to them require code updates) or give more details of your use case and we may be able to provide other suggestions.
It sounds like you just need to compare the results of the filters here. My suggestion would be that you're really trying to do something that should be done with reports, not with views.
Put two enhancedList components on the page.

Resources