2

I have a search box that will pull into all accounts we currently have. It will then direct to a custom record detail page that shows account's main information with the equipment they currently have( related list) when you click "view info"

my search visualforce page uses a controller to query by email:

 public void searchcontroller( ) {
 }
 public void search(){
 string searching='select id, Name, PersonEmail, BillingStreet, 
 BillingCountry, BillingCity, BillingState, BillingPostalCode, 
 PersonHomePhone  from account where PersonEmail like 
 \'%'+searchstring+'%\' Limit 10';
acc= Database.query(searching);
       }
      public void clear(){
     acc.clear();
     }
    }

but my second visualforce page uses the standardcontroller for "Account" and an extension "EquipmentInfoController":

   public with sharing class EquipmentInfoController {
   private Account account;

   private list<equipment__c> equip;
   public equipmentInfoController(ApexPages.StandardController 
    controller) {
    this.account= (Account)controller.getRecord();
    }
     public List<equipment__c> returnEquip()
    {
    equip = [select id, Name, gear__c, account__r.Name, 
    account__r.PersonEmail from Order__c Where 
    account__r.id=:account.id];

    return equip;
    }
}

I am able to pass the account id from the search VF page to the Equipment Info page and see all of the accounts info and the equipment they currently have, but how do I combine both apex classes into one class? Or do I need to use two separate classes since one visualforce page uses a standard controller?

1 Answer 1

3

You can definitely do this.

The key is to have two different constructors - one that accepts the standard controller and one with empty params.

Something like:

public class CombinedAwesomeController{

   private Account account;
   public CombinedAwesomeController(ApexPages.StandardController 
       controller) {
       this.account= (Account)controller.getRecord();
   }
   public CombinedAwesomeController(){
   }

   public void search(){
       String searchString='SELECT id, Name, PersonEmail FROM Order';
       //etc
   }
}
4
  • If this solved the problem, you can mark as accepted and or upvote the answer. May 11, 2018 at 2:03
  • If I wanted to add another standard controller to the same class how would I go about that?
    – J.koh
    May 11, 2018 at 2:47
  • nope, just add another constructor :) - actually, sorry, that won't work, ... yeah, you could work out the sobjecttype of the record and use an if/else as you say to assign the record. May 11, 2018 at 2:47
  • You could also have a controller that was really just a wrapper and handled the different account/order/contact objects before handing over the the real, internal controller. May 11, 2018 at 2:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .