Sample Code With Sharing: Give Guest Users Access to Read Records
In this collection of code samples, the guest user enters a date range and then views events within that range. The guest user has read access to the records via sharing rules.
Before you give read access to guest users, see Give Guest Users Access to Read Records.
This sample Aura component displays two lightning:input components, where the user enters a start and end date to view events. The lightning:card component displays each event’s StartDateTime, EndDateTime, Subject, and Location.
This sample JavaScript controller processes events for the Aura component and calls the methods in the helper file.
This JavaScript helper creates an asynchronous request to find events within the two timestamps that the user submitted and defines the actions to take when the request completes.
This sample Apex controller receives the call to find records from the JavaScript helper. It selects events that match these criteria:
- The event’s
StartDateTimeis greater than theStart_Dateparameter. - The event’s
EndDateTimeis less than theEnd_Dateparameter. - The event’s
isPrivatevalue isFalse. - The event’s
isArchivedvalue isFalse.
The query returns these fields for each of the events:
StartDateTimeEndDateTimeLocationSubjectId
Because the guest users don’t need the record ID, a for loop copies all the other fields to a new Event object. Then, we add the new objects to a new list and return that list to the client.
The guest user has access to the records with sharing rules, so we define the class with the with sharing keyword.
Any system or individual on the internet can invoke @AuraEnabled methods. Protect the execution of the method by implementing procedural access checks. Make sure that the query selects only the desired records and only the required fields.
See Also