Page Redirection on the basis of Record Type/Field (Classic)
I came across an requirement where I have redirect user to different pages on the basis of Record Type (redirection is possible on the basis of fieldvalue as well).
Lets take an example
Object : Account
Record Types:
- Customer [opens in Standard detail page]
- Partner [opens in Visualforce page]
when user try to access account record of type 'Customer' then Salesforce standard page should get open but accessing any account of type 'Partner', custom visualforce page should be open.
Controlling page-layout on the basis of record type is possible but we cannot open visualforce page on the basis of record type or field value. To achieve this functionality we have write some code and need to override standard view button.
To achieve this requirement we will create a visualforce page and apex class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AccountRedirectionController { | |
public string accId; | |
public string recName; | |
public AccountRedirectionController(ApexPages.StandardController stdcontroller) { | |
if(ApexPages.currentPage().getparameters().get('id')!=null) { | |
accId = ApexPages.currentPage().getparameters().get('id'); | |
recName = [SELECT Id, RecordtypeId, RecordType.name FROM Account where Id =: accId ].RecordType.Name; | |
} | |
} | |
public pageReference redirectToStandardpage() { | |
Schema.DescribeSObjectResult accResult = Account.SObjectType.getDescribe(); | |
Map<String,Schema.RecordTypeInfo> recTypeMap = accResult.getRecordTypeInfosByName(); | |
if(recTypeMap.containsKey(recName) && recName != 'Partner') { | |
pageReference pg = new pageReference('/'+ accId); | |
pg.getParameters().put('nooverride', '0'); | |
pg.setRedirect(true); | |
return pg; | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page standardController="Account" extensions="AccountRedirectionController" action="{!redirectToStandardpage}"> | |
<apex:pageBlock > | |
<apex:pageBlockSection title="Account Detail" columns="2"> | |
<apex:outputField value="{!account.Name}"/> | |
<apex:outputField value="{!account.Type}"/> | |
<apex:outputField value="{!account.Industry}" /> | |
<apex:outputField value="{!account.AnnualRevenue}" /> | |
<apex:outputField value="{!account.Rating}" /> | |
<apex:outputField value="{!account.Phone}" /> | |
<apex:outputField value="{!account.Fax}" /> | |
<apex:outputField value="{!account.Website}" /> | |
</apex:pageBlockSection> | |
<apex:pageBlockSection title="Account Address" columns="2"> | |
<apex:outputField value="{!account.BillingStreet}"/> | |
<apex:outputField value="{!account.BillingCity}"/> | |
<apex:outputField value="{!account.BillingState}"/> | |
<apex:outputField value="{!account.BillingCountry}"/> | |
<apex:outputField value="{!account.ShippingStreet}"/> | |
<apex:outputField value="{!account.ShippingCity}"/> | |
<apex:outputField value="{!account.ShippingState}"/> | |
<apex:outputField value="{!account.ShippingCountry}"/> | |
</apex:pageBlockSection> | |
<apex:pageBlockSection title="SLA Details" columns="2"> | |
<apex:outputField value="{!account.SLA__c}"/> | |
<apex:outputField value="{!account.SLAExpirationDate__c}"/> | |
<apex:outputField value="{!account.SLASerialNumber__c}"/> | |
</apex:pageBlockSection> | |
</apex:pageBlock> | |
</apex:page> |
Now when user view account of record type 'Customer' Salesforce standard page will open and for account of record type 'Partner' visualforce page will open.
same functionality can be achieved on the basis of field value.