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
kevoharakevohara 

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
The only thing I can think of at this point is that it has something to do with the org being a Free Edition, or the fact that its in the sandbox.
Here is my controller, but I dont think its the problem.
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   
			   	
        }        

        
    }

}

 

Any ideas?  Thanks in advance.
Also, thanks to my Tweeps for helping me to try to troubleshoot this yesterday!

 

Best Answer chosen by Admin (Salesforce Developers) 
Kevin SwiggumKevin Swiggum

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

Kevin SwiggumKevin Swiggum

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

This was selected as the best answer
kevoharakevohara

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.