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
Ankit Chauhan 10Ankit Chauhan 10 

How can I use only one extension controller for Standard Salesforce object like Leads, Account, Contacts.

Hi All,

I'm trying to write a common controller extension for standard salesforce object like lead, account and contact. Since the extension controller constructor is generic I'm planning to use the same extension class (Controller class- APEX). Is there any cons to this approach. I'm basically collecting the email and phone from these standard object and perform some operation in real-time.

My approach in short: 
1. I will give a custom button on the Salesforce standard object like Lead, Account and contact Visualforce Page. (non-override approach)
2. Please note that this approach is only for interactive use case which means while creating or editing a record (Lead, Account and Contact) in Salesforce.
3. I'm planning to handle this custom button logic in Extension controller class (say CommonControllerExtension).
4. I will perform my logic on SO email and phone.

For example:
public CommonControllerExtension(ApexPages.StandardController sController)
    {
        this.sController = sController;
    }


Any help please.
Aniket Malvankar 10Aniket Malvankar 10
public class Ext_A_B {

    public Ext_A_B(ApexPages.StandardController controller){

        sObjectType sot = controller.getRecord().getsObjectType();

        if (sot == Lead.sObjectType){
           // LEAD LOGIC
        }
        else if (sot == Contact.sObjectType){
           // CONTACT LOGIC
        }
    }

}

Yes, Possible please check the above code.


Thanks
Aniket

If you find the solution as Answer , please mark.
Ankit Chauhan 10Ankit Chauhan 10
Hi Aniket,

Is there any cons to this approach?

Thanks in advance.
Aniket Malvankar 10Aniket Malvankar 10
Ankit,

This is a approach, Best practice. More like Factory design Pattern.

Mark the replt ANSWER, if it has solved your purpose.

Thanks
Aniket