+91-9953024688
           

How to create Salesforce Visual Force wizard

   

How to create the salesforce wizard from VF pages and Apex:

  1.  Create Apex Controller :

/*

 * @Author:Manjeet Singh
 * @Purpose: Create Visualforce Wizard for Account,Contact and Opportunity
 * @Description: Create individiual VF page for Account,Contact and opportunity and handled all the VF pages from one controller
 * @Date:  2/12/2018
 * @Modified By: Sourabh and Naresh
 * @Modified Date: 2/12/2018
 * */
public class VFWizardCtrl{
    public Account objAccount{get;set;}
    public Contact objContact{get;set;}
    public Opportunity objOpportunity{get;set;}
    //Initializing class level variables
    public VFWizardCtrl()
    {
        objOpportunity=new Opportunity();
        objAccount=new Account();
        objContact=new Contact();
    }
    //Launching Wizard from button
    public PageReference createWizard()
    {
        return Page.accountWizard;
    }
    //Account page code
    public Account getobjAcc(){
        if(objAccount == null)
            objAccount=new Account();
        return objAccount;
    }
    //Restarting the Wizard
    public PageReference restartWizard()
    {
        return Page.launchWizard;
    }
    //button to jump to the Contact
    public PageReference goToAccount()
    {
        PageReference pageref = new PageReference(‘/apex/contactWizard’);
        return pageref;
    }
    //Contact page coding
    public Contact getobjCon(){
        if(objContact == null)
            objContact=new Contact();
        return objContact;
    }
    //button to move back to Account
    public PageReference backToAccount()
    {
        return Page.accountWizard;
    }
    //button to jump to the opportunity
    public PageReference goToOpportunity()
    {
        return Page.opportunityWizard;
    }
    //opportunity coding
    public Opportunity getobjopp(){
        if(objOpportunity == null)
            objOpportunity=new Opportunity();
        return objOpportunity;
    }
    //button to move back to the Contact page
    public PageReference backToContact()
    {
        return Page.contactWizard;
    }
    //buton for preview of all te information entered by the user
    public PageReference recordPreview()
    {
        return Page.wizardFinalPage;
    }
    //final page coding
    public PageReference Previous()
    {
        return Page.opportunityWizard;
    }
    //button to cancel the wizard and jump to the launch wizard page
    public PageReference Cancel()
    {
        return Page.launchWizard;
    }
    //button to save the information entered by the user
    public PageReference Save()
    {
        if(objAccount!=null){
            try{
                insert objAccount;
            }catch(Exception ee){
                ee.getStackTraceString();
            }
        }else{
            System.debug(‘:: Failed Record null :: ‘+objAccount);
        }
        //created object to save to the record
        Contact objContact2= new contact();
        Opportunity objOpportunity2 = new Opportunity();
        //copying the information to saving the record
        objContact2.LastName= objContact.LastName;
        objContact2.AccountId=objAccount.Id;
        objContact2.title=objContact.title;
        objContact2.Phone=objContact.Phone;
        objContact2.AssistantName=objContact.AssistantName;
        objContact2.AssistantPhone=objContact.AssistantPhone;
        objContact2.Department=objContact.Department;
        objContact2.Description=objContact.Description;
        objOpportunity2.AccountId=objAccount.Id;
        objOpportunity2.Name=objOpportunity.Name;
        objOpportunity2.CloseDate=objOpportunity.CloseDate;
        objOpportunity2.Amount=objOpportunity.Amount;
        objOpportunity2.Contract=objOpportunity.Contract;
        objOpportunity2.StageName=objOpportunity.StageName;
        objOpportunity2.Type=objOpportunity.Type;
        objOpportunity2.LeadSource=objOpportunity.LeadSource;
        objOpportunity2.Description=objOpportunity.Description;
        //inserting the info in record to save
        insert objContact2;
        insert objOpportunity2;
        PageReference pageref = new PageReference(‘/’+objAccount.id);
        return pageref;
    }
}
2. Create VF pages :
*********first page*************

<apex:page controller=”VFWizardCtrl” wizard=”true”>
    <apex:form >
        <apex:commandButton value=”Create Wizard” action=”{!createWizard}”/>
    </apex:form>
