You need to sign in to do that
Don't have an account?
Mike @ 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?
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
That's certainly the way I redirect users. Is this in an action attribute on a page tag?
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?
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.
So basically get rid of the rerender attribute?
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.
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