+ Start a Discussion
anup-prakashanup-prakash 

an answer required for an interview question

Hi

 

            I was facing a telephonic round of an interview and the interviewer had asked me this question.. I tried to ask people working for over 2 years on salesforce and a team Lead as well but couldn't find the answer.. So here I am.. where I've always got the help, when looked for.. So here Is the Question

 

Int: can we  use use Multiple extensions?

Me: Yes, we can.

Int: we have three extensions ext1, ext2, ext3.. and all of them have the method Method1.. and I call this method  in a VFPage whose method will be called..

Me: The one which is on he left most side.

Int: I want to call the Method of Ext2/Ext3.. How will I do so?

Me: Numb!! Job Gone!! Me yet Jobless..  :(

 

 

Bhawani SharmaBhawani Sharma
Multiple extensions are to extend the Conntroller functionality. Like:

If you have defined a standardController="Account" on page and extensions="Class1, Class2, Class3". Now you use a action={!save} on page.
1. Assuming that none of the class is having this save method. So page will automatically call the StandardController's save method.
2. If Class 1 has save method, that will be called first.
3. Class 1 doesn't have, but Class 2 has, that will be called.
4. Class 1 and 2 don't have, but Class 3 has, that will be called.
5. if someone want to call a method from Class2, don't need to define that on Class1.
anup-prakashanup-prakash

But what if the save is defined on all class1, class2 and class3.. and then I had to choose the class from which I want to cal it from ...  then how could that be achieved??

Bhawani SharmaBhawani Sharma
You can not do that. if save is defined in all three classe, page will call first controller's save method.
If you want to call the class2 save method, you will have to do it from class1 save method.
Yoganand GadekarYoganand Gadekar

If you have method's of same name in all of your extenssions then one in the left side class will be called default.

 

Thanks,

Salesforce SolutionsSalesforce Solutions

One way to accomplish this would be to design the first extension method to conditionally call another extension's method.

For example:

The first extension:

public class ExtOne {

    private final Account acct;

    public ExtOne(ApexPages.StandardController acon) { 
       id acctID = ApexPages.currentPage().getParameters().get('id');
       if(acctID != null) {
           this.acct = [SELECT Id, Name, Site FROM Account 
                   WHERE Id = :acctID ];
        }
    }
    
    public String getFoo() {
        String retVal;
        if(acct==null) {
            retval = 'No ID specified.';
        } else if(acct.name=='Pyramid Construction Inc.') {
            retVal = acct.name;
        } else {
            ApexPages.StandardController controller = new ApexPages.StandardController(acct); 
            ExtTwo Ext2 = new ExtTwo (controller);       
       
            retval = Ext2.getFoo();
        }
        return retVal;
    }
}

 The second extension:

 

public class ExtTwo {
    public ExtTwo(ApexPages.StandardController acon) { }

    public String getFoo() {
        return 'This is not Pyramid Construction Inc.';
    }
}

 

The page:

 

<apex:page standardController="Account" 
    extensions="ExtOne,ExtTwo" showHeader="false">
    <apex:outputText value="{!foo}" />
</apex:page>

 

If you load this page without an account id on the query string, it displays "No ID specified".
If you load it with the ID of an account named "Pyramid Construction Inc." it displays that name.
If you load it with the ID of an account NOT named "Pyramid Construction Inc." it displays "This is not Pyramid Construction Inc.."

 

(The question of whether or not this is good object-oriented design is a whole other topic.)