Although there are many ways to clone and some free solutions that will work here is one method:
Create javascript action for button and send record id to your custom action as shown here https://www.youtube.com/watch?v=rz4wKV3VCNQ
Then create a plugin and so new cloned record can be created
var oppguid = (string)context.InputParameters["OPGuid"];
Entity oppproduct = service.Retrieve("opportunityproduct", Guid.Parse(oppguid), new ColumnSet(true));
Guid opportunityID = ((EntityReference)oppproduct["opportunityid"]).Id;
// Call the method to set the attributes
Entity clone = oppproduct.Clone();
clone["eye_assetnumber"] = length 1;
// Create new record
Guid cloneId = service.Create(clone);
context.OutputParameters["OutputArg1"] = "You will be redirected to new Opportunity Product:" Environment.NewLine "Asset Number: " newlength Environment.NewLine cloneId.ToString();
}
}
public static class EntitiExtensions
{
public static T Clone(this T entity) where T : Entity
{
Entity clone = new Entity(entity.LogicalName);
foreach (KeyValuePair attr in entity.Attributes)
{
if (attr.Key.ToLower() == entity.LogicalName.ToLower() "id" || attr.Key.ToLower() == "activityid") continue;
clone[attr.Key] = attr.Value;
}
return clone.ToEntity();
}
}
}
You can then send user to the new record.
entityFormOptions["entityName"] = "opportunityproduct";
entityFormOptions["entityId"] = guidopp ;
Xrm.Navigation.openForm(entityFormOptions).then(
function(success) {
console.log(success);
},
function(error) {
console.log(error);
});