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
John Upton 8John Upton 8 

Guest User cannot access VF page when it updates a custom object

I've created a VF page with a custom controller, which displays content from (which I'll call Content) on publicly accessible pages on my community (i.e. anyone can access them, and will do so using the Guest User profile.
In the controller I have the code:
public void incCount() {  
    MyContent.Access_Count__c++;
    update MyContent;
    }
This all works fine, until I try to do add a code to count the number of times the page is viewed, which I do by adding the followign to my VF page after the controller declaration:
action="{!incCount}"
As soon as I include action="{!incCount}", My Guest Users are re-directed to the login page instead, i.e. they no longer have access. 
This is surely caused by me trying to Update my Conent table, but this being the case, setting the 'Custom Object Permissions' for the Guest User profile to have access to be able to Edit (and the rest, except Create) should suffice? It would seem not.

Can anyone offer any other reason why this is not working and/or how I can get it to work?

Thanks
Best Answer chosen by John Upton 8
Mahesh DMahesh D
Hi John,

==> Please comment the below line and test it once(Just to see if update is causing the issue or not.):

// update MyContent;

==> Make sure that Class is having without sharing.

==> Look into below URL:

https://developer.salesforce.com/docs/atlas.en-us.salesforce_communities_implementation.meta/salesforce_communities_implementation/networks_public_access.htm

Please do let me know if it helps you.

Regards,
Mahesh

All Answers

Shashikant SharmaShashikant Sharma
Hi John,

Make your class work without sharing mode and it will work. Your class should be decalred as below.

public without sharing NameOfYourClass

Thanks
Shashikant
Mahesh DMahesh D
Hi John,

==> Please comment the below line and test it once(Just to see if update is causing the issue or not.):

// update MyContent;

==> Make sure that Class is having without sharing.

==> Look into below URL:

https://developer.salesforce.com/docs/atlas.en-us.salesforce_communities_implementation.meta/salesforce_communities_implementation/networks_public_access.htm

Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer
John Upton 8John Upton 8
Thanks guys, the 'without sharing' did the trick, along with initialising the 'Access_Count__c' field to 0 which did not help!