+ Start a Discussion
Muhammad Jawwad 16Muhammad Jawwad 16 

How to redirect to the newly created record in visualforce page?


I'm trying to redirect to the newly created record in visualforce, but failed.

Anybody help me, how to redirect after clicking the save button to the newly created record?

Please help


Muhammad Jawwad

Rounak SharmaRounak Sharma
hello Muhammad Jawwad,
Please try the way below:
Taking insertion of account as example in the approach, We will be using two redirects (PageReference in Apex Code)
1)Once the record is saved.
2)Once the user clicks on OK.
Create a command button that will call the apex function insert()
Button markup -
<apex:commandButton value="Save" action="{!insert}" />
Add method to your class, this method will redirect to the proceed page with account id as parameter in url
public pageReference insert(){ account acc = new account();
acc.name = 'testaccount';
insert acc;
PageReference pg = new PageReference('/apex/proceed?id='+acc.id);
return pg;
This code will take user to proceed page where the user will click on OK.
3.On the proceed page just add a command button which will redirect to the detail page of record.
<apex:commandButton value="OK" action="{!proceed}" />
Apex code
public pageReference proceed(){
PageReference pg = new PageReference('/'+Apexpages.currentPage().getParameters().get('id'));
pg.setRedirect(true); return pg;
Please let me know if you still need any help
Muhammad Jawwad 16Muhammad Jawwad 16
I'm working with javascript functions, how to do with that?
Rounak SharmaRounak Sharma
hello muhammad jawwad,
Please try the below approach

function callActionFUnction(){
call(); // Action function is called using name attribute of action function.
function saveHandler(){
// This function will be called after execution of saveFields action in controller.
} </script> <apex:actionfunction action="{!saveFields }" oncomplete="saveHandler()" name="call" reRender="success"/>
<apex:commandButton status="busy" value="Save" onclick="callActionFUnction()"/>

Please let me know if it helps