In this blog, I am going to show how to clone the record using lightning component quick action. The same functionality can be implemented by using even quick actions without having component also. But this component will be used to clone any object single record.
({
doInit : function(component, event, helper) {
var action = component.get("c.cloneAnySobjet");
action.setParams({"recordId": component.get("v.recordId")});
action.setCallback(this, function(response) {
var state = response.getState();
if(state === "SUCCESS") {
var sObjectEvent = $A.get("e.force:navigateToSObject");
sObjectEvent.setParams({
"recordId": response.getReturnValue(),
"slideDevName": "detail"
});
sObjectEvent.fire();
}else if (state === "ERROR"){
var errors = response.getError();
if(errors) {
cmp.set("v.errorMsg", errors[0].message);
var errorMsg = cmp.find('errorMsg');
$A.util.removeClass(errorMsg, 'slds-hide');
var field = cmp.find('field');
$A.util.addClass(field, 'slds-hide');
}
}
});
$A.enqueueAction(action);
},
})
Step 2: Create a Quick Action
now create a new quick action as shown below
Step 3: Add the Action to the page layout
Now add the quick action to the page layout Salesforce Mobile and Lightning Experience Actions section. After adding the quick action, you can able to see them on the page layout.
Testing
Now when you click on the Custom clone button it is going to clone the record.
In this blog, I am going to explain a simple quick action lightning component that will clone the records and its related data also. In this example, I am controlling which all objects are allowed to as part of the cloning is through the custom metadata data types.
These are the records for metadata
Apex Class
public class SuperClone {
private static SuperCloneService service = new SuperCloneService();
@AuraEnabled
public static Id doClone(Id parentId) {
Id clonedId = service.doClone(parentId);
return clonedId;
}
}