Thursday, May 27, 2010

Prepopulating values from a visualforce page to a standard page

Once I had this requirement, wherein I was asked to pre-populate some of the values from a visualforce page to a standard page.

The flow can be like this (just an example):

1. Create a custom List button on Account, let's name it "Quick Account".

2. This button opens a visualforrce page. This page has an input text field (say Your Account Name) and one command button (say Go Create) in it.

3 . In your controller get and set the value for the text field and store it in a String (say your String variable name is "myAccountName")

4. In the action method of the button (returning a PageRefrence) return a page as below-
PageRefrence accPage = PageRefrence('/001/e?acc2=myAccoontName');
return accPage;

5. Bingo, now when you click on the Quick Account button, you will see a standard account edit page with pre-populated Account Name field.


The idea is that the name of the fields on the HTML have some very standard conventions. e.g. for account object, Account Name field had the HTML Id as 'acc2'. So the some of the standard fields are named as acc3, acc4 and so on.

Also for custom fields, the salesforce Id is used as HTML id. e.g. let us say a custom field on Account is 'My Name' and the salesforce Id of the fields is 'xxxxxxx'. So to pre-populate this field also you can do as:
PageRefrence accPage = PageRefrence('/001/e?acc2='+myAccoontName+'&xxxxxxx=swaleh');

and again bingo........

Happy clouding :)