How to use salesforce PageReference to redirect after a new record insert?

367    Asked by Aalapprabhakaran in Salesforce , Asked on Apr 18, 2023

I want to redirect the user to a custom vf detail page when they create a new Service Order from my custom page order page (force_NewOrder).

I have created a extension which should save and redirect the user but I am given a generic error:

Error: Error occurred while loading a Visualforce page.

VF (force_NewOrder)
 public force_NewOrderLogic(ApexPages.StandardController controller) { } Service_Order__c order; public PageReference save() { insert order; PageReference orderPage = new PageReference('/force_OrderDetail?id=' + order.id); orderPage.setRedirect(true); return orderPage; } }
Answered by Aashna Saito

I found an answer here that might work for you, where the solution would be to change your "Save" method to the following:

Public salesforce PageReference saveAndRedirect() { if(controller.save() != null) { PageReference redirectPage = Page.force_OrderDetail; redirectPage.setRedirect(true); redirectPage.getParameters().put('id',controller.getId()); return redirectPage; } return null; }
Then change your action on the VF page to "saveAndRedirect".
Reason:
1) Your "save" function is shadowed by the standard controller's "save" function, so your code won't work. You have to rename the function, and call that action instead.


Your Answer

Interviews

Parent Categories