function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
suresh dupadasuresh dupada 

What is the use of PageReference ?..........

For Example

public Pagereference method1()
{
        return null;
}

why we use pagereference as a return type,............ what is the situation to use the pagereference,,,,......... Please explain me with code example..............
Best Answer chosen by suresh dupada
Sumitkumar_ShingaviSumitkumar_Shingavi
Use of PageReference: A PageReference is a reference to an instantiation of a page reference. Among other attributes, PageReferences consist of a URL and a set of query parameter names and values like custom parameters or standard parameters for PageReference created.

PageReference pageRef = new PageReference('/apex/myVfPage');
There are several standard parameters like setRedirect which can be used for hard "reset" of controller state. If you make it false then state of variables in controller is maitained across many VF pages.
pageRef.setRedirect(true);
If you want to add any custom parameters then you can add them like below
pageRef.getParameters().put('myId', accId); //Assume accId is a variable in class or method
So, overall code will look like
public PageReference redirectToMyVF(Id accId) {	
	PageReference myVFPage = new PageReference('/apex/myVFPage')
	myVFPage.setRedirect(true);
	myVFPage.getParameters().put('myId', accId);
	return myVFPage;
}

Quick Answers to your questions:
1. Why we use pagereference as a return type?
- We normally set return type to a PAgeReference when we need user to other VF page. We return "null" if we want user on same page and we normally return "null" in catch blocks.

2. What is the situation to use the pagereference?
- When you want to jump to other VF page when something is clicked and method takes you to new VF page or refreshing current page by doing "return null;"

3. Please explain me with code example
- Explained above!


PS: if this answers your question then hit Like and mark it as solution!

All Answers

Tejpal KumawatTejpal Kumawat
Hello Suresh,

PageReference is object in the salesforce, It is use to  navigate the user to a different page or Url as the result of an action method.

Example :

public PageReference returnPage() {
        // Send the user to the detail page for the new account.
        PageReference acctPage = new ApexPages.StandardController(account).view();
        acctPage.setRedirect(true);
        return acctPage;
    }
Sumitkumar_ShingaviSumitkumar_Shingavi
Use of PageReference: A PageReference is a reference to an instantiation of a page reference. Among other attributes, PageReferences consist of a URL and a set of query parameter names and values like custom parameters or standard parameters for PageReference created.

PageReference pageRef = new PageReference('/apex/myVfPage');
There are several standard parameters like setRedirect which can be used for hard "reset" of controller state. If you make it false then state of variables in controller is maitained across many VF pages.
pageRef.setRedirect(true);
If you want to add any custom parameters then you can add them like below
pageRef.getParameters().put('myId', accId); //Assume accId is a variable in class or method
So, overall code will look like
public PageReference redirectToMyVF(Id accId) {	
	PageReference myVFPage = new PageReference('/apex/myVFPage')
	myVFPage.setRedirect(true);
	myVFPage.getParameters().put('myId', accId);
	return myVFPage;
}

Quick Answers to your questions:
1. Why we use pagereference as a return type?
- We normally set return type to a PAgeReference when we need user to other VF page. We return "null" if we want user on same page and we normally return "null" in catch blocks.

2. What is the situation to use the pagereference?
- When you want to jump to other VF page when something is clicked and method takes you to new VF page or refreshing current page by doing "return null;"

3. Please explain me with code example
- Explained above!


PS: if this answers your question then hit Like and mark it as solution!
This was selected as the best answer
Ap30Ap30
Hello
My code below is not working.
public PageReference cancel()
  {
    PageReference pgref = new PageReference('/home/home.jsp');
    pgref.setRedirect(true);
        return pgref;
  }
when clicking cancel button it has to go to homepage in lightning.Instead I'm getting error.Kindly help