Salesforce Certificate

Friday 12 July 2013

Conditionally Override "New" (Salesforce Standard button) as per Record Type Selection

Override "New" (Salesforce Standard button) as per RecordType Selection

Following is the snippet of anonymous code which will help you to override Salesforce standard "New" button as per RecordType selected by user.

Here is the example for Overriding standard "New" button of Account Object(Any sObject) as per RecordType Selection.

Step 1:
Override your Standard button of Account Object("New") with custom visualforce page to which you want to redirect page.

Step 2:
Create one Custom Label with Name = "Custom_page_rectype". Put here your custom recordtype value (hard coded) For which you want to override conditionally.

Step 3:
Put action attribute in your visualforce page's <apex:page> tag as below.

Visualforce Page:
<apex:page standardController="Account" extensions="extCtrlRecordtyperedirect"

This is Your custom visualforce Page


Apex Class: 
public class extCtrlRecordtyperedirect{ 
     Public string strRecTypeName {get; set; }
     public extCtrlRecordtyperedirect(ApexPages.StandardController controller) {
          strRecTypeid = apexpages.currentpage().getparameters().get('RecordType');
          if(strRecTypeid !=  null){
            Schema.DescribeSObjectResult d = Schema.SObjectType.Account;
            Map<Id,Schema.RecordTypeInfo> rtMapById = d.getRecordTypeInfosById();
            strRecTypeName = rtMapById.get(strRecTypeid).getName();


  1. Nice blog.Thanks for sharing useful information on Sales force.

  2. Hi, I am trying to override the new, edit, delete, ..buttons with the common visual force redirect page, is it possible to get the action type - New / Edit / Delete in the controller. I need to redirect to different pages based on the action (New/Edit/Delete/List), Record Type and Profile. Thanks for any help on this.

  3. Yes, It is Possible, you have to follow above steps for all Buttons.

  4. Thanks for your quick response. I have overridden the New and Edit actions with the same Visualforce redirect page and one controller as shown below.

    The challenge I am having is determining the requestType. Thanks again for your help.

    Redirect page:

    Redirect Controller:
    public with sharing class VFC_CustomerRedirectController {
    private Account customer;

    public VFC_CustomerRedirectController(ApexPages.StandardController controller) {
    this.customer = (Account) controller.getRecord();

    public PageReference handleRedirect() {
    PageReference returnURL;
    String requestType = 'New';

    // Get the profile name
    List PROFILE = [SELECT Id, Name FROM Profile WHERE Id=:userinfo.getProfileId() LIMIT 1];
    String profileName = PROFILE[0].Name.substring(0,5);

    // Get the selected record type
    String recordTypeId = ApexPages.currentPage().getParameters().get('RecordType');

    List RecordType = [SELECT Id, Name FROM RecordType WHERE Id=: recordTypeId LIMIT 1];
    String recordTypeName = RecordType[0].Name;

    // Get the target page based on the profile; This custom setting stores the pages for each record type/action combination
    // for eg. AccountRecType1CreatePage; AccountRecType1EditPage;...
    TargetPageMap__c targetPageMap = TargetPageMap__c.getInstance(userinfo.getProfileId());

    // Create the page reference
    if ((recordTypeName == 'RecType1) && requestType == 'New')) {
    targetPageName = targetPageMap.AccountRecType1CreatePage;
    } else if if ((recordTypeName == 'RecType1) && requestType == 'Edit')) {
    targetPageName = targetPageMap.AccountRecType1EditPage;
    } ...

    // Create the redirect page url
    returnURL = new PageReference('/apex/' + targetPageName);

    // Return the redirect page url
    return returnURL;

  5. Sorry missed the Redirect Page -

    apex:page standardController="Account"
    showheader="true" action="{!handleRedirect}"

    ** The issue is how does the controller know if the request is for create/edit/delete/list.. Thanks

    1. You have to make Different visualforce Pages for create/edit/delete/ and use same controller and different functions(Action Methods) in every page.

  6. Thanks very much for the solution

  7. Your information is really nice and very informative. Thanks for sharing this great article.

    Salesforce Training

  8. I have different scenario here.I Have one custom object and I need two different layouts while I am creating new record from Related list and directly from tab. For this I created two record types and two page layouts. For related list I created one custom button with url and assigned record type in url. while creating record from tab its asking record type selection and I don't want this, for skipping that I followed above structure but after that from related list also same page is opening, its overidden my custom button at related list level.

    Please help me on this

  9. Hi Hitesh,
    I'm trying to use this with the Task object but not having any luck. I'm trying to launch the visualforce page with a custom list button, can you recommend what the button URL should look like?

    I've tried a variety URLs such as /apex/MyPageName etc....

    Thanks for any help you can offer.

  10. today, I used to be just perusing along and happened upon your blog. simply needed to announce decent weblog and this text helped me loads, because of which i've found precisely I used to look. Nord VPN Cracked Pc

  11. I go to your blog much of the time and direction it to the total of people who wanted to highlight on happening their comprehension resulting to ease. The way of composing is stunning and in addition to the substance material is culmination score. gratitude for that discernment you give the perusers!

  12. Your substance material is nothing prompt of throbbing in bunches of ways. I guess this is alluring and eye-start texture. much obliged accurately a huge amount for stressing generally your substance material and your perusers. Good Morning Friday Quotes

  13. Thank you very much for being great, Fantastic post.

  14. Kindly Visit my website for cracked software, Windows & Mac. Download Now Latest Versions.
    Avast Cleanup Premium Crack