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
BobBBobB 

Data entry from Sites VF Page

Maybe I'm trying to do the impossible: enter data into Salesforce from a sites page if not logged into Salesforce. Here's what I did:

 

Data Entry Object. Created it and made it public, completely. Yeah, I know, but it's a DE.

 

DataEntry VF Page. Controller from above. Single field entry form.

 

Sites link to bring it up: http://bobbaileyvforce-developer-edition.na9.force.com/EntryForm

 

The form will come up as you can see but Save doesn't work. Gets "Authorization Required" error, very spiffy page but not what I wanted to see.

 

Is this even possible? If not, why does web-to-lead and web-to-cases work?

 

tia... Bob

sfdcfoxsfdcfox

Bob,

 

Users of your site page ARE logged in-- as a guest user.

 

See my "email preferences page":

 

http://phyrfoxster-developer-edition.na3.force.com/emailPref

 

Here's the associated code:

 

<apex:page controller="emailPref" showHeader="true" sidebar="false">
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock rendered="{!notDone}">
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!save}" value="Submit"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">
                Use this form to be added or removed from our periodic emails.
                <apex:inputText label="Email" value="{!emailAddress}"/>
                <apex:selectList size="1" multiselect="false" label="Action" value="{!emailStatus}">
                    <apex:selectOption itemValue="Opt In"  itemLabel="Opt In"/>
                    <apex:selectOption itemValue="Opt Out" itemLabel="Opt Out"/>
                </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
public with sharing class emailPref {

    public emailPref() {
        notDone = true;
    }

    public Boolean notDone { get; set; }
    public String emailStatus { get; set; }
    public String emailAddress { get; set; }

    public void save() {
        try {
            Lead[] L = [SELECT Id FROM Lead WHERE Email = :emailAddress];
            for(lead tempLead:l) {
                tempLead.hasOptedOutOfEmail = emailStatus == 'Opt Out';
            }
            if(l.isEmpty()) {
                l.add(new Lead(lastname='unknown',email=emailAddress,hasoptedoutofemail=emailStatus=='Opt Out'));
            }
            if((new set<string>{'Opt In','Opt Out'}).contains(emailStatus)) {
                upsert l;
                apexpages.addmessage(new apexpages.message(apexpages.severity.info,'We have updated your preferences.'));
                notDone = false;
                return;
            }
        } catch(exception e) {
        }
            ApexPages.addMessage(new apexpages.message(apexpages.severity.error,'Some invalid input was received. Please try again.'));
    }
}

This page works just fine, as you can tell from my demo.

 

So, what's with the "authorization required" message?

 

This happens when your user receives an unhandled exception and they are not logged in. For example, were the try-catch block not in place, an invalid email address would cause the ugly authorization required page, instead of an informative message.

 

Try commenting out the try-catch block in this code in your org, and use an invalid email, like "test". You'll see the authorization required message. Using the try-catch block allows the error to surface correctly.

 

Try using the Debug Logs in setup to see if you can pinpoint the error that's happening.