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
AQAQ 

Calling Visualforce page from Custom List Button

I am trying to call a Visualforce page from a custom list button.  I have done this successfully from a detail page button by creating a page using a standard controller for the detail object, selecting Visualforce page as the content source and then picking the page I wanted from the pull down list for content.
 
However, I can't seem to get this to work for a list button.  I pick the list button option on the custom button or link page, and pick visualforce page for the content source, but I don't get my visualforce page to appear in the content pull down list.
 
Is there something I'm doing wrong?
 
As an adjunct question, should I be using the standard controller for the detail object being displayed at the top of the page or for the objects being referenced in the list (in the case I'm working on now, the objects in the list are the same object as the detail object and reference back to the detail object)? 
 
I understand that one of the benefits of using a visualforce page in this manner is that the id for the detail object is automatically passed to the visualforce page, thereby making data display easier on the detail page.  If a visualforce page can be invoked from a list button, what object id is passed?  That of the detail item at the top of the page?
 
Thanks in advance for your help,
Jeff
 
Best Answer chosen by Admin (Salesforce Developers) 
AQAQ

In order for a visual force page to appear while creating a custom button on a detail page, you must use the standard controller for the object that will appear on the detail page.  Therefore, if you wanted to create a custom button from the contacts detail page, you must you the contact standard controller on the visual force page. 

 

You can add a custom controller extension to contain all the various custom code and functionality you require.

 

The visual force documentation shows how to set this all up.

All Answers

JeremyKraybillJeremyKraybill
It's a little bit underdocumented, but the only way to get VF pages to show up as selections for an object's list view is to make sure their controller has the following style of constructor in it:

Code:
public MyListController(ApexPages.StandardSetController stdSetController) {
    // if you wish, do something with stdSetController.getSelected()
}

Then if your page uses your object type as its standardController and your custom controller as an extension, it will show up as a selection.

Jeremy Kraybill
Austin, TX

jwetzlerjwetzler
It's not under documented at all, it has it's own section in the Visualforce dev guide

Your page needs to make use of the standard list controller (standardcontroller with the recordSetVar attribute defined on your page).  If you want to add custom logic to your page via an extension (which I assume you do, since you already have an Apex controller associated to your page) then you need to define it as an extension on your page, and then you'll need to add the constructor Jeremy mentioned.
AQAQ

The example in the documentaion isn't exactly what I'm trying to do.  Assume that the opportunity list as shown in the documentation is linked to the account, I want to create a different "New Opportunity" button which will call my custom visualforce page.  I don't want to override the existing button since I want to use the resultant code in a managed package.

On that custom visualforce page, I want to use something like <apex:detail/> to display the account detail information to serve as a reference while the user is inputting the new opportunity. Is there anyway with this technique to get access to the id for the parent (account) record?

I'm concerned about using a URL such as "/apex/mypage?accid={!Opportunity.AccountId}" with the custom button because I've read some posts here that suggest such usage will not work in a managed package.  If this kind of link would work, then my problem is solved.

Any suggestions on how I might accomplish this?

Jeff
myztakenmyztaken

 

 

AQ... Indeed usfing somethig like "/apex/mypage?accid={!Opportunity.AccountId}" doesnt work on a managed package...

 

Im having the same problem than AQ...

 

I made a custom button and placed it on the top of my Opp Layout and all i wanto to do is open a visualforce page with a parameter... If I want to do that on VF its as easy as writing:

 

 

<apex:outputLink value="{!$Page.myPage}"> Link Label

<apex:param name="oppId" value="{!opportunity.Id}"/> </apex:outputLink>

 I tried using $Page on a OnClick javascript with no luck..

 

If anyone knows how to accomplish this I would really appreciate it if you share it with us...

 

Thanks in advance...

 

~ foe

 

 

 

 

Srinivas_V2Srinivas_V2

There are two ways to do this

1.  send it like, apex/mypage?id={!Opportunity.Id}". handle it in the extensions Controller's Constructor. Fetch the accountId in the constructor by querying the opportunity. and set it as a property. then u can use it in the page

2. If the above does not suits, the other way round is,take a simple s-control

something like

<html>

<head>

var accountId = '{!Opportunity.AccountId} '

window.parent.location.href = "../apex/mypage?accId=" +accountId

</head>

</html>

 

AQAQ

I ended up reworking my code so that I always called a visual force page without parameters from the custom button.  In doing so, the controller automatically inserts the id of the record on which the button had been placed.  By using that id, I was able to eventually obtain the data that I needed.  Sometimes that took some imagination, but I haven't found any situations yet that I couldn't solve.

 

A simple example goes something like this:  I want to place a custom button on the opportunity screen that actually gets me to the account record, not the opportunity record.  The controller will automatically place the id of the opportunity as a parameter on the visual force page that I call.  I put an action="{!init}" statement into the the apex:page line which calls an init function in my controller extension.  The "init" routine in my extension extracts the id from the page parameter, loads the opportunity to which the id refers, extracts the account id from the opportunity that was loaded and then loads the appropriate account

 

Sometimes it requires more manipulation to get where I want to go, but as I said above, I've been able to rework all my controllers so that I'm now able to use visual force pages for all my custom buttons.  I suspect its possible that sooner or later this technique will fail, but for now its solved my problems.

 

 

nilnil

Hi,

 

I need to add a new Custom button (name:HelloWorld) in "Contacts" detail page, while creating the button when I select the Content source as 'Visualforce Page' I am not able to see any Visualforce page in content list.

 

I added my own controller which uses the uses apex generated by WSDL to get the data from the SOAP service.

 

Below is the code for custom controller & custom page.

 

public class HellowWorldController {

public String searchText;


public String searchText() {
// Apex class generated from WSDL file
helloworldTestSforceCom.HelloWorldServiceSoap p = new helloworldTestSforceCom.HelloWorldServiceSoap();
String searchText = p.HelloWorld();

return searchText;
}

}

 

  Created Visrualforce page called CustomPage

<apex:page controller="HellowWorldController">

<!-- Begin Default Content REMOVE THIS -->

<h1>Congratulations</h1>

This is your new Page

<apex:form >

<apex:commandButton title="ScreenName" action="{!searchText}" value="SearchName"/>

</apex:form>

<!-- End Default Content REMOVE THIS -->

</apex:page>

While creating the custom button on 'Contacts' I am not able to see this CustomPage in the content when I select the Visualforce Page

 

Can any please help me out how to use the Custom Controller for getting data from external SOAP service.

 

Thanks

AQAQ

In order for a visual force page to appear while creating a custom button on a detail page, you must use the standard controller for the object that will appear on the detail page.  Therefore, if you wanted to create a custom button from the contacts detail page, you must you the contact standard controller on the visual force page. 

 

You can add a custom controller extension to contain all the various custom code and functionality you require.

 

The visual force documentation shows how to set this all up.

This was selected as the best answer
nilnil

Thank you AQ

 

Manu ErwinManu Erwin

jwetzler wrote:
Your page needs to make use of the standard list controller (standardcontroller with the recordSetVar attribute defined on your page).  If you want to add custom logic to your page via an extension (which I assume you do, since you already have an Apex controller associated to your page) then you need to define it as an extension on your page, and then you'll need to add the constructor Jeremy mentioned.

 

 

Thanks Jill - that works a treat.

 

cheers,

manu 

Message Edited by manu on 04-22-2009 12:43 PM
jeremiah_smalljeremiah_small

Just a note in case this can save someone some time. If you're going to access getSelected() method on your StandardSetController, you must explicitly cast the sObject to the type you're expecting. getSelected() returns a generic sObject.

 

 

public MyListController(ApexPages.StandardSetController stdSetController) { // if you wish, do something with stdSetController.getSelected() // if your set is typed as (for example) Opportunity, do this: List<Opportunity> myOppList = (List<Opportunity>)stdSetController.getSelected(); }

 

 

 

jdk4702jdk4702

Hi folks - I'm attempting to implement this method to create a List View button that gets the selected rows & pass them to another method, but I'm getting an error :

 

 

Illegal view ID runSelectedRules. The ID must begin with /

 

Are these buttons supposed to return a page reference?  If so, how to I get the list view ID to return to the page after the controller method completes?

 

 

My List View button points to this VF page:

 

<apex:page standardController="Replacer__c" recordSetVar="r" extensions="replacerRunAllSelectedRulesController" action="runSelectedRules">
    
</apex:page>

 

 

Which call this controller when clicked:

 

public with sharing class replacerRunAllSelectedRulesController{

    ApexPages.StandardSetController setCon;

    public replacerRunAllSelectedRulesController(ApexPages.StandardController controller) {
        //Not sure why this is needed...
    }//replacerRunAllSelectedRulesController

    public replacerRunAllSelectedRulesController(ApexPages.StandardSetController controller) {
        setCon=controller;
    }//replacerRunAllSelectedRulesController
    
    public PageReference runSelectedRules(){
        List<Replacer__c> selectedRules=new List<Replacer__c>();
        for(sObject s: setCon.getSelected()){
            Replacer__c r=(Replacer__c)s;
            selectedRules.add(r);
        }//for 1
        String objectName='';
        Boolean sendEmail=TRUE;
        Integer numJobsCantBeRunNow=replacerExecute.replacerQueueBatchJobs(objectName, selectedRules, sendEmail);
        
//        return ApexPages.CurrentPage();
    }//runSelectedRules
    
}//replacerRunAllSelectedRulesController

I've tried other combo's too - putting the code in the set controller init and calling init, trying to return a different page ref, trying to return no pageref, but haven't hit anything that works yet :(

 

I should add that in all of my iterations, the debug logs show all the code runs, but nothing actually happens (methods aren't committed?).

 

textualtextual

so this thread help me out of a jam

before i was relying on query string info and now i have to change that logic

but overall, im in a better place thanks to this thread

 

:smileyhappy:

James76James76

I have a visual page with the standard controller set to my custom object: Contract Review

 

I want to create a Custom List button on the Contract Review related list in opportunities. However, when creating the button the content field is blank and there is no option to select my ContractReview VF page.

 

Any ideas?

 

 

textualtextual

for lists, you need a recordset var defined in your VF page

 

<apex:page standardController="Replacer__c" recordSetVar="r">

 

James76James76

Thank you ,easy when you know how.  I have another question my Custom object (Contract_Review__c) has a master-detail relationship with Opportunity and on my visualforce page I want to have an output field which displays the opportunity name. This value is not populating by using: 

<apex:outputField value="{!Contract_Review__c.Opportunity__c}"/> , I've tried many variation but just can't seem to get it right. Any guidance would be a great help.

TechOwlTechOwl

@ James76 : try to use this:

<apex:outputField value="{!Contract_Review__c.Opportunity__r.Name}"/>

 

"{!Contract_Review__c.Opportunity__c}" will just refer to the Opportunity ID - you need to use "Opportunity__r" to refer to the related Opportunity object itself.

abhishekshuklacs001abhishekshuklacs001

Is it possible to create STANDARD LIST CONTROLLER for TASK ?

BapuBapu

Thanks a lot ! that was useful. 

"The visual force documentation shows how to set this all up. " is it possible to explain with an example ? i tired but i lost some where (infact every where).

 

 

 

BapuBapu

Thanks a lot ! that was useful. 

"The visual force documentation shows how to set this all up. " is it possible to explain with an example ? i tired but i lost some where (infact every where).

 

 

 

dave.pvndave.pvn
@textual: Very helpful solution. recordSetVar works perfectly. Thanks :D