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
Anupama JanteAnupama Jante 

pageReference.setRedirect() is not working in lightning

Hi Folks,

I have a opportunity quick action to open VF page in lightning. The controller is updating the record but the update is not reflecting on the record detail page i.e. the detail is not getting refreshed through below.

PageReference pg = new PageReference('/'+Opp.Id);
pg.setRedirect(true);
return pg;

The above code refreshes the detail page in classic but not in lightning.

I have also tried with  adding the oncomplete attribute to button i.e. oncomplete="window.opener.location.refresh();" but no luck
Please help me to achieve detail page refresh through VF/apex in lighting.

Thanks,
Anupama

 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Anupama,

Greetings to you!

I tried to research your problem and found some solution which might help you. 

If you want to use your page in lightning only then you can use:
//Some Logic
INSERT acc;

PageReference pageRef = new PageReference('/' + acc.id);
aura.redirect(pageRef);
return pageRef;

However, if you want to use your page in both classic and lightning then you can use getUiThemeDisplayed ()
 
String uiThemeDisplayed = UserInfo.getUiThemeDisplayed();

These are the values:
Theme1—Obsolete Salesforce theme
Theme2—Salesforce Classic 2005 user interface theme
Theme3—Salesforce Classic 2010 user interface theme
Theme4d—Modern “Lightning Experience” Salesforce theme
Theme4t—Salesforce mobile app theme
Theme4u—Lightning Console theme
PortalDefault—Salesforce Customer Portal theme
Webstore—Salesforce AppExchange theme

So, you have to use if condition like this:
 
//Some Logic
INSERT acc;

String uiThemeDisplayed = UserInfo.getUiThemeDisplayed();
PageReference pageRef = new PageReference('/' + acc.Id);

if(uiThemeDisplayed=='Theme4d'){  // Lightning Theme Value          
       aura.redirect(pageRef);
}

if(uiThemeDisplayed=='Theme2' || uiThemeDisplayed=='Theme3'){  // Classic Theme Value
       pageRef.setRedirect(true);
}
return pageRef;

Also, there is a known issue: Visualforce Page reference id not working on Lightning.

Please refer to the below link for more information.
https://success.salesforce.com/issues_view?id=a1p3A0000001BrUQAU&title=visualforce-page-reference-id-not-working-on-lightning

And there is no workaround available. But it is in a review and we may get a solution for this soon.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Anupama JanteAnupama Jante
Thanks Khan Anas for reply but unfortunately its not working.
I have also tried calling js function with below code onComplete action and still  no luck.
- sforce.one.navigateToURL('one/one.app#/sObject/'+oppId); 
- window.open(window.location.origin+'/'+'/one/one.app#/sObject/'+oppId+'/view','_parent');
However,  redirect is happening but the updated oppty record is not showing up. Its just displaying the older version of the record .

Thanks,
Anupama