Showing posts with label Salesforce Page Redirect. Show all posts
Showing posts with label Salesforce Page Redirect. Show all posts

Friday, May 19, 2017

Instantiation of a Page Reference in Apex, Salesforce

In many cases, after a custom action performed, we may need to redirect the page to a different page, could be another Visualforce page / standard Page like a list view of an object or any of default pages like view / edit page of a record or a new record entry page.

Page redirection happens in Apex code using PageReference class.

There are multiple ways to instantiate a page reference in Apex. Below are list of all possible ways.

Approach 1:
PageReference pageRef = new PageReference('***partialURL***');
PageReference pageRef = new PageReference('***FullURL***');

Eg:

PageReference pageRef = new PageReference('/apex/CustomPage1?id=xxxxxx');
PageReference pageRef = new PageReference('/a04');

PageReference pageRef = new PageReference('http://www.google.com');

Approach 2:
PageReference pageRef = Page.<<VF Page Name>>;

Eg:

PageReference pageRef = Page.CustomPage;
pageRef.getParameters().put('id','XXXXXXX');

Approach 3:
Refer stnadard pages of an Object / record

PageReference pageRef = new ApexPages.StandardController(recordObj).edit();
PageReference pageRef = new ApexPages.StandardController(recordObj).view();

Refer List view of an object

PageReference pageRef = new PageReference('/'+<<Object>>.sObjectType.getDescribe().getKeyPrefix());

PS: Try not to hard code URLs and use above available options to build the URLs.

Thursday, March 19, 2015

Configure Redirect URLs after Record Saved in Salesforce

In general, after a record is saved in Salesforce, it will redirect to Detail Screen or list of records of its corresponding Object.

There are many cases where we want to override this functionality. For example, after a new Account created, we want user to create Tasks to it, so once the New Account is saved, page should redirect to New Task screen with Current Account ID details.

To accomplish this, we in general need to create a Custom New Form for Account and in that form, we override default save action and once default save action is accomplished, we redirect using PageReference

We also save one more easy way to accomplish this on default forms, adding details through URL parameters.

By default, Salesforce provided 3 URL parameters,  'retURL', 'saveURL', 'cancelURL'.

We can make use of these parameters to redirect the screen after default button actions.

lets understand with below simple example.

https://demo.salesforce.com/a08/e?retURL=%2Fa08%2Fo&saveURL=/apex/google&cancelURL=/apex/yahoo

The above URL is actually a new form for a custom object.

https://demo.salesforce.com/a08/e

But we also provided other parameters along with URL.

saveURL=/apex/google, this parameter is used for redirection after the record saved. So, once a record is saved, it redirected to a custom page 'google', also the newly created record Id is sent as a parameter by default.

https://demo.salesforce.com/apex/google?newid=a08o0000006rTSR

cancelURL=/apex/yahoo, this parameter is used for redirection if user clicked on default Cancel button . This redirects to a custom page 'yahoo' if user cancelled the form.