6

My workflow looks something like this:

  1. I have a button in the related list to create a NEW custom object record with a custom visualforce page (VFNew.page)
  2. Open the saved record with an Edit button which is overridden with a custom visualforce page (VFEdit.page)

Here where it gets a bit complicated: both the visualforce pages have the same visualforce component embedded into them.

VFNew.page (used in a button from the related list) ->

<apex:page id="page" docType="html-5.0" standardController="Custom__c" recordSetVar="cItems" ..>
   <c:customComponent />
</apex:page>

VFEdit.page (used in an Edit button override)->

<apex:page id="page" docType="html-5.0" standardController="Custom__c" ..>
   <c:customComponent />
</apex:page>

CustomComponent.component ->

<apex:component extensions="CustomController" allowDML="true">
</apex:component>

Controller class ->

public with sharing class CustomController{

   public CustomController(){
      System.debug('1');
      init();
   }

   public CustomController(ApexPages.StandardSetController controller){
      System.debug('2');
      init();
   }

   public CustomController(ApexPages.StandardController controller){
       System.debug('3');
       initEdit();
   }
}

What I had hoped for is that when the VF is called from the related list button then the constructor 2 will be called because of the StandardSetController parameter and when the VF is called from the overridden Edit button then the constructor 3 will be called because if the StandardController parameter --- but in all the cases the constructor 1 is called - always.

Is there another way to find out in constructor 1 which Controller parameter is available so that I can get the Id if it comes from the Edit page?

2 Answers 2

3

You can pass variables from a visualforce page to a component. The problem is that this attribute will be assigned after the call of the controller constructor. But there is a workaround by calling the method you need in the variable setter. For example:

CustomComponent.component

<apex:component extensions="CustomController" allowDML="true">
    <apex:attribute name="IsFromRelatedList" type="Boolean" required="true" assignTo="{!IsFromRelatedList}" description="From where this component is called" />
</apex:component>

VFEdit.page (used in an Edit button override)

<apex:page id="page" docType="html-5.0" standardController="Custom__c" ..>
   <c:customComponent IsFromRelatedList="false" />
</apex:page>

VFNew.page (used in a button from the related list)

<apex:page id="page" docType="html-5.0" standardController="Custom__c" recordSetVar="cItems" ..>
   <c:customComponent IsFromRelatedList="true" />
</apex:page>

Controller class

public with sharing class CustomController{
   public Boolean IsFromRelatedList {get;set{
        this.IsFromRelatedList = value;
        if(value == true){
            //do what you want if the call is from the related list
        }else{
            //do what you want if this is not from the related list
        }
    }}

   public CustomController(){
      System.debug('1');
      init();
   }
}
2
  • I actually found another way to accomplish this before you answer came in -> using the page Id parameter in the constructor 1 to find out where the button click comes from and then calling the corresponding init function. But i tried your solution as well and was kinda successful with it ... just when calling any init functions from the variable setter it gave me some errors - which i did not want to spend too much time on it. But ill mark your answer thanx for the quick response
    – owsata
    Mar 28, 2017 at 13:58
  • Thanks but you know that you can answer your own question and mark it as the right answer ! It can be helpful in the future to other people with the same problem. Mar 28, 2017 at 14:00
0

I actually found another way to accomplish this before the other answer came in -> using the page Id parameter in the constructor 1 to find out where the button click comes from and then calling the corresponding init function.

1
  • Could you please post your code? Mar 28, 2017 at 14:06

You must log in to answer this question.

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