Baffled by a new record I've added that isn't available to query.

Person__c b = New Person__c(Name='Bob');
insert b;                                                                    //Returns a valid Id
Person_c newRecord = [SELECT Name FROM Person__c WHERE Id=:b.Id LIMIT 1];    //Returns NULL

I figured the insert operation might not have completed, so I tried:

Integer amount;
Database.SaveResult sr;

amount = [SELECT COUNT() FROM Person__c];   //Returns 3
sObject s = new Person__c (Name='Bob');
sr = Database.insertImmediate(s);
if (sr.isSuccess()) System.Debug('Saved!'); //Shows saved
amount = [SELECT COUNT() FROM Person__c];   //Returns 3 (expected 4)

Both of the above inserts work, but I can't seem to retrieve the new record within the same function. When I refresh the page, I can see there are now 4 records.

This works in a timely manner as a logged in user or on Dev console. Just not on guest. On guest, there is some sort of delay. The fact that the insert occurs (later) suggests I have the permissions set to Create + the field-level settings.

Am I missing a step such as "reindex" or "Database.refresh()"

1 Answer 1


Guest user can't own a record, so a guest user sharing rule would need to be configured to provide read access to the record

  • Thank you for the replay. Note: guest user successfully creates the record and can also read it. Only "later" by a few seconds. Seemingly after my code needed it Sep 14 at 13:08
  • It could be the sequence of events for the sharing record creation. I believe that criteria based sharing rules are calculated asynchronously, so that's where the gap may be coming from. I'll see if I can find a link to support this once I'm back at desk.
    – BenBates
    Sep 14 at 22:16
  • Here is a previous answer that seems to support this theory: salesforce.stackexchange.com/questions/375247/…
    – BenBates
    Sep 14 at 22:20
  • This is interesting. When I use "without sharing" there is no delay, which further seems to support the theory Sep 15 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.