+ Start a Discussion
Austin MayerhoferAustin Mayerhofer 

SOQL Query permissions

Hi all, I'm having some issues with what I think is an access issue.
I have code where the user uploads a pdf to a VisualForce page, and clicks a button which sends that pdf both to Salesforce files and the files in the specified group. This works perfectly when I enter the VisualForce Page in "Preview" from the Developer Console.
However, this doesn't work when I go to the actual URL of my Site. I am taken to a "Authorization Required" page after clicking the button and no files are stored. To be clear, by "actual URL" I mean when you go to Salesforce Sites and click the URL of your Site (which has the VisualForce page as its home page).
I've found that the main reason this is probably happening is an error. In preview, the SOQL query which is assigned to the pantryGroups variable has a size of 1, indicating it returned the correct group, but at the URL, the pantryGroups variable has a size of 0. Up to this point I did a few hours of research on public access settings to fix other Authorization Required pages but couldn't solve this issue. I was looking into "Profile" and see that I am a System Administrator so I went to give SysAdmins access to the VisualForce page and Apex class, but they already did.
What's going on here? When I go to the URL am I not actually a System Administrator at that point? Would I be considered a guest user? Any help is appreciated. I am trying to upload the file to both my Salesforce files and the groups files. Works perfectly fine in Preview, SOQL fails at the URL.
Here's the Apex code that runs when the button is clicked (pdfFile is a ContentVersion object):
// insert the file into the SF backend
pdfFile.PathOnClient = pdfFile.Title + '.pdf'; // The files name which will help in preview
pdfFile.ContentLocation = 'S'; // to use S specify this document is in Salesforce, to use E for external files
insert pdfFile;
// retrieve the uploaded file ID
Id pdfFileId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =: pdfFile.Id].ContentDocumentId;
// insert ContentDocumentLink to share file with
ContentDocumentLink pdfLink = new ContentDocumentLink();
pdfLink.ContentDocumentId = pdfFileId;
List<CollaborationGroup> pantryGroups=[select id, MemberCount from CollaborationGroup where Name=:groupName]; // grab the group from SF backend.
System.debug('groups size: ' + pantryGroups.size()); // prints 1 in preview, 0 at URL
CollaborationGroup pantryGroup = pantryGroups[0];
pdfLink.LinkedEntityId = pantryGroup.Id; // link the file to group
pdfLink.Visibility = 'AllUsers'; // all users in group can see
pdfLink.ShareType = 'C'; // all users in group can edit file
insert pdfLink;

Nayana KNayana K

You are dealing with files in case of guest user. By default, guest users can’t upload files and don’t have access to objects and their associated records.
To enable guest users to upload files, enable the org preference "Allow site guest users to upload files".

Please give a try