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
Zoom_VZoom_V 

How can I add this Detail Page Button to a Visualforce page?

I am attempting to add the following button to a Visualforce page, which is functioning as the override to the View of an object.

The button will call another VF page - which in turn will call a Flow. I know the code works because I've used the button when using the object in a Page Layout.

Here is the following code for both the VF page it will call, and the extension/controller code it will run :

VF Page :
<apex:page StandardController="Vendor_Profile__c" Extensions="New_RelatedVendor_With_Flow_Controller">
<flow:interview interview="{!myflow}" name="New_Related_Vendor" finishlocation="{!finishlocation}"> 
<apex:param name="VarPrimeVendProfRecID" value="{!Vendor_Profile__c.Id}"/>
</flow:interview>
</apex:page>

Extension :
 
public class New_RelatedVendor_With_Flow_Controller {
    public New_RelatedVendor_With_Flow_Controller(ApexPages.StandardController controller) {
         }

public flow.interview.New_Related_Vendor myflow {get;set;}
    public New_RelatedVendor_With_Flow_Controller() {
    }    

    public String getendID() {        
        if (myflow !=null) return myflow.VarPrimeVendProfRecID;
        else return 'home/home.jsp';
        }       

    public PageReference getFinishLocation() {        
        PageReference endlocation = new PageReference('/' + getendID());
        return endlocation;
        }
}

Thank you for any help you can provide. 
 
Best Answer chosen by Zoom_V
pconpcon
So in the parent Visualforce page you would add the command button show above and then you will need to have a create a controller extension for your page

MyExtension.cls
public class MyExtension {
    public MyExtension(ApexPages.StandardController stdController) {
    }

    public PageReference launchFlow() {
        return Page.FlowPage;
    }
}

Page
<apex:page standardcontroller="Vendor_Product__c" extensions="myExtension">
    <apex:messages />
    <apex:sectionheader title="{!$ObjectType.Vendor_Product__c.label} Detail" subtitle="{!Vendor_Product__c.Name}"/>
    <chatter:feedwithfollowers entityId="{!Vendor_Product__c.Id}"/>
    <apex:form >
        <apex:pageblock mode="maindetail" title="{!$ObjectType.Vendor_Product__c.label} Detail">
            <apex:pageblockbuttons >
                <apex:commandbutton value="Edit" action="{!Edit}"/>
                <apex:commandbutton value="Delete" action="{!Delete}"/>
                <apex:commandbutton value="Launch Flow" action="{!launchFlow}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:pageblockbuttons>

You will want to update line 6 of the extension to point to your VF page and you'll also want to update 11 of the Page to change the name of the button.  You will also want to update the class name of the extension to something more meaningful than myExtension

All Answers

pconpcon
And adding it as a detail page button with a content source of Visualforce does not work?
Zoom_VZoom_V
@pcon - what would be the easiest way to do that ?
pconpcon
From the object page you want to create the button, scroll down to the Buttons, Links, and Actions section and click New Button or Link.  Fill out the information on this page and choose Visualforce from Content Source and choose your Visualforce page from the dropdown.  Then after saving the button add it to your page layout.
Zoom_VZoom_V
Yes, I already have created the button itself and it works in the Page Layout of the object. But I don't want to use the Page Layout for the object. I want to put it into a VF, which I've also done. Now I want to know what would be the proper VF coding to put the button in my original post into a VF page. All I know it would be something like <apex:commandButton action=..... Would that work ? If so, what would be the rest of that line ?
pconpcon
You can do
<apex:commandButton action="{!startFlow}" />

and then in your controller do
 
public PageReference startFlow() {
    return Page.myFlowPageName;
}

You could also use apex:commandLink and then write custom CSS to make it styled like a button.

 
Zoom_VZoom_V
pcon - thank you very much for your help on this. I'm still trying to figure out how that fits into my current controller/extension I already have above. Can you help me with that ? Thanks again.
pconpcon
The page and extension you have above is what you are trying to redirect to when your user clicks the button, correct?
Zoom_VZoom_V
That's correct, yes.
pconpcon
Then you will need to modify you "parent" page to include the code for the apex:commandButton.  If your "parent" page is not a Visualforce page then you will need to create a custom button and add it to your page layout.
Zoom_VZoom_V
Yes, the "parent" page is a Visualforce page. Here is the beginning of it, including the pageblockbuttons : 
 
