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"
action="{!if(strRecTypeName==$Label.Custom_page_rectype,null,
URLFOR($Action.Account.Edit,
$ObjectType.Account,null,true))}">

This is Your custom visualforce Page

</apex: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();
        }
}

11 comments:

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

    ReplyDelete
  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.

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

    ReplyDelete
  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);
    returnURL.setRedirect(true);

    // Return the redirect page url
    return returnURL;
    }
    }

    ReplyDelete
  5. Sorry missed the Redirect Page -

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

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

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

      Delete
  6. Thanks very much for the solution

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

    Salesforce Training

    ReplyDelete
  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

    ReplyDelete
  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.

    ReplyDelete