0

I overrode the 'New' button of a custom object with a Visualforce page. According to this documentation doing this should only be possible on detail pages and not edit pages. However the 'Save & New' on the edit page is calling my Visualforce page (called WizardRedirector). What's more is it does not work when called from the edit page though it does from the detail page. It seems to be a 'Attempt to de-reference a null object' error originating from the Standard Controller I pass my extension. The Consumable Analysis Component object is always a child to a Consumable Analysis parent. When the 'New' button is accessed from the related list the code works just fine.

Here is the 'WizardRedirector' page:

<apex:page standardController="Consumable_Analysis_Component__c" 
  extensions="RedirectorController" action="{!redirect}">
    <apex:messages />
    <h1>Determining Record Type...</h1>
</apex:page>

And the extension code:

 Consumable_Analysis_Component__c component;
 Consumable_Analysis__c parent;
 Id parentId;
 public Map<Id,RecordTypeInfo> myMap = new Map<Id,RecordTypeInfo>();

  public RedirectorController(ApexPages.StandardController controller){
 try{
     System.debug(controller);
     this.component = (Consumable_Analysis_Component__c)controller.getRecord();
   System.debug(this.component);
   this.parentId = this.component.Solution_Analysis__c;
   this.parent = [SELECT Id, RecordTypeId FROM Consumable_Analysis__c WHERE Id=:parentId limit 1];
   this.myMap = Schema.SObjectType.Consumable_Analysis__c.getRecordTypeInfosById();
} catch(System.Exception e){
   ApexPages.addMessages(e);
} } 

 public PageReference redirect(){
    try{
       if(myMap.get(parent.RecordTypeId).getName()=='A'){
          PageReference pref = new PageReference('/apex/AConsumableAnalysisWizard');
           pref.getParameters().putAll(ApexPages.currentPage().getParameters());
     pref.setRedirect(true);
      return pref;

} else{ 
   PageReference pref = new PageReference('/apex/BConsumableAnalysisWizard');
   pref.getParameters().putAll(ApexPages.currentPage().getParameters());
   pref.setRedirect(true);
   return pref;

}}catch(System.Exception e){ApexPages.addMessages(e); return null;}

If someone could explain why the code should work when accessed from the related lists on the parent page not from the edit page, and how to remedy that issue, that would be appreciated. Optionally having the edit page with the Salesforce default button would be ok as well.

4
  • 2
    That's a bit of a wall of text. Can you provide a short version at the top (one to two sentences at a high level)?
    – Adrian Larson
    Feb 6, 2017 at 19:57
  • Sorry I'll shorten it
    – Josh D
    Feb 6, 2017 at 19:59
  • Do you have an error message and line number? Is your primary issue a NullPointerException?
    – Adrian Larson
    Feb 6, 2017 at 21:35
  • @AdrianLarson There is a ExecutionException: Attempt to de-reference a null object at the if-statement in the redirect() method. This is because as @RahulSharma pointed out my query is resulting in the error below. Ultimately this is because component being returned by the controller is null.
    – Josh D
    Feb 6, 2017 at 22:05

1 Answer 1

1

Whenever you get a value out of a Map, if you are not sure whether it is null or not, you should check.

Null Safe

RecordTypeInfo info = myMap.get(parent.RecordTypeId);
if (info != null && info.getName == 'A') {

You Have

if(myMap.get(parent.RecordTypeId).getName()=='A'){

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .