function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DorababuDorababu 

Redirect to detail page when using Person account

Hi friends,

In my sandbox we have enabled Person Accounts. The problem is when a record is saved it should go to different custom detail pages based on person account or business account. I have tried overriding the View button and this is redirecting even the business account to the overriden page(which is for personAccount detail page). If a record is being saved it shuld be redirected, to person account detail page if account is person acccount and, to business account detail page if account is BusinessAccount. 

 

For save in businessAccount creation page controller:

public PageReference saveRedirect(){

Account a = (Account) con.getRecord();
insert a;
return new pagereference('https://c.cs16.visual.force.com/apex/CustomerBusinessDetail'+a.id);
}

 

---------------------

for save in personAccount controller:

public pagereference save(){
Account a = (Account) con.getRecord();
a.recordtypeid='012f00000004Jfk';
a.LastName = a.Last_Name__pc;
a.FirstName = a.First_Name__pc;
insert a;
return new pagereference('/'+a.id);
}

 

 

Where should I differentiate the saving criteria?

sfdcfoxsfdcfox

You'll have to make a "dispatch" page, one that knows how to direct to different locations depending on the input.

 

It looks like this:

 

<!-- AccountViewRedirect -->
<apex:page standardController="Account" extensions="AccountViewRedirectExtension" action="{!redirect}">
  <apex:outputText rendered="false" value="{!Account.RecordType.DeveloperName}/></apex:page>

 

public with sharing class AccountViewRedirectExtension {
  ApexPages.StandardController controller;
  
  public AccountViewRedirectExtension(ApexPages.StandardController controller) {
    this.controller = controller;
  }

  public PageReference redirect() {
    Account record = (Account)controller.getRecord();
    PageReference ref = controller.view();
    Map<String,PageReference> refTypes = new Map<String,PageReference> {
      'A' => Page.AccountViewTypeA,
      'B' => Page.accountViewTypeB
    };
    if(refTypes.containsKey(record.RecordType.DeveloperName)) {
      ref = refTypes.get(record.RecordType.DeveloperName);
    }
    ref.setRedirect(true);
    ref.getParameters().put('id',record.Id);
    ref.getParameters().put('nooverride','1');
    return ref;
  }
}

How this design works:

 

Set AccountViewRedirect as the page override for the "view" standard button.

 

When user visits the record, they will be shown one of three pages. If record type is "A", they will see one page, if it is "B", they will see a second page, otherwise, they will see the standard salesforce.com detail page. Change the page names and record type names as appropriate.

 

This design allows you to extend your functionality by modifying the map values.