0

I have an email field on a Contact and when I click a button it has to render a section that will tell you "You can't add the contact to a campaign because the email field is empty."

If the email field is not empty, it has to check if it is contained in a list and if it is, it has to show a different error message "The email is not accepted" and not let you add the contact to a campaign and it shouldn't show the error message for the 1st section

If the contact has opted out it should also stop you from adding the record to the campaign and show you an error message related to that.

This is my controller

public class ContactAddToCampaignController extends AddToCampaignControllerBase {

    private Id contactId;
    public boolean Missing;
    public boolean BadEmail;
    public boolean OptOut;
    public boolean Loaded=False;

    public ContactAddToCampaignController() 
    {
        contactId = (Id)ApexPages.currentPage().getParameters().get('id');
        BadEmail=False;
    }
    public boolean IsMissingEmail
   {
        get
        {
            return String.isBlank(SelectedContact.Email);
        }
        private set;
    }

    public Boolean IsBadEmail
    {
        get
        {   Set<String> emails = new Set<String>{'abuse@','database@','test@'};
            if (!String.isBlank(SelectedContact.Email)) 
               return emails.contains(SelectedContact.email.substringBefore('@') + '@');
            return false;
        }
        private set;
    }

   public boolean IsOptedOut
   {
        get
        {
            return (SelectedContact.HasOptedOutOfEmail);
        }
        private set;
    }


    private Contact SelectedContact 
    {
        get
        {
            if (SelectedContact == null)
            {
                SelectedContact = [SELECT Id, Email, HasOptedOutOfEmail FROM Contact WHERE Id = :contactId LIMIT 1];        
            }
            return SelectedContact;
        }
        private set;
    }


    public PageReference init()
    {
        if (IsMissingEmail){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'This Contact cannot be added to a Campaign because it does not have an email address.'));
        }  
        else
            if (IsBadEmail){
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'This Contact cannot be added to a Campaign because the email address is a role based email address.'));

            }
        else if (IsOptedOut) {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'This Contact cannot be added to a Campaign because it has Opted Out of emails.'));

        }
         return null;        
    }


    public PageReference addToCampaign() 
    {
        if (SelectedCampaign == '--None--')
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'A Campaign must be selected.'));
            return null;
        }

        List<CampaignMemberStatus> statusList = [SELECT Id, CampaignId, IsDefault, Label FROM CampaignMemberStatus WHERE CampaignId = :SelectedCampaign AND IsDefault = True LIMIT 1000];

        List<CampaignMember> existingMembers = [SELECT Id, ContactId, CampaignId FROM CampaignMember WHERE CampaignId = :SelectedCampaign AND ContactId = :SelectedContact.Id LIMIT 1];

        try
        {
            if (existingMembers.isEmpty())
            {
                insert new CampaignMember(ContactId = SelectedContact.Id, CampaignId = SelectedCampaign, Status = statusList[0].Label);
            }
        }
        catch (System.DmlException ex)
        {
            for (Integer i=0;i<ex.getNumDml();i++)
            {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, ex.getDmlMessage(i)));    
            }
            return null;                        
        }

        return redirectToContact();
    }      

    public PageReference cancel() 
    {
        return redirectToContact();
    }

    private PageReference redirectToContact()
    {
        PageReference retPage = new PageReference('/' + ApexPages.currentPage().getParameters().get('Id'));
        retPage.setRedirect(true);
        return retPage;
    }

And my vf page:

<apex:page controller="ContactAddToCampaignController" tabStyle="Campaign" action="{!init}">
    <apex:sectionHeader title="Add to Active Campaign"/>
    <apex:pageBlock mode="edit">
        <p>
            <apex:pageMessages />
        </p>               

        <!-- It doesn't have an email -->
        <apex:form >
         <!--   <apex:actionRegion> -->
            <apex:pageBlockSection rendered="{!(!IsMissingEmail)}" columns="1" title="Add to Campaign" collapsible="false">
                <apex:pageblocksectionitem >
                    <apex:outputLabel value="Active Campaigns" />
                    <apex:outputPanel layout="block" styleClass="requiredInput">
                         <apex:outputpanel layout="block" styleClass="requiredBlock"/>                 
                         <apex:selectList value="{!SelectedCampaign}" id="lstCampaigns" multiselect="false" size="1">
                             <apex:selectOptions value="{!ActiveCampaigns}"></apex:selectOptions>
                         </apex:selectList>
                    </apex:outputPanel>
                </apex:pageblocksectionitem>
                <apex:pageblocksectionitem >                    
                    <apex:outputPanel >
                        <div align="center">
                            <apex:commandButton value="Add to Campaign" action="{!addToCampaign}"></apex:commandButton>                        
                            <apex:commandButton value="Cancel" action="{!cancel}"></apex:commandButton>                        
                        </div>
                    </apex:outputPanel>
                </apex:pageblocksectionitem>
            </apex:pageBlockSection>
       <!--     </apex:actionRegion> -->

            <!--Has a role based Email -->
            <apex:actionRegion rendered="{!(!IsBadEmail)}">
             <apex:pageBlockSection rendered="{!(!IsBadEmail)}" columns="1" title="Rendered Add to Campaign" collapsible="false">
                <apex:pageblocksectionitem >
                    <apex:outputLabel value="Active Campaigns" />
                    <apex:outputPanel layout="block" styleClass="requiredInput">
                         <apex:outputpanel layout="block" styleClass="requiredBlock"/>                 
                         <apex:selectList value="{!SelectedCampaign}" id="lstCampaigns" multiselect="false" size="1">
                             <apex:selectOptions value="{!ActiveCampaigns}"></apex:selectOptions>
                         </apex:selectList>
                    </apex:outputPanel>
                </apex:pageblocksectionitem>
                <apex:pageblocksectionitem >                    
                    <apex:outputPanel >
                        <div align="center">
                            <apex:commandButton value="Add to Campaign" action="{!addToCampaign}"></apex:commandButton>                        
                            <apex:commandButton value="Cancel" action="{!cancel}"></apex:commandButton>                        
                        </div>
                    </apex:outputPanel>
                </apex:pageblocksectionitem>
            </apex:pageBlockSection> 
            </apex:actionRegion>

        </apex:form>
    </apex:pageBlock>
</apex:page>

It works except for that it always throws an error or it shows different sections. What is wrong?

  • {!(!IsBadEmail)} I'd strongly encourage you to use {!NOT(IsBadEmail)} from a readability standpoint – caleb.breckon Dec 20 '16 at 20:14
  • True, thanks! I just changed it. Do you know what the issue may be? – The Ghost Dec 20 '16 at 20:17
  • "It works except for that it always throws an error or it shows different sections." This doesn't make any sense and is lacking enough detail to help out. – caleb.breckon Dec 20 '16 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.