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 SinghalAnkit Singhal 

Same 'Apply SMP' Custom button on Page Layouts of Account,Opportunity,Contact and Campaign

I want to use same Custom button on Page layouts of 4 different standard objects i.e. Account,Opportunity,Contact and Campaign.

The click of this button will redirect a User to a VF page.

Visualforce Page:

 

<apex:page standardController="Account" extensions="MyAccountController" >
<apex:pageBlock >
<apex:form >
<apex:pageBlockSection title="Corporate Templates">

 <apex:repeat value="{!CorporateSMPs}" var="d" id="corpSMPs">
 <apex:commandlink action="{!openSMP}" value="{!d.Name}">
 <apex:param name="paramName" assignTo="{!SMPName}" value="{!d.Name}" />
 </apex:commandlink>
   <br/>
 </apex:repeat>

</apex:pageBlockSection>
<apex:pageBlockSection title="Zone Templates">
<apex:repeat value="{!ZoneSMPs}" var="d" id="ZoneSMPs">
 <apex:outputlink value="/{!d.Id}">{!d.Name}</apex:outputLink>
   <br/>
 </apex:repeat>



</apex:pageBlockSection>
<apex:pageBlockSection title="Agent Templates">
<apex:repeat value="{!AgentSMPs}" var="d" id="AgentSMPs">
 <apex:outputlink value="{!d.Id}">{!d.Name}</apex:outputLink>
   <br/>
 </apex:repeat>
</apex:pageBlockSection>
</apex:form>
</apex:pageBlock>
</apex:page>

 

I have implemented this button on Account object Only.

Now I want to use this button on Opportunity,contact and Campaign as well.

I just need the Id of clicked Object page on click of button in the Controller.

 

Controller Code:

public class MyAccountController
{
    public Account acct;
    public String accNum;
    public Id AccId;
    public String SMPName{set;get;}
    public Account Act;
     public MyAccountController(ApexPages.StandardController stdController)
    {
       this.acct=(Account)stdController.getRecord();
       Act=[Select Id,Target__c,AccountNumber from Account where Id=:acct.Id];
       accNum=Act.AccountNumber;
      AccId=Act.Id;
    }
          // List<SMP_Template__c> CorporateSMPs = [SELECT S.SMP_Id__c,S.Name from SMP_Template__c S where S.Is_Available__c='Yes'and S.Target__c=:acct.Target__c and S.SMP_Office_Type__c='Corporate'];
    // List<SMP_Template__c> ZoneSMPs=[SELECT S.SMP_Id__c,S.Name from SMP_Template__c S where S.Is_Available__c='Yes'and S.Target__c=:acct.Target__c and S.SMP_Office_Type__c='Zone'];
    // List<SMP_Template__c> AgentSMPs=[SELECT S.SMP_Id__c,S.Name from SMP_Template__c S where S.Is_Available__c='Yes'and S.Target__c=:acct.Target__c and S.SMP_Office_Type__c='Agent'];
    public Pagereference openSMP()
    {
    return new PageReference('/apex/ApplyAccountSMP?Name='+SMPName+'&accountNumber='+accNum);
    }
    public List<SMP_Template__c> getCorporateSMPs()
    {
    If(acct!=null){
     //Act=[Select Id,Target__c,Name from Account where Id=:acct.Id];
   
       List<SMP_Template__c> CorporateSMPs=new List<SMP_Template__c> ([SELECT S.SMP_Id__c,S.Name from SMP_Template__c S where S.Is_Available__c='Yes'and S.Target__c=:Act.Target__c and S.SMP_Office_Type__c='Corporate']);
      return(CorporateSMPs);
 }
 return null;      }
 public List<SMP_Template__c> getZoneSMPs()
    {
    If(acct!=null){
    Account Act=[Select Id,Target__c from Account where Id=:acct.Id];
       List<SMP_Template__c> ZoneSMPs=new List<SMP_Template__c> ([SELECT S.SMP_Id__c,S.Name from SMP_Template__c S where S.Is_Available__c='Yes'and S.Target__c=:Act.Target__c and S.SMP_Office_Type__c='Zone']);
      return(ZoneSMPs);
 }
 return null;      }
 public List<SMP_Template__c> getAgentSMPs()
    {
    If(acct!=null){
    Account Act=[Select Id,Target__c from Account where Id=:acct.Id];
       List<SMP_Template__c> ZoneSMPs=new List<SMP_Template__c> ([SELECT S.SMP_Id__c,S.Name from SMP_Template__c S where S.Is_Available__c='Yes'and S.Target__c=:Act.Target__c and S.SMP_Office_Type__c='Agent']);
      return(ZoneSMPs);
 }
 return null;      }
 }

 

Kindly tell me if I can bring the Ids of any object in the controller.

 

Thanks,

Ankit

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You won't be able to do this using a standard controller, as that will error if it receives a non-account id.  There's a couple of options here:

 

(1) Make the page use a custom controller that retrieves the id, uses the describe methods to determine the type and then takes appropriate action. You'll need to make the button hit the URL rather than visualforce page directly though.

(2) Move the majority of the functionality into a custom component that takes the id as a parameter. Then create a page for each sobject type that uses the standard controller and then delegates to the custom component.

All Answers

bob_buzzardbob_buzzard

You won't be able to do this using a standard controller, as that will error if it receives a non-account id.  There's a couple of options here:

 

(1) Make the page use a custom controller that retrieves the id, uses the describe methods to determine the type and then takes appropriate action. You'll need to make the button hit the URL rather than visualforce page directly though.

(2) Move the majority of the functionality into a custom component that takes the id as a parameter. Then create a page for each sobject type that uses the standard controller and then delegates to the custom component.

This was selected as the best answer
Ankit SinghalAnkit Singhal
Thanks a lot Bob. It works using custom controller and buttons having content type as javascript.