<apex:page standardcontroller="Vendor_Product__c">
<apex:messages />
                               <apex:sectionheader title="{!$ObjectType.Vendor_Product__c.label} Detail" subtitle="{!Vendor_Product__c.Name}"/>
                               <chatter:feedwithfollowers entityId="{!Vendor_Product__c.Id}"/>
                               <apex:form >
                                                             <apex:pageblock mode="maindetail" title="{!$ObjectType.Vendor_Product__c.label} Detail">
                                                                                            <apex:pageblockbuttons >
                                                                                                                          <apex:commandbutton value="Edit" action="{!Edit}"/>
                                                                                                                          <apex:commandbutton value="Delete" action="{!Delete}"/>
                                                                                                                         
                                                                                            </apex:pageblockbuttons>


But I have never needed to put a custom button on a VF page before, and this is confusing because it has to use that small VF page just to call my Flow along with assigning the parent record ID to a variable to use in the Flow. 

So I don't know how to properly put that button code of the VF and the controller you have into the ones I have.

Can you help me with that ? 

Thank you so much.
 

pconpcon
So in the parent Visualforce page you would add the command button show above and then you will need to have a create a controller extension for your page

MyExtension.cls
public class MyExtension {
    public MyExtension(ApexPages.StandardController stdController) {
    }

    public PageReference launchFlow() {
        return Page.FlowPage;
    }
}

Page
<apex:page standardcontroller="Vendor_Product__c" extensions="myExtension">
    <apex:messages />
    <apex:sectionheader title="{!$ObjectType.Vendor_Product__c.label} Detail" subtitle="{!Vendor_Product__c.Name}"/>
    <chatter:feedwithfollowers entityId="{!Vendor_Product__c.Id}"/>
    <apex:form >
        <apex:pageblock mode="maindetail" title="{!$ObjectType.Vendor_Product__c.label} Detail">
            <apex:pageblockbuttons >
                <apex:commandbutton value="Edit" action="{!Edit}"/>
                <apex:commandbutton value="Delete" action="{!Delete}"/>
                <apex:commandbutton value="Launch Flow" action="{!launchFlow}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:pageblockbuttons>

You will want to update line 6 of the extension to point to your VF page and you'll also want to update 11 of the Page to change the name of the button.  You will also want to update the class name of the extension to something more meaningful than myExtension
This was selected as the best answer
Zoom_VZoom_V

pcon - Thank you so much on this. That was so helpful. I've got the button working in the VF page now, and it is in turn referring to another page (the one in my original post) - which as you can see is also using another extension. I am mainly using that other page and extension for the sake of not only launching the flow, but also collecting the current record's ID with the param (to be used in the Flow) and setting the finishlocation of the Flow as well. 

My problem is that my param/ID method isn't working. I'm guessing the best way to handle this would be to combine the VF pages (and Extensions) into one, but I'm not sure if that would work. This is where I was really losing it earlier, because I had a feeling they should be combined. Do you know how to do that ?

Thanks again. I really appreciate it.

pconpcon
So your VF page containing your flow loads correctly and has the Id in the parameter of the URL?  But in your actual flow (being passed via your apex:param you are not getting the record Id?
Zoom_VZoom_V

pcon - Thank you for your response. The button will properly launch the other little VF page (which in turn launches the flow) but the param in that VF page is not producing the ID of the record which is open when the button is being pushed.

You're correct in that it is the ID in the URL at the time the button is pushed, but I am gathering it by referring to the actual ID of the record.

<apex:param name="VarPrimeVendProfRecID" value="{!Vendor_Profile__c.Id}"/>

(the entire page is in my original post) 

If the custom button is used in a Page Layout it will properly get the ID and save it to the variable. But it's not doing that when the button is put into this VF page. 

I'm guessing it's because I'm essentially going from one VF page (the one with the button) - and then the button launches that other page which THEN tries gathering the record ID as it launches the Flow. Maybe it's because the ID can't be gathered from a VF page launched in the back-end like that ? Any ideas on that ? 


 

 

pconpcon
I would first start with adding an apex:outputText with the value of Vendor_Profile__c.Id to make sure that the Id is accessible.  After that, I would verify that the flow has the variable named VarPrimeVendProfRecID.  If you visit your the page with the flow embedded directly and provide the URL with the Id does that work?
Zoom_VZoom_V
That variable is in the flow. It populates properly if I just run the button in a Page Layout.

I don't understand what you mean with by this : "If you visit your page with the flow embedded directly and provide the URL...."  ... ?

Thanks for all of your help.
pconpcon
What is the URL you get when you click the button from the page layout?  And does this button take you to your Visualforce page or directly to the flow?