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
DEV SFDC 28DEV SFDC 28 

Refresh custom object's related list in Lightning from (classic) Visualforce page.

Salesforce experts,

I have following use case:
A custom object related to an Account.
The related list is added into Account's page layout.
From a custom button a Visualforce page is opened, which initializes the custom object, fills the values and saves it.
After clicking the confirm button on VF page, the user is redirected back to Account page.

Everything works as designed on SF Classic.
BUT on Lightning the related list is not updated, although the object is created successfully. First after clicking the related list or refreshing the page manuelly, new record is shown.

<apex:commandButton> contains action returning redirected PageReference is tried first:
VF page:
......
<apex:commandButton value="AddList" action={!addList}"/>
......
Controller:
......
public PageReference addList() {

  // Logic to create custom object. 
 
 PageReference pageRef = new PageReference('/' + ApexPages.currentPage().getParameters().get('id')); 
 pageRef.setRedirect(true);
 return  pageRef;
}
......

If commandButton action returns redirection (pageRef is set), JS in oncomplete is not triggered. None of the following works. Just for test purposes.
<apex:commandButton value="AddList" action={!addList}" oncomplete="alert('blabla');"/>
<apex:commandButton value="AddList" action={!addList}" oncomplete="window.opener.location.refresh();"/>
<apex:commandButton value="AddList" action={!addList}" oncomplete="sforce.one.navigateToSObject('objId');"/>

Aliasing controller method through <apex:actionFunction> then JS:
......
<script>
  function myComp() {
  	  addListAction();
  	  sforce.one.navigateToURL('objId');
	  //alert('blabla');
  }
</script>
<apex:actionFunction name="addListAction" action="{!addList}"/>
<apex:commandButton value="AddList" oncomplete="myComp();"/>
......
JS is triggered but no effect on Lightning.

This is already an old question but no answer could be found by searching through the community/internet.
I really hope someone already found a workaround (without using Lighting component or event) and can help me out here.
Tried suggestions from these resources and much more, none of them works.
https://www.forcetalks.com/salesforce-topic/navigate-to-url-is-not-refreshing-the-page-in-salesforce-lightning/
https://developer.salesforce.com/forums/?id=906F0000000MGSsIAO
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

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;

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
DEV SFDC 28DEV SFDC 28
Hello Khan,

Thank you very much for your quick response.
I need to use the VF page in both Lightning and Classic. But no problem, I know how to distinguish and handle them differently. Your comment explained it also pretty well. The only problem is get e.g. Account detail page or related list refreshed. 
Very interesting part from your suggestion is:
aura.redirect(pageRef);
I will try it and leave comment if it works.
Thank you again.


 
DEV SFDC 28DEV SFDC 28
Updated:
Based on @Khan's comment, aura.redirect() is called. But there is no statement shows how aura is initialized.
So some research. From Apex Developer Guide I could only find AuraEnabled Annotation, which exposes a method to Lightning Component: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_AuraEnabled.htm

Further search leads to following posts:
https://salesforce.stackexchange.com/questions/200888/what-is-aura-redirect
https://salesforce.stackexchange.com/questions/188191/is-using-the-aura-apex-class-supported
It seems like Doc is not available.
From GitHub there is info about Aura framework, unfortunately not related to SFDC Classic usage, but for e.g. Lightning Component.

Try in Org, using code like this won't cause problem by saving or compiling:
public PageReference addList() {
   
......
     
     PageReference pageRef = new PageReference('/' + ApexPages.currentPage().getParameters().get('id'));
     if(!getLightningEnabled()) {
     	system.debug('MYPLACEHOLDER CLASSIC: ' + pageRef.getUrl());
        pageRef.setRedirect(true);
     } else {
     	system.debug('MYPLACEHOLDER LIGHTNING: ' + pageRef.getUrl());
     	//aura.redirect(pageRef);
        System.Aura.redirect(pageRef);
     }

     return  pageRef;
  }
But in runtime the following error occurs:
User-added image
Therefor the question is still open.
Has anyone further information or hint about how Lightning record detail page can be refresh from Classic VF?
Thx.
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Actually, this 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 mark this as solved if the information was helpful.

Thanks and Regards,
Khan Anas
DEV SFDC 28DEV SFDC 28
Hi Khan, thank you again for the quick response.
But the reference id is probably not the cause for this error.

Retried after your reply: 
  • no change is made to the code
  • activated debugging
  • the log contains correct id and no error User-added image
  • but the error message kept shown
Probably aura.redirect() works only in Lightning Component context? It seems like the error is caused by: 
aura.redirect(pageRef); OR
System.Aura.redirect(pageRef); OR
system.Aura.redirect(pageRef);
Put any of these redirect() into previous "else" segment won't cause problem saving code to server. Only runtime.
Replace it with classic pageRef.setRedirect(true); no error page anymore, but the detail page is of corse not refreshed.
Samiullah saudagarSamiullah saudagar
Hi Guys,

I have a similar problem. Did you found any solution.

Regards,
Sami