用Visualforce Page创建向导页面

2019-10-29  本文已影响0人  江江酱酱233
一、需求阐述

创建一个向导页面,分三步新建opportunity以及相应的account和contact并保存到系统中。

二、代码实现
Page 01
page01
<apex:page Controller="newOpportunityController " tabStyle="Opportunity">
    <script>
        function confirmCancel(){
            var isCancel = confirm("Are you sure to cancel?");
            if (isCancel) {return true;}
            return false;
        }
    </script>
    <apex:sectionHeader title="New Customer Opportunity" subtitle="Step 1 of 3"/>
    <apex:form>
        <apex:pageBlock title="Customer Information" mode = "edit">
            <apex:pageBlockSection title="Account Information">
                <apex:inputField id = "accountName" value="{!account.name}"/>
                <apex:inputField id = "accountSite" value="{!account.site}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Contact Information">
                <apex:inputField id = "contactFirstName" value="{!contact.firstname}"/>
                <apex:inputField id = "contactLastName" value="{!contact.lastname}"/>
                <apex:inputField id = "contactPhone" value="{!contact.phone}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons>
                <apex:commandButton value="Next" action="{!step2}"/>
                <apex:commandButton value="Cancel" action="{!cancel}" onclick="return confirmCancel()" immediate="true"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Page 02
page02
<apex:page controller="newOpportunityController" tabStyle="Opportunity">
    <script>
        function confirmCancel(){
            var isCancel = confirm("Are you sure to cancel?");
            if (isCancel) {return true;}
            return false;
        }
    </script>
    <apex:sectionHeader title="New Customer Opportunity" subtitle="Step 2 of 3"/>
    <apex:form>
        <apex:pageBlock title="Opportunity Information" mode = "edit">
            <apex:pageBlockSection title="Opportunity Information">
                <apex:inputField id="opportunityName" value="{!opportunity.Name}"/>
                <apex:inputField id="opportunityAmount" value="{!opportunity.Amount}"/>
                <apex:inputField id="opportunityClosedate" value="{!opportunity.closeDate}"/>
                <apex:inputField id="opportunityStagename" value="{!opportunity.StageName}"/>
                <apex:inputField id="contactRole" value="{!role.Role}"/>
            </apex:pageBlockSection>
            <apex:commandButton value="Previous" action="{!step1}"/>
            <apex:commandButton value="Next" action="{!step3}"/>
            <apex:commandButton value="Cancel" action="{!cancel}" onclick="return confirmCancel()" immediate = "true"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Page 03
page03
<apex:page controller="newOpportunityController" tabStyle="Opportunity">
    <script>
        function confirmCancel(){
            var isCancel = confirm("Are you sure to cancel?");
            if (isCancel) {return true;}
            return false;
        }
    </script>
    <apex:sectionHeader title="New Customer Opportunity" subtitle="Step 3 of 3"/>
    <apex:form>
        <apex:pageBlock title="Confirmation" mode = "edit">
            <apex:pageBlockSection title="Account Information">
                <apex:outputField value="{!account.Name}"/>
                <apex:outputField value="{!account.Site}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Contact Information">
                <apex:outputField value="{!contact.FirstName}"/>
                <apex:outputField value="{!contact.LastName}"/>
                <apex:outputField value="{!contact.Phone}"/>
                <apex:outputField value="{!role.Role}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Opportunity Information">
                <apex:outputField value="{!opportunity.Name}"/>
                <apex:outputField value="{!opportunity.Amount}"/>
                <apex:outputField value="{!opportunity.CloseDate}"/>
            </apex:pageBlockSection>
            <apex:commandButton value="Previous" action="{!step2}"/>
            <apex:commandButton value="Save" action="{!save}"/>
            <apex:commandButton value="Cancel" action="{!cancel}" onclick="return confirmCancel()" immediate = "true"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Controller
public with sharing class newOpportunityController {
    Account account;
    Contact contact;
    Opportunity opportunity;
    OpportunityContactRole role;

    public Account getAccount(){
        if (account == null) account = new Account();
        return account;
    }

    public Contact getContact(){
        if (contact == null) contact = new Contact();
        return contact;
    }

    public Opportunity getOpportunity(){
        if (opportunity == null) opportunity = new Opportunity();
        return opportunity;
    }

    public OpportunityContactRole  getRole(){
        if (role == null) {
            role = new OpportunityContactRole();
        }
        return role;
    }

    public PageReference step1(){
        return Page.opptyStep1;
    }

    public PageReference step2(){
        return Page.opptyStep2;
    }

    public PageReference step3(){
        return Page.opptyStep3;
    }

    public PageReference cancel(){
        PageReference opportunityPage = new ApexPages.StandardController(opportunity).view();
        opportunityPage.setRedirect(true);
        return opportunityPage;
    }   

    public PageReference save(){
        account.Phone = contact.Phone;
        insert account;

        contact.accountId = account.Id;
        insert contact;

        opportunity.accountId = account.Id;
        insert opportunity;

        role.opportunityId = opportunity.Id;
        role.contactId = contact.Id;
        insert role;

        PageReference opptPage = new ApexPages.StandardController(opportunity).view();
        opptPage.setRedirect(true);
        return opptPage;
    }
}

参考:https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_wizard.htm

上一篇 下一篇

猜你喜欢

热点阅读