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
bdstangbdstang 

Create Custom Controller Usable by Multiple Pages

I've created a custom controller that allows me to provide saving, cancelling, viewing PDF, and return URL navigation -- code below.  Works great, but it is specific to a single custom object, meaning that if I have pages bound to different custom objects, the way I have it structured, I have to have a custom controller for each custom oject. 

 

Is there a way to create a custom controller similar to the functionality mine is providing, but be able to reference it regardless of the custom object bound to the VF page?

 

Any help/thoughts woul be appreciated.

 

 

public class eaSaveReturn {E_A_Eligibility_Autism__c ea; public ApexPages.StandardController controller; public string retURL{get; set;} public eaSaveReturn(ApexPages.StandardController controller) { ea=(E_A_Eligibility_Autism__c)Controller.getRecord(); } public PageReference savereturn() { {upsert ea;} PageReference contactPage= new PageReference('/'+ApexPages.currentPage().getParameters().get('retURL')); return contactPage; } public PageReference cancelOnly() { PageReference contactPage= new PageReference('/'+ApexPages.currentPage().getParameters().get('retURL')); return contactPage; } public PageReference saveOnly() { {upsert ea;} PageReference contactPage= ApexPages.currentPage(); return contactPage; } public PageReference PDF() { {upsert ea;} PageReference contactPage= new PageReference('/apex/E_A_Eligibility_Autism_PDF?id='+ ApexPages.currentPage().getParameters().get('id')); return contactPage; } }

 

 

 

 

 

bob_buzzardbob_buzzard

You should be able to refer to the object as an sobject, rather than declaring a particular type.  I've done this for custom lookup and history controllers.

 

The only tricky bit is your PDF pagereference - as this is directing to a page based on the type, you'll need to inspect the sObject type and take action accordingly.

 

Reworked code is shown below.  This compiled (when using objects from my dev environment), but caveat emptor, I've not used it against a page:

 

 

public class eaSaveReturn { sObject ea; public ApexPages.StandardController controller; public string retURL{get; set;} public eaSaveReturn(ApexPages.StandardController controller) { ea=(sObject)Controller.getRecord(); } public PageReference savereturn() { {upsert ea;} PageReference contactPage= new PageReference('/'+ApexPages.currentPage().getParameters().get('retURL')); return contactPage; } public PageReference cancelOnly() { PageReference contactPage= new PageReference('/'+ApexPages.currentPage().getParameters().get('retURL')); return contactPage; } public PageReference saveOnly() { {upsert ea;} PageReference contactPage= ApexPages.currentPage(); return contactPage; } public PageReference PDF() { {upsert ea;} PageReference contactPage=null; if (ea instanceof E_A_Eligibility_Autism__c) { contactPage= new PageReference('/apex/E_A_Eligibility_Autism_PDF?id='+ ApexPages.currentPage().getParameters().get('id')); }

else

{

// set up page reference for other types here

} return contactPage; } }