+ Start a Discussion
Dream_weaverDream_weaver 

Can we assign "Standard controller " attribute dynamically in any VF page????

Is it possible? It would have saved a lot of effort of writing  similer VF pages of different  controller..

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

No, but you can use components to this effect. Here is a common idiom that I use:

 

public with sharing class standardControllerExt {
  public ApexPages.StandardController standardController { get; set; }
  public standardControllerExt(ApexPages.StandardController controller) {
    this.standardController = controller;
  }
}

 

<apex:page standardController="Account" extensions="standardControllerExt">
  <c:commonComponent standardController="{!standardController}"/>
</apex:page>

 

<apex:component selfClosing="true" controller="commonComponentController">
  <apex:param name="standardController" required="true" description="The standard controller to use." type="ApexPages.StandardController" assignTo="{!standardContrller}"/>
</apex:component>

 

public with sharing class commonComponentController {
  boolean init;
  apexpages.standardcontroller controller;
  public commonComponentController() {
    init = false;
    // do NOT initialize anything that depends on "controller" here
  }
  public void setStandardController(ApexPages.StandardController con) {
    if(init) {
      return;
    }
    this.controller = con;
    // do more initialization here.
  }

  public ApexPages.StandardController getStandardController() {
    return controller; // Required for component to be happy.
  }
}

For example, we have this massive program we're about to release (ERP, here we come!) that uses "line items" in many different places (more than eight). We use one common component that is called by each page, resulting in very minimal duplicated code. The trick is how to initialize this setup correctly; follow this starting point, you should do okay.

All Answers

sfdcfoxsfdcfox

No, but you can use components to this effect. Here is a common idiom that I use:

 

public with sharing class standardControllerExt {
  public ApexPages.StandardController standardController { get; set; }
  public standardControllerExt(ApexPages.StandardController controller) {
    this.standardController = controller;
  }
}

 

<apex:page standardController="Account" extensions="standardControllerExt">
  <c:commonComponent standardController="{!standardController}"/>
</apex:page>

 

<apex:component selfClosing="true" controller="commonComponentController">
  <apex:param name="standardController" required="true" description="The standard controller to use." type="ApexPages.StandardController" assignTo="{!standardContrller}"/>
</apex:component>

 

public with sharing class commonComponentController {
  boolean init;
  apexpages.standardcontroller controller;
  public commonComponentController() {
    init = false;
    // do NOT initialize anything that depends on "controller" here
  }
  public void setStandardController(ApexPages.StandardController con) {
    if(init) {
      return;
    }
    this.controller = con;
    // do more initialization here.
  }

  public ApexPages.StandardController getStandardController() {
    return controller; // Required for component to be happy.
  }
}

For example, we have this massive program we're about to release (ERP, here we come!) that uses "line items" in many different places (more than eight). We use one common component that is called by each page, resulting in very minimal duplicated code. The trick is how to initialize this setup correctly; follow this starting point, you should do okay.

This was selected as the best answer
petergascoynepetergascoyne
Great solution @sfdcfox, I like how I can use the standard controller cancel button functionality. 

Just to note replace this line of code in the component:

<apex:param name="standardController" required="true" description="The standard controller to use." type="ApexPages.StandardController" assignTo="{!standardContrller}"/>

with:
<apex:attribute name="standardController" required="true" description="The standard controller to use." type="ApexPages.StandardController" assignTo="{!standardController}"/>
 
Best Regards,

Peter
Rosario Marano 4Rosario Marano 4
Hi guys,
thanks for the idea. I have an additional question.
How can I use the setStandardController method for setting the standard controller based on the record Id ?? I mean..I have a record with id 001..... Given this prefix I know this is an Account record. How can I set the standard controller of the visualforce page to Account??