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
Chiho SullivanChiho Sullivan 

Help With Controller Extension and Test for Save Record, Populate Picklist Field with Default Value, and Redirect to Thank You Page

First off, I am a volunteer with a non-profit.  They've asked me to help do something very simple but this is not my area of expertise.  They would like an online form to populate a picklist field with a default value (Online_Referral__c; picklist options Yes or No), insert a record, then redirect to a thank you page.  I understand how this all needs to work and how the objects and VF pages need to be created but I can't get the controller extension and test to work.  When I used the below, all tests passed in the sandbox but when I try to load it to production I get the following error:   Apex Class Invalid Type:  TrainingAgencyReferralSaveAndThankYou.  I just think that what I created has some very basic issues that hopefully someone with much ore experience than me can help me to correct.  I know our non-profit would be very appreciative!

Controller Extension
public class TrainingAgencyReferralSaveAndThankYou {
Referrals__c referrals;
public TrainingAgencyReferralSaveAndThankYou(ApexPages.StandardController controller){
this.controller = controller;   
}

public PageReference saveAndThankYou() {
Referrals.Online_Referral__c = 'Yes';
controller.save();
PageReference ThankYouPage = Page.TrainingReferralThankYou;
ThankYouPage.setRedirect(true);
return ThankYouPage;
}
private final ApexPages.StandardController controller;


Test
@isTest
public with sharing class KFGCreateTrainingReferralControllerTest {
    //==================== TEST METHOD(s) ======================================
    static testMethod void KFGCreateTrainingReferralControllerTest() {
    
    Referrals__c newReferrals = new Referrals__c();
   
        // Test insert Referrals__c
        newReferrals.Referral_Date__c = Date.Today();
        newReferrals.Referrer_First_Name__c = 'TestFirstName';
        newReferrals.Referrer_Last_Name__c = 'TestLastName';
        newReferrals.Referrer_Title__c = 'TestTitle';
        newReferrals.Agency__c = 'TestAgency';
        newReferrals.Email__c = 'testemail@test.com';
        newReferrals.Work_Phone__c = '619-111-2222';
        newReferrals.Mobile_Phone__c = '858-999-8888';
        newReferrals.Street_Address__c = 'TestAddress';
        newReferrals.City__c = 'San Diego';
        newReferrals.State__c = 'CA';
        newReferrals.Zip_Code__c = 92111;
        newReferrals.Referral_for_Class_Number__c = 8;
        newReferrals.Client_First_Name__c = 'ClientFirst';
        newReferrals.Client_Last_Name__c = 'ClientLast';
        newReferrals.Client_Phone__c = '800-555-7777';
        newReferrals.Client_Email__c = 'clientemail@test.com';
        newReferrals.Reason_for_Referral__c = 'TestReasonforReferral';
        newReferrals.Why_is_the_Client_Under_Your_Care__c = 'TestWhyUnderCare';
        newReferrals.How_Long_Have_You_Been_Working_Together__c = 'TestHowLongWorkingTogether';
        newReferrals.How_Often_Do_You_Communicate__c = 'TestHowOftenCommunicate';
        newReferrals.How_Often_Do_You_Meet__c = 'TestHowOftenMeet';
        newReferrals.What_Agencies_Do_You_Collaborate_With__c = 'TestAgencyCollaboration';
        newReferrals.What_Are_the_Clients_Goals__c = 'TestClient Goals';
        newReferrals.What_Are_The_Clients_Current_Challenges__c = 'TestClientChallenges';
        newReferrals.Challenges_You_Have_With_the_Client__c = 'TestChallengesWithClient';
        newReferrals.Will_You_Maintain_Collaborative_Support__c = 'Yes';
        newReferrals.Applicant_8_30am_4_30pm_Attendace_Issues__c = 'No';
        newReferrals.Specify_Time_Restrictions_Day_and_Time__c = 'TestTimeDayRestrictions';
        newReferrals.Signature__c = 'TestSignature';
        newReferrals.Date__c = Date.Today();
 insert newReferrals;
 ApexPages.StandardController controller = new ApexPages.StandardController(newReferrals);
    TrainingAgencyReferralSaveAndThankYou testnewReferral = new TrainingAgencyReferralSaveAndThankYou(controller);
    system.assertEquals(Page.TrainingReferralThankYou.getURL(), testnewReferral.saveAndThankYou().getURL());
 System.Assert(newReferrals.Id != null, 'The Referral did not insert properly, please check validation, workflows, etc.');
 
  Referrals__c newReferralsObj = [select Online_Referral__c from Referrals__c where id = :newReferrals.id ];
 
    System.AssertEquals(newReferralsObj.Online_Referral__c = 'Yes','The Online Referral field was not able to update.');
    }  
}

VF Page (not formatted version)
<apex:page sidebar="false" showHeader="false" standardController="Referrals__c" extensions="TrainingAgencyReferralSaveAndThankYou">  

