• Sheldox Man
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Here's my use case for a sites app:

1. Send an email out to Contacts via sf.com
2. Include a "manage my subscriptions" link in the email that has the contact id in it
3. End user clicks on that URL and lands on a sites page
4. End user is asked to enter the email address for which they want to modify subscriptions.
5. If the email they enter matches the email on the contact record, the can
6. change some settings on their contact record and hit save to commit them

I had this working in the authenticated view, but can't get sites to allow me to modify contact records as a public user. I can see why sf.com might not want to allow that. But I also don't want to make a person authenticate for this task.

So I changed things to allow the user to create a custom object that manages the subscription, and then I have a trigger on that object to update the contact record with the changes. So in effect, they are modifying the contact, but with a custom object and a trigger in between.

However, Sites won't let me do this unauthenticated. The checkboxes on my custom object don't show on the vf page. I don't get "authorization required" but the fields just aren't there.

Is Sites seeing my trigger and disallowing my public user because it knows I am going to update a Contact? Or am I screwing things up in a different way?

Here is the page:
Code:
<apex:page Controller="ext_contact_subscriptions" showHeader="False" title="Manage Your Subscriptions" >
<!-- Begin Default Content REMOVE THIS -->
<div style="margin:10px">
<h1>Manage your Subscriptions</h1>
<p>Check what you want to receive from us!</p>
<apex:form >
 <apex:outputPanel id="emailLogin" layout="block" style="margin-left:10px;"> 
  <apex:outputPanel rendered="{!AND(NOT(emailVerified),NOT(success))}" >
   <apex:outputText value="Your email:"/>
   <apex:inputText value="{!enteredEmail}" />
   <apex:commandButton value="submit" action="{!validateEmail}" rerender="emailLogin,subscriptions"/>
  </apex:outputPanel>
  <apex:outputPanel rendered="{!validationError}" layout="block">
  <br/>
   <apex:outputText style="color:#ff0000;" value="That email did not match this subscription. Try another email address."/>
  </apex:outputPanel>
 </apex:outputPanel>
 <apex:outputPanel id="subscriptions" layout="block" style="margin-left:10px;">
  <apex:outputPanel rendered="{!AND(emailVerified,NOT(success))}">
   <h2>Change your Email if you Wish</h2>
   <br/><br/>
   <apex:outputText value="Your Email: " style="margin-left:10px;"/> <apex:inputField value="{!subscription.Email__c}" /><br/><br/>
   <h2>Your Subscriptions</h2>
   <br/><br/>
   <apex:inputField value="{!subscription.Monthly_eNewsletter__c}" /> <apex:outputText value="eNewsletter"/>
   <br/><br/>
   
   <apex:outputText style="color:#666666;margin-left:10px;" value="Receive our monthly newsletter."/><br/><br/>
   <apex:inputField value="{!subscription.Action_Alerts__c}" /> <apex:outputText value="Action Alerts"/>
   <br/><br/>
   <apex:outputText style="color:#666666;margin-left:10px;" value="Receive our periodic notifications about actions."/><br/><br/>
   <apex:commandButton action="{!save}" value="Save" rerender="emailLogin,subscriptions,success"/>
  </apex:outputPanel>
 </apex:outputPanel>
 <apex:outputPanel id="success" layout="block" style="margin-left:10px;">
  <apex:outputPanel rendered="{!success}">
   <apex:outputText style="color:#666666;margin-left:10px;" value="Thanks for submitting your changes! They will go into effect immediately."/>
  </apex:outputPanel>
 </apex:outputPanel>
 <!--figure out redirect-->

</apex:form>
</div>
</apex:page>


Here is the controller:

Code:
public class ext_contact_subscriptions {
 
 public Contact contact = new Contact();
 
 public Boolean emailVerified   { get; set; }
 public String enteredEmail    { get; set; }
 public Boolean success     { get; set; }
 public Boolean validationError   { get; set; }
 public string msg       { get; set; }
 
 public Subscription__c subscription { get {return subscription;} set {subscription = value;} }
  
 public ext_contact_subscriptions() {
        contact = [select id, email,Monthly_eNewsletter__c, Action_Alerts__c from contact where id = :ApexPages.currentPage().getParameters().get('id')];
        emailVerified = false;
        validationError = false;
        success = false;
        subscription = new Subscription__c();
        subscription.Contact__c = contact.Id;
        subscription.Email__c = contact.Email;
        subscription.Monthly_eNewsletter__c = contact.Monthly_eNewsletter__c;
        subscription.Action_Alerts__c = contact.Action_Alerts__c;
    }
    
    public PageReference validateEmail() {
     //contact.email = 'steve@test.com';
     if (enteredEmail == contact.email) {
      emailVerified = true;
      validationError = false;
     } else {
      validationError = true; 
     }
     return null; 
    }
    
    
    public PageReference save() {
     
     try {
        // update contact;
       // subscription.contact__c = contact.Id;
     //   subscription.email__c = contact.email;
      //  subscription.Monthly_eNewsletter__c = contact.Monthly_eNewsletter__c;
      //  subscription.Action_Alerts__c = contact.Action_Alerts__c;
        insert subscription;
        
         success = true;
        } catch (exception e) { 
         msg = e.getMessage(); 
        }
     
     return null; 
    }
}

 and here is the trigger:

Code:
trigger subscription_to_contact on subscription__c (after insert) {
 Map<Id,subscription__c> subsToKill = new Map<Id,subscription__c>();
 List<Contact> contactsToProcess = new List<Contact>();
 for (subscription__c sub : Trigger.new) {
  Contact contact = new Contact (    
   id=sub.contact__c,
   Email = sub.email__c,
   Action_Alerts__c = sub.Action_Alerts__c,
   Monthly_eNewsletter__c = sub.Monthly_eNewsletter__c
  );
  contactsToProcess.add(contact);
  subsToKill.put(sub.id,sub);
 }
 if(contactsToProcess.size()>0){
  try {
   update contactsToProcess;  
   //delete subsToKill.values();
        } catch (exception e) { 
         system.debug(e.getMessage()); 
        }  
 } 
}

 Thanks much!

Steve