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
SFDC GuestSFDC Guest 

Display visualforce based on record type selected

Hi All,

I am trying to display vf pages based on record type selected from standard salesforce page. I have two record types API Names are - RecordType1, RecordType2. Customer__c is object api name. After selecting record type and clicking on next button, i am getting below error.


Visualforce Error
Help for this Page
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!redirectToNewVFPage}' in component <apex:page> in page recordtypepage: Class.NewCustomerExtension.redirectToNewVFPage: line 13, column 1
Class.NewCustomerExtension.redirectToNewVFPage: line 13, column 1

Below is the code.

public with sharing class NewCustomerExtension {

    public String recordTypeId;

    public NewCustomerExtension (ApexPages.StandardController std) {

    }

    public Pagereference redirectToNewVFPage(){
        Pagereference pg = null;
        if(ApexPages.CurrentPage().getParameters().get('RecordType') != null){
            recordTypeId = ApexPages.CurrentPage().getParameters().get('RecordType');
            if(recordTypeId == Schema.SObjectType.Customer__c.getRecordTypeInfosByName().get('RecordType1').getRecordTypeId()){
               // pg = new Pagereference('/apex/Page1');
               pg = Page.Page1;   
            }else if(recordTypeId == Schema.SObjectType.Customer__c.getRecordTypeInfosByName().get('RecordType2').getRecordTypeId()){
               // pg = new Pagereference('/apex/Page2'); // Add Parameters to Page if Needed
               pg= Page.Page2;
            }
            pg.setRedirect(true);
            return pg;
        }
        return null;
    }
}

Page Name: RecordTypePage
<apex:page standardController="Customer__c" extensions="NewCustomerExtension" action="{!redirectToNewVFPage}">
</apex:page>

PageName: Page1:
<apex:page standardController="Customer__c" >
   Page 1
</apex:page>

PageName: page2
<apex:page standardController="Customer__c" >
  <h1>Congratulations</h1>
  This is your new Page
</apex:page>

I have overrided New button with page: RecordTypePage.

Plz let me know how to redirect to their respective pages. Thanks in advance.

 
Best Answer chosen by SFDC Guest
Davy HuijgensDavy Huijgens
The error is on this line:
 
if(recordTypeId == Schema.SObjectType.Customer__c.getRecordTypeInfosByName().get('RecordType1').getRecordTypeId())

and based on what you are stating 
Schema.SObjectType.Customer__c.getRecordTypeInfosByName().get('RecordType1')

 will return null and thus getRecordTypeId() will throw the null pointer exception.

getRecordTypeInfosByName needs the name of the recordtype as a parameter and not the api name.

All Answers

Davy HuijgensDavy Huijgens
The error is on this line:
 
if(recordTypeId == Schema.SObjectType.Customer__c.getRecordTypeInfosByName().get('RecordType1').getRecordTypeId())

and based on what you are stating 
Schema.SObjectType.Customer__c.getRecordTypeInfosByName().get('RecordType1')

 will return null and thus getRecordTypeId() will throw the null pointer exception.

getRecordTypeInfosByName needs the name of the recordtype as a parameter and not the api name.
This was selected as the best answer
Caroline WebbCaroline Webb
I am surly tell my IT students to help you for Display visualforce based on record type selected from standard salesforce page and how to redirect to their respective pages whom I am providing educational services at Personal Statement Folks - http://www.personalstatementfolks.co.uk/.
 
SFDC GuestSFDC Guest
Davy, Thanks.