Replace standard record type selection page with a Visualforce Page
In Salesforce.com, record types let you offer different business processes, picklist values, and page layouts to different users. You may find yourself in a situation where you need to use a custom visualforce page to replace the standard record type selection page to make dealing with Salesforce just a tad bit easier. The following article explains how.
Use Case: Let’s say you have multiple record types on an Object (in our example Opportunity) and you go through the custom record type selection page while creating a new record. This article describes how you would replace the standard record type selection page with a Visualforce page and navigate to the standard create page with the fields you want to carry over using URL hack. This allows you to pre-populate fields on the sObject (in our example Opportunity Name).
Step 1: Create a custom Visualforce page (in our example OpportunityVP) and a custom apex controller for it (in our example OpportunityExtension).
The code for the Visualforce page is as follows:
<apex:page standardController="Opportunity" extensions="OpportunityExtension">
<apex:sectionheader title = "New Opportunity" subtitle = "Select Opportunity Record Type" />
<apex:form>
<apex:pageBlock title = "Select Opportunity Record Type" mode = "edit">
<apex:pageBlockButtons>
<apex:commandButton value = "Continue" action = "{!conti}" />
<apex:commandButton value = "Cancel" action = "{!cancel}" />
</apex:pageBlockButtons>
<apex:pageBlockSection columns="1">
<apex:outputLabel ><strong>Select Record Type:</strong ></apex:outputLabel>
<apex:selectList value="{!selectedRecordType}" size="1">
<apex:selectOptions value="{!options}"></apex:selectOptions>
</apex:selectList>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
The Custom Controller code is as follows:
public class OpportunityExtension {
private final Opportunity u;
public String selectedRecordType{get;set;}
public List<SelectOption> options {get;set;}
public OpportunityExtension(ApexPages.StandardController stdController) {
options = new List<selectOption>();
List<RecordType> rec = [Select ID, Name From RecordType where sObjectType = 'Opportunity'];
for(RecordType opps : rec) {
//for all records found - add them to the picklist options
options.add(new selectOption(opps.Id, opps.Name));
}
this.u = (Opportunity)stdController.getRecord();
}
public PageReference conti() {
List<RecordType> recName = [select Name from RecordType where Id =: selectedRecordType];
PageReference pr = new PageReference('/006/e?retURL=' + ApexPages.currentPage().getParameters().get('retURL') + '&RecordType=' + selectedRecordType + '&ent=Opportunity&opp3=' + recName[0].Name + '&accid=' + ApexPages.currentPage().getParameters().get('accid') + '&nooverride=1');
pr.setRedirect(true);
return pr;
}
}
We cannot access the record types as a picklist directly in a Visualforce page using the standard controller. So, we are creating a custom picklist in the extension where the RecordTypeName, RecordTypeId are the names, values in the picklist (the code for it is in the constructor).
We will discuss what the code in the conti function does in the next step.
Step 2: In this step we are creating a new list button where we execute some javascript. In our example, we are using the standard Opportunity object, so we are creating the list button using the following steps:
a. Setup -> Customize -> Opportunities -> Buttons, Links, and Actions.
b. Next, click on New Button or Link.
c. Give it a name (in our example New Opportunity), in display type choose list button (you can keep display checkboxes checked.)
d. Behavior: Execute Javascript.
e. Content Source: OnClick Javascript.
Paste the following code in the content box:
window.location="apex/OpportunityVP?retURL=%2F{!Account.Id}&accid={{!Account.Id}";
The above code will redirect to our Visualforce page when we click on our newly created button (called New Opportunity).
In our example, we have designed the New Opportunity button to be on the Account page as a related list button, so, we need to replace the Standard List button with the above button on the account page where opportunity is the related list.
In the conti function, we are creating a page reference object where we are using the parameters from the above button to perform our URL hack.
In our example, we are giving the retURL as the AccountID field so that when we save the opportunity it goes back to the Account record from where it was created.
The accid field is used for pre-populating the Account field in the opportunity create Page. We are also using opp3 and giving it the name of the selected record type. In our example, we wanted to prepopulate the opportunity name field with the record type name that was picked in the record type selection page.
The result of this is shown in the screenshot below:
The nooverride=1 forces us to go to the standard create page. In our example, we are restricting our users from creating opportunities through the opportunity tab. We have overridden the behavior of the standard new button of opportunity to redirect to the page below.
If you don’t use nooverride, you won’t be redirected to the standard create page after selecting the record type. (You can skip using nooverride if you are not performing such a restriction).
Finally, to deploy our code, we need to write a test class, which is given below:
@isTest
public class OpportunityExtensionTest {
public static testMethod void testControllerMethod() {
Account acc = new Account(Name='test', AccountNumber = 'C00000');
insert acc;
List<RecordType> rec = [Select ID, Name From RecordType Where sObjectType = 'Opportunity'];
Opportunity o = new Opportunity();
PageReference pageRef = Page.Opportunity_Information_Page;
Test.setCurrentPage(pageRef);
ApexPages.StandardController sc = new ApexPages.standardController(o);
OpportunityExtension ext = new OpportunityExtension(sc);
ext.selectedRecordType = Id.valueOf(rec[0].ID);
System.currentPageReference().getParameters().put('retURL','%2F' + acc.Id);
System.currentPageReference().getParameters().put('accid', acc.Id);
String nextPage = ext.conti().getUrl();
}
}
}
Patient Care Manager with expertise in children's health.
6 年Great way to enforce a naming convention tied to opportunity record type! Would love to hear feedback from David K. Liu