[SalesForce] Pagereference not working in Lightning

I have a Detail page button (VF page) and controller for it.

I used Pagereferenc method to take user to the record details page:

However, it works fine in the Classic, but not in Lightning.

    String url = '/'+engCloneCopy.id+'/e?retURL=%2F'+engCloneCopy.id;        
    return new PageReference(url);  

In Lightning, it will open up the edit page correctly, however once user saves the record, then page is blank and record gets updated in the background.

Expectation is that, once user save the record it should go back to the record detail page as it works in classic.

enter image description here
Any thoughts?

Best Answer

You can try sforce.one for to achieve your ask. sforce.one.editRecord(​recordId) Opens the page to edit the record specified by recordId. It works very well in the Lightning and mobile app. You need to handle redirection separately for classic.

A sample working example is:-

<apex:page standardController="Account" lightningStylesheets="true">
    <script>
    function  openeditpage(){
        sforce.one.editRecord('001B000000sOeCCIA0');
    }
    </script>
    <apex:form>
        <apex:commandButton value="BacK" oncomplete="openeditpage()" rendered="{!$User.UITheme == 'Theme4d' || $User.UITheme == 'Theme4t'}" />
    </apex:form>
</apex:page>

The above codes check whether user in Lightning or mobile app and once he clicks on it, it will redirect him to the edit page after completion of action you have mentioned in command button.

The edit record popup will redirect you to detail page once you hit the save button.

enter image description here