+ Start a Discussion
Mike @ BlackTabMike @ BlackTab 

.setRedirect(true) changes browser location, but does not change url.

The following code sends the user back to a specific project, however the browser url doesn't change AND nothing on the page can be clicked. The URL is always: https://c.cs12.visual.force.com/apex/Add_Project_Roles

 

PageReference back = new PageReference('/' + projectId);
back.setRedirect(true);
return back;

 Is there a better way to redirect the user from an Apex Controller?

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Its simpler than that - as you have a rerender attribute that means only that section of the page will be changed once the method completes, the URL and the rest of the page won't be affected.  

All Answers

bob_buzzardbob_buzzard

That's certainly the way I redirect users.  Is this in an action attribute on a page tag? 

Mike @ BlackTabMike @ BlackTab

Yes, I have a method called save() that returns this page reference. 

 

Should I have an onComplete javascript method that redirects the user on my commandButton?

 

<apex:commandButton value="Save" action="{!save}" rerender="error" oncomplete="A JS FUNCTION"/>

 

bob_buzzardbob_buzzard

Its simpler than that - as you have a rerender attribute that means only that section of the page will be changed once the method completes, the URL and the rest of the page won't be affected.  

This was selected as the best answer
Mike @ BlackTabMike @ BlackTab

So basically get rid of the rerender attribute?

bob_buzzardbob_buzzard

Yes.  If you are trying to give the user an error message on failure of the method, I'd simply put a try catch round it and in the event of an error add a message to the page and then return null.  

 

If rerendering is important, the way I've handled this in the past is to set a property in the controller to indicate if an error occurred, and along with the error component rerender an outputpanel containing javascript that changes the browser location if there were no errors.

lakshmi ganapathirajulakshmi ganapathiraju
public with sharing class leadConvert {

    public Account acc { get; set; }
     public Lead  led{ get; set; }

public leadConvert(){

 led= new Lead();

}

public List<Account> acclist{get;set;}
  public void accounts()
     {
      Lead l=new Lead();
      l.LastName=led.LastName;
      l.Company=led.Company;
      l.Status=led.Status;
      insert l;
   
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(l.id);

          LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
Database.LeadConvertResult lcr = Database.convertLead(lc);
redirect();
      }
 public PageReference redirect(){
        System.debug('lak');
            PageReference pageRef = new PageReference('https://www.google.co.in/?gfe_rd=cr&ei=o96dVtK-BujI8AeDzoWYAw&gws_rd=ssl');
            // pageRef.setRedirect(true); 

            return pageRef;
     
 
       
     }     

}
not working