Activity › Forums › Salesforce® Discussions › What is the difference between given two Salesforce Apex code in description?
-
What is the difference between given two Salesforce Apex code in description?
Posted by shariq on August 17, 2017 at 9:01 AMApex Code 1:-
public PageReference save()
{
PageReference pr = new PageReference(‘/apex/AnyApexPage’);
pr.setRedirect(true);
return pr;
}
Apex Code 2:-
public PageReference save()
{
return Page.AnyApexPage;
}
Parul replied 7 years, 8 months ago 3 Members · 2 Replies -
2 Replies
-
Hi Shariq,
There are different ways you can refer to or instantiate a PageReference in one of the following ways:
- Page.existingPageName
Refers to a PageReference for a Visualforce page that has already been saved in your organization. By referring to a page in this way, the platform recognizes that this controller or controller extension is dependent on the existence of the specified page and will prevent the page from being deleted while the controller or extension exists. - PageReference pageRef = new PageReference(‘***partialURL***’);
Creates a PageReference to any page that is hosted on the Lightning platform. For example, setting ‘partialURL’ to ‘/apex/Hello’ refers to the Visualforce page located at mySalesforceInstance/apex/Hello. Likewise, setting ‘partialURL’ to ‘/’ + ‘recordID’ refers to the detail page for the specified record.
NOTE: This syntax is less preferable for referencing other Visualforce pages than Page.existingPageName because the PageReference is constructed at runtime, rather than referenced at compile time. Runtime references are not available to the referential integrity system. Consequently, the platform doesn’t recognize that this controller or controller extension is dependent on the existence of the specified page and won’t issue an error message to prevent user deletion of the page. - PageReference pageRef = new PageReference(‘***fullURL***’);
Creates a PageReference for an external URL - You can also instantiate a PageReference object for the current page with the currentPage ApexPages method. For example: PageReference pageRef = ApexPages.currentPage();
Hope this helps you.
- Page.existingPageName
- [adinserter block='9']
-
Apex code1-
Creates a PageReference to any page that is hosted on the Lightning platform. For example, setting ‘partialURL’ to ‘/apex/Hello’ refers to the Visualforce page located at mySalesforceInstance/apex/Hello. Likewise, setting ‘partialURL’ to ‘/’ + ‘recordID’ refers to the detail page for the specified record.
Apex code2-
By referring to a page in this way, the platform recognizes that this controller or controller extension is dependent on the existence of the specified page and will prevent the page from being deleted while the controller or extension exists.
Thanks
Log In to reply.