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
aam1raam1r 

Query returns results as anonymous but not via apex code

Hi Guys,

This may be something i kick myself with (and i hope it is) but i have a simple query in my apex code and i cannot understand why it has started failing for a specific site guest user.  Here's an extract form my class that contains the query:
// Retrieve the Order
System.debug('*** Ref: orderId = ' + orderId);
System.debug('*** Current user = ' + [select name from user where id = :userinfo.getuserid()].name);

Order ord = [Select Id, Name, Status From Order Where Id = :orderId];
I'm leaving the debug steps in here so we can see the debug logs:
USER_DEBUG|[106]|DEBUG|*** Ref: orderId = 8013N000000YSUoQAO
SOQL_EXECUTE_BEGIN|[107]|Aggregations:0|SELECT name FROM user WHERE id = :tmpVar1
SOQL_EXECUTE_END|[107]|Rows:1
USER_DEBUG|[107]|DEBUG|*** Current user = Site Guest User
SOQL_EXECUTE_BEGIN|[108]|Aggregations:0|SELECT Id, Name, Status FROM Order WHERE Id = :tmpVar1
SOQL_EXECUTE_END|[108]|Rows:0
SYSTEM_MODE_EXIT|false
FATAL_ERROR|System.QueryException: List has no rows for assignment to SObject
Now, looking in Salesforce, the record id for Order 8013N000000YSUoQAO does exist.  it has created by and owner as Site Guest User.  If i change the query on the order to be without the where clause for the order id and execute the same steps then i do get a number of Order IDs returned.  if i run teh query as anonymous, it works fine.  If i run the same steps for system admin it works fine. I have checked to make sure teh Site Guest User has CRUD access to Order  object and it does.

Either i've worked on this project too long and intensivley that i cannot see the obvious or something else is missing that i'm not catching.

Can someone please make a suggestion please?

Thanks
Aamir
 
aam1raam1r
Ok, i found the reason why this problem occurs - the account on the order record is not accessible by the Site user.  There seems to be an issue with Site Guest User permissions since the Spring 20.  Or there is something else arong with record accessibility.  Either way the above issue is not an issue with the code.