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
VisualForceVisualForce 

Getting Record Id without using Controller and extensions

Hi..
 
     How to get a saved record id in visual force page without using controller and extension.  Here I am using Standard controller... I have to pass this id to next VF page...
 
     I am using oncomplete function in first VF page save button .. Here I can get the name of the object and other field details in a  oncomplete javascript function.. After getting this name I should pass this name to another VF page.. That page having one lookup for first page object.. So I can autopopulate the name in a lookup using script... After save this second page I have to go to first object detail page.. Here  I need a record id.... How to get this id...
 
        I am using Proffesional Edition... I cant use controller here....
 
Best Answer chosen by Admin (Salesforce Developers) 
mtbclimbermtbclimber
Not sure I am following you here. Are you saying your desired flow is:

1) Create a parent object on Visualforce page 1
2) Save the parent and go to Visualforce page 2
3) Create a child object on Visualforce page 2 using the parent Id from 2)
4) Save the child and go to the parent's standard detail page

Is this correct?

If so at which point are you having trouble?

All Answers

mtbclimbermtbclimber
Not sure I am following you here. Are you saying your desired flow is:

1) Create a parent object on Visualforce page 1
2) Save the parent and go to Visualforce page 2
3) Create a child object on Visualforce page 2 using the parent Id from 2)
4) Save the child and go to the parent's standard detail page

Is this correct?

If so at which point are you having trouble?
This was selected as the best answer
VisualForceVisualForce

Yes you are correct...

          My problem is I cant catch record id of parent object..

1) Create a parent object on Visualforce page 1 - Yes
2) Save the parent and go to Visualforce page 2 -Yes
3) Create a child object on Visualforce page 2 using the parent Id from 2) - Now I am Using Parent Name.. How to get a parent ID here .. This is my problem
4) Save the child and go to the parent's standard detail page - Yes
 
                    I cant use controller and extension here.. I am using Proffesional Edition..
mtbclimbermtbclimber

I'll probably move this solution to a wiki entry/blog post shortly but here's how you can workaround the no-custom controller limitation in Professional Edition to stitch something like this together.

 

Keep in mind that you are looking to craft a modification to the standard salesforce navigation flow which means you need to script something somewhere, fortunately there is JavaScript for that.

 

You also need to be aware of a couple of tricks, the first of which is the standard QuickSave method on the standard controller which is like the Save method except it keeps the user in place rather than redirect as per the standard salesforce navigation rules.

 

The second trick is assigning values from QS params into properties of a controller, in this case looking for an ID and assigning it to the parent relationship field on the standard controller's sobject property (the child record).

 

I've cooked up an example here that uses standard objects to make it easier for you and other interested readers to consume. In this case the parent is an Account and the Child is a Contact.

 

Page1: New Account Visualforce Page

<apex:page standardController="Account">
<apex:form id="theform">
<script>
var id = '{!account.id}';
if(id) {
window.location = '{!URLFOR($Page.contactNew, '',[accountId = account.id])}';
}
</script>
<apex:pageBlock title="ID: {!account.id}">
<apex:pageMessages />
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!quickSave}" rerender="theform" status="theStatus"/>
<apex:actionStatus startText="creating account..." id="theStatus"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:inputField value="{!account.name}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

NOTE: the above markup won't save until there is a page in your account named "contactNew", either create a stub for that page first or comment/remove the reference to $Page.contactNew until it exists.

 

Component: c:setProperty (custom)

<apex:component>
<apex:attribute name="property" type="String" description="The binding expression for the property to which the value will be assigned."/>
<apex:attribute name="value" type="String" assignTo="{!property}" description="The value to apply to the property"/>
</apex:component>

 

Page2: New Contact Visualforce Page (named "contactNew")

<apex:page standardController="Contact">
<apex:form id="theform">
<script>
var id = '{!contact.id}';
if(id) {
window.location = '{!URLFOR($Action.Account.View, contact.accountId)}';
}
</script>
<c:setProperty value="{!$CurrentPage.parameters.accountId}" property="{!contact.accountId}"/>
<apex:pageBlock title="ID: {!contact.id}">
<apex:pageMessages/>
<apex:pageBlockButtons>
<apex:commandButton value="save" action="{!quickSave}" rerender="theform" status="theStatus"/>
<apex:actionStatus startText="creating contact..." id="theStatus"/>
</apex:pageBlockButtons>
<apex:pageBlockSection>
<apex:inputField value="{!contact.lastname}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

NOTE: you'll get a runtime error for the null value passed into the URLFOR function here whenever contact.accountId is null, under the rules here that condition should not arise though you might want to account for that possibility by checking for null if desired.

 

This solution suffers from a lack of transactionality as there are two independent requests here. This can only be overcome with a custom controller. There's nothing forcing the user to complete the process. If your user bails out after creating the parent and before completing the child, the parent will exist but the child will not.

 

Message Edited by mtbclimber on 02-01-2009 01:02 PM
seshu paturuseshu paturu
As mentioned by @mtbclimber:

<script>
   var id = '{!account.id}';
</script>

This works fine and a simple solution if we are using a standard controller.