</apex:page>
***********second page**********
<apex:page Controller=”VFWizardCtrl” wizard=”true”>
    <apex:form >
     <apex:pageBlock title=”Account”>
            <apex:pageBlockSection columns=”1″>
                <apex:inputField value=”{!objAccount.Name}”/>
                <apex:inputField value=”{!objAccount.Site}”/>
                <apex:inputField value=”{!objAccount.Type}”/>
                <apex:inputField value=”{!objAccount.Fax}”/>
                <apex:inputField value=”{!objAccount.Rating}”/>
                <apex:inputField value=”{!objAccount.Phone}”/>
                <apex:inputField value=”{!objAccount.Description}”/>
                </apex:pageBlockSection>
            <apex:commandButton value=”restartWizard” action=”{!restartWizard}”/>
            <apex:commandButton value=”Next” action=”{!goToAccount}”/>
        </apex:pageBlock>
    </apex:form>
</apex:page>
**********3rd page*****************
<apex:page controller=”VFWizardCtrl” wizard=”true”>
    <apex:form >
     <apex:pageBlock title=”Contact”>
            <apex:pageBlockSection columns=”1″>
                <apex:inputField value=”{!objContact.Name}”/>
                <apex:inputField value=”{!objContact.LastName}”/>
                <apex:inputField value=”{!objContact.Title}”/>
                <apex:inputField value=”{!objContact.AssistantName}”/>
                <apex:inputField value=”{!objContact.AssistantPhone}”/>
                <apex:inputField value=”{!objContact.Department}”/>
                <apex:inputField value=”{!objContact.Phone}”/>
                <apex:inputField value=”{!objContact.Description}”/>
                </apex:pageBlockSection>
            <apex:commandButton value=”Back” action=”{!backToAccount}”/>
            <apex:commandButton value=”Next” action=”{!goToOpportunity}”/>
        </apex:pageBlock>
    </apex:form>
</apex:page>
*********fourth page*******************
<apex:page Controller=”VFWizardCtrl” wizard=”true”>
    <apex:form >
     <apex:pageBlock title=”Opportunity”>
            <apex:pageBlockSection columns=”1″>
                <apex:inputField value=”{!objOpportunity.Name}”/>
                <apex:inputField value=”{!objOpportunity.Amount}”/>
                <apex:inputField value=”{!objOpportunity.CloseDate}”/>
                <apex:inputField value=”{!objOpportunity.Contract}”/>
                <apex:inputField value=”{!objOpportunity.StageName}”/>
                <apex:inputField value=”{!objOpportunity.Type}”/>
                <apex:inputField value=”{!objOpportunity.LeadSource}”/>
                <apex:inputField value=”{!objOpportunity.Description}”/>
                </apex:pageBlockSection>
            <apex:commandButton value=”Back” action=”{!backToContact}”/>
            <apex:commandButton value=”Preview” action=”{!recordPreview}”/>
        </apex:pageBlock>
    </apex:form>
</apex:page>
********fifth page***********
<apex:page Controller=”VFWizardCtrl” wizard=”true”>
    <apex:form >
     <apex:pageBlock title=”Account”>
            <apex:pageBlockSection columns=”1″>
                <apex:outputField value=”{!objAccount.Name}”/>
                <apex:outputField value=”{!objAccount.Site}”/>
                <apex:outputField value=”{!objAccount.Type}”/>
                <apex:outputField value=”{!objAccount.Fax}”/>
                <apex:outputField value=”{!objAccount.Rating}”/>
                <apex:outputField value=”{!objAccount.Phone}”/>
                <apex:outputField value=”{!objAccount.Description}”/>
                </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock title=”Contact”>
            <apex:pageBlockSection columns=”1″>
                <apex:outputField value=”{!objContact.Name}”/>
                <apex:outputField value=”{!objContact.LastName}”/>
                <apex:outputField value=”{!objContact.Title}”/>
                <apex:outputField value=”{!objContact.AssistantName}”/>
                <apex:outputField value=”{!objContact.AssistantPhone}”/>
                <apex:outputField value=”{!objContact.Department}”/>
                <apex:outputField value=”{!objContact.Phone}”/>
                <apex:outputField value=”{!objContact.Description}”/>
                </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock title=”Opportunity”>
            <apex:pageBlockSection columns=”1″>
                <apex:outputField value=”{!objOpportunity.Name}”/>
                <apex:outputField value=”{!objOpportunity.Amount}”/>
                <apex:outputField value=”{!objOpportunity.CloseDate}”/>
                <apex:outputField value=”{!objOpportunity.Contract}”/>
                <apex:outputField value=”{!objOpportunity.StageName}”/>
                <apex:outputField value=”{!objOpportunity.Type}”/>
                <apex:outputField value=”{!objOpportunity.LeadSource}”/>
                <apex:outputField value=”{!objOpportunity.Description}”/>
                </apex:pageBlockSection>
        </apex:pageBlock>
    <apex:commandButton value=”Previous” action=”{!Previous}”/>
    <apex:commandButton value=”Cancel” action=”{!Cancel}”/>
    <apex:commandButton value=”Save” action=”{!Save}”/>
    </apex:form>
