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
Betty LiuBetty Liu 

custom clone button with selected fields on Lightning experience

Hi All,

I am new to salesforce lightning experience, I would like to create a custom clone button with selected fields only on opportunity, does anyone know to do that? Thanks. 
sfdcMonkey.comsfdcMonkey.com
hi Betty Liu 
can you please do more specific about your requirement in details
thanks 
Betty LiuBetty Liu
Hi Plyush,
As current clone button on Opportunity is clone all the fields. I only want certain fields to be clone not all the fields on opportunity, therefore I need to create a custom clone button to do that. 
There are some forum disscussed how to create that on salesforce classic, I followed and able to do what I want on salesforce classic, but it does not work on lightning experience. 
Thanks.
Nishith Animesh 19Nishith Animesh 19
Hi Betty,
Following codes should resolve your issue:

Create Lightning component:
<aura:component controller="CloneOpp" implements="force:lightningQuickActionWithoutHeader,lightning:actionOverride">
	<ltng:require styles="/resource/SLDS202/assets/styles/salesforce-lightning-design-system.css"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="oppName" type="String" />
    <aura:attribute name="typePick" type="String" />
    <aura:attribute name="campaign" type="String" />
  
     <aura:dependency resource="c:createRecord"/>
    <!-- Load the navigation events in the force namespace. -->
    <aura:dependency resource="markup://force:*" type="EVENT"/>
    
    
</aura:component>
Helper:
({
	 doInit: function(component, event, helper) {
      //call the helper function with pass [component, Controller field and Dependent Field] Api name 
     // alert('Hi');
      helper.queryOpp(component, event, helper);
   },
   
})
Com Controller:
({
    queryOpp: function(component,  event, helper) {
      var recordId = component.get("v.recordId");
        if(recordId!=undefined){
        
        
        alert(recordId);
        var action = component.get("c.getDetailsFromOpp");
        action.setParams({
            recordId: recordId
        });
        action.setCallback(this, function(response){
            var state = response.getState();
           
            if (state === "SUCCESS") {
                var opp = response.getReturnValue();
                component.set("v.oppName", opp.Name); 
                component.set("v.typePick", opp.pcikListTest__c);
                component.set("v.campaign", opp.CampaignId);
               
                helper.createOppRecord(component, event, helper);
              
            }
           
        });
        
        $A.enqueueAction(action);
        }
    },
	 createOppRecord : function (component, event, helper) {
       var name=  component.get("v.Name"); 
       var picklist = component.get("v.typePick"); 
        var campaign=  component.get("v.campaign");
        // alert(picklist);
       //  alert(campaign);
    var createOpportunityEvent = $A.get("e.force:createRecord");
	createOpportunityEvent.setParams({
    "entityApiName": "Opportunity",
    "defaultFieldValues": {
        'Name' : 'New Opp',
        'Type' : 'Existing Customer - Upgrade',
        'pcikListTest__c' : picklist,
        'CampaignId' : campaign
    	}
	});
	createOpportunityEvent.fire();
    },
    
})

Class:
public class CloneOpp{    
  
  @AuraEnabled  
    public static Opportunity getDetailsFromOpp(string recordId){
       Opportunity opp = [select Id,Name,CampaignId,TrackingNumber__c,pcikListTest__c
        from Opportunity Where Id = :recordId limit 1];
       return opp ;
    }
 }

Create Lightning component action:
When you will use this action pcikListTest__c and CampaignId will be prepopulated.
So I am using force:createRecord to create new opportunity record with prepolated values. You can add as many fields as you like.

 
Sushma Mishra 5Sushma Mishra 5
Hi Nishith Animesh 19,
Thanks , can we do the cloning for activity object .I want cloning of event and task how to do that.Please let me know.
Conner Buchanan 1Conner Buchanan 1
I've tried implementing the above code, and every time I click the Lightning component action button I get the following error: 

User-added image

Any ideas on what could be wrong?
QuicksilvrQuicksilvr
Hey Conner, seems like the controller and helper JS code blocks were wrongly titled in Nishith's original post. Swap them around and it should work.
Jon-Michael Murphey 2Jon-Michael Murphey 2
I implemented the code above with the Helper and Controller switched however when i click the quick action it opens a white popup and just stays there. Nothing is happening.(See below)
 User-added image