0

I am attempting to use a custom button inside a VF page to gather the current record's ID and then in turn launch a Flow. In order to do that I need to launch another VF page in the back-end which launches the Flow, captures the record ID, and assigns it to a variable - which I later use in the Flow.

The button works fine if it is in a Page Layout. But I need to render the object in a VF page for other reasons. So, I added the button to the VF page. Now, the button still launches the Flow properly, but it doesn't capture the record ID and the variable is null.

Here is the Visualforce page which contains the button. The button is "Create A Link To Another Vendor" :

<apex:page standardcontroller="Vendor_Profile__c" extensions="ProfileDetailExtension">
<apex:messages />
    <apex:sectionheader title="{!$ObjectType.Vendor_Profile__c.label} Detail" subtitle="{!Vendor_Profile__c.Name}"/>
    <chatter:feedwithfollowers entityId="{!Vendor_Profile__c.Id}"/>
    <apex:form >
        <apex:pageblock mode="maindetail" title="{!$ObjectType.Vendor_Profile__c.label} Detail">
            <apex:pageblockbuttons >
                <apex:commandbutton value="Edit" action="{!Edit}"/>
                <apex:commandbutton value="Delete" action="{!Delete}"/>
                <apex:commandbutton value="Create A Link To Another Vendor" action="{!launchFlow}"/>

Here is the extension to that main VF page :

public class ProfileDetailExtension {
    public ProfileDetailExtension(ApexPages.StandardController stdController) {
    }

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

This is the back-end VF page which is launched by that button/extension. This is where the Flow is launched and the record ID of the record which is open at the time the button is pressed is captured. :

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

This is the extension of that back-end VF page :

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;
        }
}

I'm guessing that it won't work properly because I am trying to capture the record ID in that back-end VF page which doesn't have access to the record. But I have to do that because I need to capture ID and save it to a variable within the flow:interview commands in order for the variable to be available during the Flow.

Any ideas ? I appreciate any help.

Thank you.

0
2

Can you just pass the record id as a parameter with the pagereference for the second vf page?

Here is the extension to the first VF page containing the button :

public class ProfileDetailExtension {
    public ProfileDetailExtension(ApexPages.StandardController stdController) {
            }

    public PageReference launchFlow() {
         PageReference flowPage = new PageReference('/apex/New_Related_Vendor_with_Flow');
         flowPage.getParameters().put('recordId', ApexPages.currentPage().getParameters().get('id'));
         return flowPage;
     }
}

Note: this assumes that you are passing a record id to the first visualforce page as a url parameter. If for some reason that doesn't work see here for another example of how to get the record id from a controller extension: http://www.davehelgerson.com/?p=407

Then get it in the constructor for the flow page extension:

This is the controller for the page that launches the flow

public class New_RelatedVendor_With_Flow_Controller {
    public Id recordId {get;set;}
    public flow.interview.New_Related_Vendor myflow {get;set;}

    public New_RelatedVendor_With_Flow_Controller(ApexPages.StandardController stdController) {
        recordId = ApexPages.currentPage().getParameters().get('recordId');
    } 

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

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

then pass recordId to your flow:

This is the VF Page containing the flow

<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="{!recordId}"/>
    </flow:interview>
</apex:page>
8
  • That's really great @Jason. I really appreciate it. I think you've got it, I'm just coming into some errors - one that I got was in the first pagereference. The line with 'flowPage.getParameters( ).put..... is producing an error : "Incompatible value type Schema.SObjectfield for <String,String>" I don't understand that error. Do you have any ideas on that ?
    – Zoom_v
    Dec 10 '15 at 18:50
  • 1
    Turns out you can't use Vendor_Profile__c.Id in the controller extension, but you should be able to get the id in the first extension like this ... .put('recordId', ApexPages.currentPage().getParameters().get('id')); Does that work?
    – Jason
    Dec 10 '15 at 19:49
  • I guess you're saying to use the that whole line in the extension for the back-end VF page ? I'm also confused on what exactly the line would be. Could you possibly write it out like you did in your previous post, or maybe just edit that one ? ...and I'm assuming it would still need to be referred to in the same way by the param in the back-end VF page...?
    – Zoom_v
    Dec 10 '15 at 20:33
  • I edited the answer above (should have done that in the first place). Hope that helps!
    – Jason
    Dec 11 '15 at 13:20
  • 1
    Hi Zoom, I tried editing one more time to be more clear about where the code goes and what it is doing: in the first controller, "flowPage.getParameters().put('recordId', ApexPages.currentPage().getParameters().get('id'));" is getting the current record id and passing a parameter to the second page called "recordId". The controller is getting that parameter and setting the recordId variable in the controller: recordId = ApexPages.currentPage().getParameters().get('recordId'); Finally that recordId variable is what you are passing into the flow as the record id <apex:param ... value="recordId"
    – Jason
    Dec 11 '15 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.