一编饺、需求闡述
創(chuàng)建一個(gè)向?qū)ы?yè)面玻募,分三步新建opportunity以及相應(yīng)的account和contact并保存到系統(tǒng)中蚓再。
二滑肉、代碼實(shí)現(xiàn)
Page 01
<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
<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
<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