You need to sign in to do that
Don't have an account?
Simple Insert Not Working for Public Site
I have an issue that I cant seem to resolve. I have a Force.com Free Edition org an I am building some Pages in the Sandbox. I have a simple custom object that is acting sort of like a Hit counter. It's just got a couple of text fields and a timestamp field right now.
My controller is working fine. The page is rendered and pulls/displays data from another custom object just fine. The problem occurs when I try to insert a record to this Hit Counter object. I am routed to the "Required Login" screen. If I comment out the insert statement, the page is fine. I am well aware of the permissions that need to be available to the guest user under Sites -> Public Access Settings. Here is what I know so far...
- The object in question has read, create, and edit checked for the guest user
- The field level security is set to visible for everything and nothing is checked to be read only for the guest user (except for the system fields that you cant change)
- The guest user has access to all VF Pages
- The guest user has access to all Apex classes
- There are no triggers on the custom object
- There are no validation rules on the custom object.
- There are no relationship fields on the custom object
- Sharing rules on the object are public R/W
- The Controller is not using "with sharing" modifier
public class LandingPageController { private String pid; public Landing_Page__c page {get; private set;} public String errormsg {get; private set;} private PageReference pageRef = ApexPages.currentPage(); private Map<String, String> headers = pageRef.getHeaders(); private Map<String, String> getParams =PageRef.getParameters(); public LandingPageController() { pid = getParams.get('pid'); if(pid != null) { page = [SELECT Id, Name FROM Landing_Page__c WHERE LPN__c = :pid]; } if(page == null) { errormsg = 'No page returned!'; } else { Hit__c hit = new Hit__c(); hit.IP_Address__c = headers.get('True-Client-IP'); hit.LPN__c = getParams.get('pid'); hit.Timestamp__c = System.now(); insert hit; // <- THIS IS THE PROBLEM } } }
Hey Kevin,
Did you try taking the insert method out of the constructor and putting it in an action (PageReference) method?
then you can call the action method from the apex:page tag.
I'm pretty sure DML isn't allowed in a controller's constructor.
--Kevin
All Answers
Hey Kevin,
Did you try taking the insert method out of the constructor and putting it in an action (PageReference) method?
then you can call the action method from the apex:page tag.
I'm pretty sure DML isn't allowed in a controller's constructor.
--Kevin
You are correct Kevin. Guess I shouldn't code late at night. Problem solved.
The whole thing about the "Login Required" screen really threw me off. Quite misleading.