</apex:page>
3. Test Class:
/*
* @Author:Manjeet Singh
* @Purpose: Create Visualforce Wizard for Account,Contact and Opportunity
* @Description: Create individiual VF page for Account,Contact and opportunity and handled all the VF pages from one controller
* @Date:  2/12/2018
* @Modified By: Sourabh and Naresh
* @Modified Date: 2/12/2018
* */
@isTest
public class VFWizardCtrlTest {
    public static List<Account> testacc;
    public static List<Contact> testcon;
    public static List<Opportunity> testopp1;
    @testSetup
    public static void VFWizardCtrl()
    {
        testacc = new List<Account>();
        testcon = new List<Contact>();
        testopp1 = new List<Opportunity>();
        for(INTEGER i=0;i<20;i++)
        {
            testacc.add(New Account(Name =’Acc’+i));
        }
        insert testacc;
        for(INTEGER i=0;i<20;i++)
        {
            testcon.add(New Contact(LastName =’Con’+i));
        }
        insert testcon;
        for(INTEGER i=0;i<20;i++)
        {
            testopp1.add(New Opportunity(Name =’Opp’+i,StageName=’Prospecting’,CloseDate=System.today()));
        }
        insert testopp1;
    }
    @isTest
    public static void testcontroller1()
    {
        VFWizardCtrl();
        VFWizardCtrl objVFWizardCtrlTest = new VFWizardCtrl();
        Test.startTest();
        objVFWizardCtrlTest.objAccount = testacc[0];
        objVFWizardCtrlTest.objContact = testcon[0];
        objVFWizardCtrlTest.objOpportunity = testopp1[0];
        objVFWizardCtrlTest.createWizard();
        objVFWizardCtrlTest.getobjAcc();
        objVFWizardCtrlTest.restartWizard();
        objVFWizardCtrlTest.goToAccount();
        objVFWizardCtrlTest.getobjCon();
        objVFWizardCtrlTest.backToAccount();
        objVFWizardCtrlTest.goToOpportunity();
        objVFWizardCtrlTest.getobjOpp();
        objVFWizardCtrlTest.backToContact();
        objVFWizardCtrlTest.recordPreview();
        objVFWizardCtrlTest.Previous();
        objVFWizardCtrlTest.Cancel();
        objVFWizardCtrlTest.Save();
        Test.StopTest();
    }
        @isTest
    public static void testcontroller2()
    {
        VFWizardCtrl objVFWizardCtrlTest2 = new VFWizardCtrl();
        objVFWizardCtrlTest2.objContact=NULL;
        objVFWizardCtrlTest2.objAccount=NULL;
        objVFWizardCtrlTest2.objOpportunity=NULL;
        objVFWizardCtrlTest2.getobjAcc();
        objVFWizardCtrlTest2.getobjCon();
        objVFWizardCtrlTest2.getobjOpp();
        Test.StopTest();
    }
}

Comments (1)

Leave a Reply

Your email address will not be published. Required fields are marked *

Nube Lead Infoventure Pvt Ltd