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
padmapadma 

How To Write a Extensions in a class

 Hi,
I am new in salesforce,How to  write a code for the extensions. how   it will be invoke the page?
Any one can explain this? please....
Thanks,
pa
 
Best Answer chosen by padma
viswanadh pviswanadh p
Hi Padma,

Extension for standard controller in class:

You just call the constructor and mention the parameters like this
public class myControllerExtension
{

public myControllerExtension(ApexPages.StandardController stdController)
{
}
}


Extension for Custom Controller in class

There is two type of methods:
See the Example on below:
1)
public class myControllerExtension
{
public myControllerExtension(
ApexPages.StandardController controllerParam)
{
}
}


2)
<apex:page controller="Custom__C"  Extension= "MyApexClass"> ... </apex:page>

public with sharing class MyApexClass
{
private ApexPages.StandardController ctrl;
public Custom__C custm {get;set;}
public MyApexClass()
{
try
{
acct = [select Name from Custom__C where Id = : ApexPages.currentPage().getParameters().get('id')];
}
catch (QueryException e)
{
custm = new Custom__C();
}
ctrl = new ApexPages.StandardController(custm);
}



Thanks & Regards,
Viswanadh

 

All Answers

PratikPratik (Salesforce Developers) 
Hi Padma,

Extension to a controller is an apex class. You can invoke/call the extension in visualforce page in  <apex:page> tag.

<apex:page  controller="controllername"  extensions=" extensionclassname">

</apex:page>

Let me know if need any further help.

Thanks,
Pratik
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

In addition to Pratik's answer, a controller extension is any Apex class containing a constructor that takes a single argument of type ApexPages.StandardController or CustomControllerName, where CustomControllerName is the name of a custom controller you want to extend.

Simple example:
public class myControllerExtension {

    private final Account acct;
    
    // The extension constructor initializes the private member
    // variable acct by using the getRecord method from the standard
    // controller.
    public myControllerExtension(ApexPages.StandardController stdController) {
        this.acct = (Account)stdController.getRecord();
    }

    public String getGreeting() {
        return 'Hello ' + acct.name + ' (' + acct.id + ')';
    }
}

Read the full article here: http://www.salesforce.com/docs/developer/pages/Content/pages_controller_extension.htm (http://www.salesforce.com/docs/developer/pages/Content/pages_controller_extension.htm" target="_blank)

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
viswanadh pviswanadh p
Hi Padma,

Extension for standard controller in class:

You just call the constructor and mention the parameters like this
public class myControllerExtension
{

public myControllerExtension(ApexPages.StandardController stdController)
{
}
}


Extension for Custom Controller in class

There is two type of methods:
See the Example on below:
1)
public class myControllerExtension
{
public myControllerExtension(
ApexPages.StandardController controllerParam)
{
}
}


2)
<apex:page controller="Custom__C"  Extension= "MyApexClass"> ... </apex:page>

public with sharing class MyApexClass
{
private ApexPages.StandardController ctrl;
public Custom__C custm {get;set;}
public MyApexClass()
{
try
{
acct = [select Name from Custom__C where Id = : ApexPages.currentPage().getParameters().get('id')];
}
catch (QueryException e)
{
custm = new Custom__C();
}
ctrl = new ApexPages.StandardController(custm);
}



Thanks & Regards,
Viswanadh

 
This was selected as the best answer