    <div id="intro">
Please complete all fields before submitting the Agency Referral Form.  Also, if you have not done so already, please have your client complete the Project Launch Culinary Job Training Application.  Applicants can view program requirements and complete their online application at <a href="http://kitchensforgood.org/culinary-job-training/" style="color:blue">http://kitchensforgood.org/culinary-job-training/</a>.  Applicants can also submit a <a href="KFGTrainingApplication" style="color:blue">hard copy application</a> to <a href="training@kitchensforgood.org" style="color:blue">training@kitchensforgood.org</a> or at 404 Euclid Avenue, San Diego, CA 92114.
    </div>
    <apex:form >
    <div id="onlineform">
        <apex:inputHidden value="{!Referrals__c.Online_Referral__c}"/>
    </div>
    <div id="referreragencyinformation">
        First Name<apex:inputfield value="{!Referrals__c.Referrer_First_Name__c}"/>
        Last Name<apex:inputfield value="{!Referrals__c.Referrer_Last_Name__c}"/>
        Title<apex:inputfield value="{!Referrals__c.Referrer_Title__c}"/>
        Agency<apex:inputfield value="{!Referrals__c.Agency__c}"/>
        Email<apex:inputfield value="{!Referrals__c.Email__c}"/>
        Work Phone<apex:inputfield value="{!Referrals__c.Work_Phone__c}"/>
        Mobile Phone<apex:inputfield value="{!Referrals__c.Mobile_Phone__c}"/>       
        Street Address<apex:inputfield value="{!Referrals__c.Street_Address__c}"/>       
        City<apex:inputfield value="{!Referrals__c.City__c}"/>       
        State<apex:inputfield value="{!Referrals__c.State__c}"/>       
        Zip Code<apex:inputfield value="{!Referrals__c.Zip_Code__c}"/>       
    </div>       
   
    <div id="generalreferralandclientinformation">
        Referral Date<apex:inputfield value="{!Referrals__c.Referral_Date__c}"/>
        Referral For Class Number<apex:inputfield value="{!Referrals__c.Referral_for_Class_Number__c}"/>
        Client First Name<apex:inputfield value="{!Referrals__c.Client_First_Name__c}"/>
        Client Last Name<apex:inputfield value="{!Referrals__c.Client_Last_Name__c}"/>
        Client Phone<apex:inputfield value="{!Referrals__c.Client_Phone__c}"/>
        Client Email<apex:inputfield value="{!Referrals__c.Client_Email__c}"/>
    </div>       
    <div id="referrerandclientrelationshipinformation">
        Reason for Referral<apex:inputfield value="{!Referrals__c.Reason_for_Referral__c}"/>       
        Why is the Client Under Your Care<apex:inputfield value="{!Referrals__c.Why_is_the_Client_Under_Your_Care__c}"/>
        How Long Have You Been Working Together<apex:inputfield value="{!Referrals__c.How_Long_Have_You_Been_Working_Together__c}"/>
        How Often Do You Communicate<apex:inputfield value="{!Referrals__c.How_Often_Do_You_Communicate__c}"/>
        How Often Do You Meet<apex:inputfield value="{!Referrals__c.How_Often_Do_You_Meet__c}"/>
        What Other Agencies Do You and Your Client Collaborate With<apex:inputfield value="{!Referrals__c.What_Agencies_Do_You_Collaborate_With__c}"/>
        What Are the Client's Short and Long Term Goals<apex:inputfield value="{!Referrals__c.What_Are_the_Clients_Goals__c}"/>
        What Are the Client's Current Challenges<apex:inputfield value="{!Referrals__c.What_Are_The_Clients_Current_Challenges__c}"/>       
        What Challenges Do You Have With the Client<apex:inputfield value="{!Referrals__c.Challenges_You_Have_With_the_Client__c}"/>       
        Anything Else We Should Know<apex:inputfield value="{!Referrals__c.Anything_Else_We_Should_Know__c}"/>       
    </div>       
    <div id="continuedcollaborationclientattendancesignature">
        Kitchens for Good is a training program, and will not take the place of any social service agency the client is/will be utilizing. Are you willing to maintain collaborative support (maintain an open line of communication, attend meetings, etc.) with Kitchens for Good to promote the overall well-being of the client?<apex:inputfield value="{!Referrals__c.Will_You_Maintain_Collaborative_Support__c}"/>
        Are there any restrictions that would prohibit/interfere with the client’s ability to participate Monday through Friday from 8:30am-4:30pm?<apex:inputfield value="{!Referrals__c.Applicant_8_30am_4_30pm_Attendace_Issues__c}"/>
        Specify Attendance Time Restrictions (Day and Time)<apex:inputfield value="{!Referrals__c.Specify_Time_Restrictions_Day_and_Time__c}"/>
        Signature<apex:inputfield value="{!Referrals__c.Signature__c}"/>
        Date<apex:inputfield value="{!Referrals__c.Date__c}"/>
    </div>       
       
    <apex:commandButton value="Save" action="{!saveAndThankYou}"/>
    </apex:form>   
</apex:page>