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
Joshua Danis 7Joshua Danis 7 

Edit Page and Related List New Button are Both Overridden - Different Behaviors

I have created two custom objects we'll call the analysis and component. Components are always children to an analysis; they do not exist alone. Both analyses and components have two record types, we'll call them A and B. An A type analysis cannont have B type components and vice versa. Type A and Type B components are considerably different and as such I've created two different Visualforce pages to create components skipping the need for record type selection and streamlining things etc. I've created a third Visualforce page which is essentially a dummy page that calls an action from its extension. This action redirects to one of the two former pages depending on the parent analysis's record type. This third page I've name 'WizardRedirector' is used to override the standard 'New' button of the components. It should only be accessed from the related list on a preexisting parent analysis. Everything works fine when accessed from there, however I've discovered when I go to the standard edit page of a component and click the 'Save & New' button from the edit page it fails to redirect. 

I understand the immediate reason for failure is a null reference error in the redirect() method, where the variable 'parent' is null. The ultimate reason this is null is because the when I call StandardController.getRecord() it is returning a blank record. When accessed from the parent's related list the record at least has the parent record's ID, but when accessed from the edit page of a component it does not have this. I'm trying understand why this is in particular.

Here is the extensions code: 
Consumable_Analysis_Component__c component;
  Consumable_Analysis__c parent;
  Id parentId;
  Map<Id, RecordTypeInfo> myMap = Schema.SObjectType.Consumable_Analysis__c.getRecordTypeInfosById();

  public RedirectorController(ApexPages.StandardController controller){
     try{
       this.component = (Consumable_Analysis_Component__c)controller.getRecord();
       this.parentId = this.component.Solution_Analysis__c;
       this.parent = [SELECT Id, RecordTypeId FROM Consumable_Analysis__c WHERE Id=:parentId limit 1];
    } 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;}
  
  }

 
Joshua Danis 7Joshua Danis 7
I'm confused as to why this should be an issue as according to this documentation: https://help.salesforce.com/articleView?id=links_override_considerations.htm&language=en_US&type=0 (https://help.salesforce.com/articleView?id=links_override_considerations.htm&language=en_US&type=0" target="_blank) overriding the edit page buttons is not possible yet that is exactly what is happening with my buttons. 

In any case the work around I used was to take the retURL param and try querying it as both objects and passing the param back as the parent ID.
Consumable_Analysis_Component__c component;
  Consumable_Analysis__c parent;
  Id parentId;
  Map<Id, RecordTypeInfo> myMap = Schema.SObjectType.Consumable_Analysis__c.getRecordTypeInfosById();

  public RedirectorController(ApexPages.StandardController controller){
     try{
       this.component = [SELECT PCS_Solution_Analysis__c FROM Consumable_Analysis_Component__c
                         WHERE Id = :retURL LIMIT 1];
       this.parentId = this.component.Solution_Analysis__c;
       this.parent = [SELECT Id, RecordTypeId FROM Consumable_Analysis__c WHERE Id=:parentId limit 1];
     ApexPages.currentPage().getParameters().put('CF00N3A00000CHfkV_lkid',parentId);
    } catch(System.Exception e){
       try{
          this.parentId = retURL;
            this.parent = [SELECT Id, RecordTypeId FROM Consumable_Analysis__c WHERE Id=:parentId limit 1];
       } catch(System.Exception f){
            ApexPages.addMessages(f);
         }
    } 
  } 
  
  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;}
  
  }