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
Valentina ErmilovaValentina Ermilova 

Can't get User by DML

I got user Id and trying to get user. This user is exist with this Id exactly. But I get 0 rows.
 
if (Schema.sObjectType.User.fields.Email.isAccessible() &&
            Schema.sObjectType.User.fields.FirstName.isAccessible()){

            User recipient = [SELECT Email, FirstName
                               FROM User
                               WHERE Id = :recipientId];
}
This script is a part of our REST API. So it run with permissions of Site Guest User.

Where the error can be?
 
Best Answer chosen by Valentina Ermilova
Valentina ErmilovaValentina Ermilova
Problem was resolved. I changed sharing settings for User here emea.salesforce.com/p/own/OrgSharingDetail. The default sharing for Guest User was private.

All Answers

Andrew EchevarriaAndrew Echevarria
If you do System.debug('*** ID ** : ' + recipientId); before the query, check the logs and is the User's Id display? If not, then the issue is not with the query but that the recipientId variable is not being populated properly in the previous logic
Valentina ErmilovaValentina Ermilova
Debug is problematic, because script worked perfect on developer instance, problem appears only on production instance where it installed as package. On production instance I can't see system.debug logs, but I see only requests to database. According requests, script get other object, that has contains necessary user Id. Id in report is the same that user has.
Andrew EchevarriaAndrew Echevarria
I think you need to look closely at the logic that created the recipientId because the code you posted appears to be functional.
Valentina ErmilovaValentina Ermilova
Problem was resolved. I changed sharing settings for User here emea.salesforce.com/p/own/OrgSharingDetail. The default sharing for Guest User was private.
This was selected as the best answer