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 

Get the Record ID at the time of pressing a custom "New" button

I am using a custom New button in an object to launch a Visualforce page - which then launches a Flow. I would like to capture the Record ID of that record which I am in when I press the button and save the ID to a variable I can use within the Flow which gets launched.

How can I do this ?
Best Answer chosen by Zoom_V
pconpcon
I think you almost have what you need there, but you're syntax is just a little off
 
<apex:page StandardController="ObjectA__c" Extensions="New_ProductReview_With_Flow_Controller">
    <flow:interview interview="{!myflow}" name="New_Product_Review" finishlocation="{!finishlocation}">
        <apex:param name="VarVendProdRecID" value="{!ObjectA__c.Id}"/>
    </flow:interview>
</apex:page>

You need to have the apex:param inside the flow tag.  By having the /> on the end you close the flow tag and the param isn't applied to it.

All Answers

pconpcon
Is the flow starting before or after the object gets written to the database?  Or does the Visualforce page pass information to the flow and the flow does the creation?
Zoom_VZoom_V

As it is now, I have a custom button at the top of an object's (ObjectA) page layout which launches a VisualForce page - which in turn launches a Flow - which ultimately creates a record for another object (ObjectB).

At the time that the user presses the custom button and the Flow is launched (with a Screen element) the URL is showing "https://c.cs42.visual.force.com/apex/NameOfMyFlow?scontrolCaching=1&id=a48430345St".

That record id which is shown at the end of the URL is the id of the original record which the user is viewing at the time of pressing the custom button to launch the Flow. That is the id I would like to capture and save to a variable, and then use the variable within the Flow. 

There must be a way to do that. I have tried using the param command within the VF page which is launched in order to try and capture the id but that doesn't look to be working. I don't know if it's incorrect coding or if it's just not possible in the way I'm trying.

Here is the VisualForce page which is launched at the time the custom button is pressed within ObjectA :

 

<apex:page StandardController="ObjectA__c" Extensions="New_ProductReview_With_Flow_Controller">
<flow:interview interview="{!myflow}" name="New_Product_Review" finishlocation="{!finishlocation}"/>
 <apex:param name="VarVendProdRecID" value="{!ObjectA__c.Id}"/>
</apex:page>

I'm wondering if maybe there could be a way to capture the id by stripping it out of the URL - and then assigning it to the variable using param that way...?

Any help would be appreciated.

Thank you very much.
pconpcon
I think you almost have what you need there, but you're syntax is just a little off
 
<apex:page StandardController="ObjectA__c" Extensions="New_ProductReview_With_Flow_Controller">
    <flow:interview interview="{!myflow}" name="New_Product_Review" finishlocation="{!finishlocation}">
        <apex:param name="VarVendProdRecID" value="{!ObjectA__c.Id}"/>
    </flow:interview>
</apex:page>

You need to have the apex:param inside the flow tag.  By having the /> on the end you close the flow tag and the param isn't applied to it.
This was selected as the best answer
Zoom_VZoom_V

@pcon - That's it ! Thank you so much !!!!! If I could give you credit for Best Answer twice I would. 

What's up with this : The id which is being returned to me (and assigned to my variable within the Flow) at that time is NOT the same id of the original record. And yet I'm taking that id and plugging it into a lookup field within the newly created record - and when you go to that newly created record and click on that lookup field it correctly brings you to that original record, which is using that original id ! So, it's working properly - but I don't understand how it's doing it with a totally different id ! It's like that original record has two id's. Weird.

Any idea how that's working ?

pconpcon
How do they differ?  Is one 15 characters and the other 18 characters?  Are the 100% different?  Can you provide examples of what you're seeing?
Zoom_VZoom_V

Yes, one is 15 and the other 18. The one which is displayed in the URL at the time of viewing the original record (a7v560000004CSt) is the 15 and the one which is being returned by that VF page and plugged into my Flow is the 18 (a7v560000004CStAAM). I see that the second simply plugged 3 new characters at the end of the 15 one. If I copy and paste either one into the URL they work. 

So I guess Salesforce has 2 for each record ... ? 

pconpcon
That's a "normal" thing to happen [1].  15 character Ids are "the old way" to use them and 18 character ids are "the new way".  Both with work.  The 15 char is case sensitive and the 18 char is not.  The TL;DR it doesn't matter.

[1] https://help.salesforce.com/apex/HTViewSolution?id=000004383&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000004383&language=en_US)
Zoom_VZoom_V

ok, I just didn't know they would use both for the same record. I guess I never noticed it before when using Data Loader or what not. Still seems weird to me. 

Thanks again @pcon ! You've been great and I really appreciate it !