+ Start a Discussion
aebenaeben 

Not able to link a VF page to a Custom Button

I have a custom button linking to a S-Control. After rewriting the S-Control to an VF page, I am trying to make the button point to the VF page. When I pick the Content Source as Visual Force Page, the Content drop-down just displays an empty list. It doesn't let me pick a VF page.
 
Any help would be appreciated.
JimRaeJimRae

According to the help:

Only Visualforce pages that use the standard controller for the object on which the button appears can be selected. For example, if you want to use a page to override the Edit button on accounts, the page markup must include the standardController="Account" attribute on the <apex:page> tag:

My guess is that you are using a custom controller, and that is why it is not visible.  You should look into using a standard controller with an extension for your customization, that should allow you to do what you are trying to do.
aebenaeben

Thanks for the response.

You are right.  I am using a custom controller. Sorry I didn't mention it before.

So There is no way we can attach a VF with customer controller to a customer link/button?

 

JimRaeJimRae
Not a problem.  I believe you can do it with a controller extension.
 
When you define your page, do it like this:
Code:
<apex:page standardController="Account" extensions="mycustomcontroller" >

 
Look in the VF documentation for information on how to modify your controller to be an extension controller.
aebenaeben

Thanks again.

The reason I opted for customer controller is, the VF page in discussion can be invoked from Account or Lead or Contact pages.

So now if I add a one org constructor in my customer controller and have Account as my standard controller just for the sake for creating an extension, would that help?

aebenaeben
No. It does not work.
 
My VF page can be navigated to from Account or Lead or Contact.
 
In my VF page,
 
standardController='Account'
extensions='myController'
 
It works only if the parameter that I pass to VF page is an Account.id. If I pass Lead.id, it fails saying that the id is not compatible with the standard controller.
 
 
JimRaeJimRae

Sorry, I didn't know you were trying to attach it to multiple objects.

I am not sure if someone else has any ideas, my only thought would be to have 3 versions of the page, one for each standard object, and have them all point to the same custom controller ( you will need logic to get the controller extension to work correctly).

 

aebenaeben

Have three diff versions of the same page is not an option. We had ruled that out.

I am thinking of using a URL or Java Script content type for the custom link instead of Visual Force Page. I will update this thread when I am done with it. Thanks.

 

mtbclimbermtbclimber
Can you post your page/Controller so we can see what you are trying to do here? This will help us design a mechanism to support what you are trying to do. Better yet would be to also create an idea on the idea exchange :)

Thanks,
hokusaihokusai
Use an Scontrol that returns a URL using the following format

"/apex/insertpagenamehere?paramname1=param1&paramname2=param2"
aebenaeben
Here is what I am trying to accomplish

1. Page can be accessed from Account or Lead or Opportunity.
2. We has a custom objects for our organization. The page allows the users to search for those custom object. So we have form with some text fields to accomplish this.
3. After the search is done, user can pick a record and the list and click on a save button. The  action method behind the save button takes care of creating a relationship  between the custom object and  either Account or Lead or Opportunity based on where the request came from.
4. When the save is successful, user is redirected to Account or Lead or Opportunity based on where the request came from.
5. Page also has a cancel button. At point if the user clicks the Cancel button, user gets redirected to Account or Lead or Opportunity.

Controller has all the logic to keep track of where the request came from and where it has to go when it is done.

If you need the code, I can work up something.

Since the page can be accessed from different objects, we cannot use a standard controller or controller extensions. Only option is to choose a custom controller. But, when you use a custom controller it cannot be linked to a button or link with a content type Visual Force Page. You have choose either URL or Java Script as the content type.

I have found this to be working perfectly fine. Does using URL or Java Script instead of Visual Force as content type makes a difference, I am not sure.

So What I am suggesting is, Have provisions to attach a Visual Force Page with a Custom Controller to a custom button or Link with content type Visual Force Page.


Kirk F.ax361Kirk F.ax361
To continue Hokusai's idea:

1) create your custom controller.
Your custom controller is expecting 2 paramters: let's say "ot" for object type and "id" for the object ID.  You'll have to write code to make it smart enough to take any object type (like account, lead, opportunity) and use the object ID to find and work with that object.  A big if/then/else, probably.

2) create your visual force page.  It refers to the custom controller only.
(let's call it "doAmazingThing")
(URL syntax would be /apex/doAmazingthing?id=1234&ot=Account)

3) create 3 very small visualforce pages, one for each of the objects where you want the button to appear.  These helper pages are completely identical, except a) They refer to the standard controller they're based on, like one based on Account and the next based on Lead, and the third on Opportunity.  b) they add a new parameter, called OT, and set the parameter to "Account" or "Lead" or "Opportunity".  These pages do nothing but take the ID they were given, add the OT, and then redirect the user to the page above with the ID and OT passed along.  Think of these as a layer of abstraction around your main visualforce page, allowing it to be used from different objects. 
(let's call them "doAmazingThingFromAccount" and "doAmazingThingFromLead" and "doAmazingThingFromOpportunity")
(URL syntax would be "/apex/doAmazingthingFromAccount?id=1234")

4) Naturally, create a button on each object that calls the correct helper page.  So on your account, you'll create a new button that calls the "doAmazingthingFromAccount" page.

So, the user clicks the button (say, on an account page), which takes the current account ID and goes to the doAmazingthingFromAccount page.  That page takes the ID it was given, adds the new parameter ot=Account, and immediately redirects the user to the generic doAmazingThing page.  You're done.

One of the strengths of this design is you can change the code in your doAmazingThing routine, and it will immediately be available to the contact, account, and other buttons.  All your code is in one place, except for the small helper pages that redirect the users there.  It'll make writing your test classes easier.  ;-)

I think this might be what Hokusai was suggesting.