[SalesForce] Standard Actions and Custom clone button

I need to override the clone functionality to pre-populate certain fields on custom object that is being cloned. So I have this URL button and passed in the record being cloned as an id in the url of VF page (/apex/myVFPage?obj.id={!Opp.Id}).
The VF page is used just for redirection. In the action attribute of VF page, a controller method is invoked, which populates desired
fields in the new record and then the newly created record page is displayed in edit mode.Everything works fine as expected in classic. However in LEX, the navigation is a little messed up.
On button click of custom clone, the record opens up in edit mode, you edit and save the record.
Shows a message that record was successfully cloned.
You close the success prompt.
The original record (source for cloning) reappears in edit mode.
You Cancel out of this and you can see the newly cloned record in list view.

I am looking for a way to prevent this original record from reappearing. What possible solutions can be possible?
Is using standard action going to help here? Any expert opinions, links to any blogs will be really appreciated.

Below is the code:
On Opportunity object I have custom Clone button which is a Detail Page Button and the url is:'/apex/OpportunityClone?opp.id={!Opportunity.Id}'

Page

<apex:page name="OpportunityClone" title="Clone Opportunity" lightningStyleSheets="true" id="oppPage" tabStyle="Opportunity" controller="OppCloneController" action="{!redirectToOppEdit}">
<apex:form id="oppCloneForm">
</apex:form>

Controller

public with sharing class OppCloneController {

public Opportunity srcOpp {get;set;}
public String oppId;

public OppCloneController(){

    oppId = ApexPages.currentPage().getParameters().get('opp.id');
    String queryString = 'Select ' + SObjectUtils.getFieldNamesString('Opportunity') + ' from Opportunity where Id = :oppId';
    srcOpp = Database.query(queryString);     
}

public PageReference redirectToOppEdit() {
    Opportunity clonedOpp = srcOpp.clone(false, true, false, false);
    clonedOpp.srcOfferId = srcOpp.Id;
    clonedOpp.Status = 'Open';
    clonedOpp.Date_Submitted__c = null;
    insert clonedOpp;
    return new PageReference('/' + clonedOpp.Id + '/e?cancelURL=%2F' + clonedOpp.Id + '&retURL=%2F' + clonedOpp.Id);
}

}

Best Answer

The issue, I suspect, is the URL hack in the PageReference, which was never support and likely less so under Lightning.

Creating a Lightning component is the best option IMO.

I found this example that I think is brilliant. NB: Not my website.

Clone Any SObject Record With Lightning Component

https://rajvakati.com/2018/10/17/clone-to-any-sobject-record-with-lightning-component/

Related Topic