Conceptually the operation is easy and is based on the retrieving of all the record information using the Organization web service, then build an object identical to the first and use the web service to create a new record with this object.
Few things are important to take in mind:
1. In the building of the object the record ID must be "skipped" because, obviously, we can't create a record with an existing Guid
2. Date fields are returned in a format like: "Date("date_string") that it is not accepted in record creation from the Organization web service: depending of the need of every field, we decided to "skip it" or "reformat it". In the sample code we show both of these cases.
3. StateCode and StatusCode should be skipped (or managed) because, natively, Organization web service doesn't allow the creation of a record in a closed state.
function cloneRecord(serverUrl, originRecordType,
originRecordGuid) {
var cloneData = {};
var activityId;
var
serverUrl = document.location.protocol + "//"
+ document.location.host
+ "/" + Xrm.Page.context.getOrgUniqueName();
var oDataUri = serverUrl + "/xrmservices/2011/OrganizationData.svc/"
+
originRecordType + "Set?$select=*&$filter=<EntityId>
eq guid'"
+ originRecordGuid + "'";
+ originRecordGuid + "'";
jQuery.support.cors = true;
jQuery.ajax({
type:
"GET",
contentType: "application/json;
charset=utf-8",
datatype: "json",
url:
oDataUri,
async: false, //Synchronous operation
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results
will
be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept",
"application/json");
},
success: function (data, textStatus,
XmlHttpRequest) {
if (data && data.d && data.d.results)
{
cloneData = data.d.results[0];
//Here insert the code to skip/transform
fields such as
Record Id, Date fields, etc..
Record Id, Date fields, etc..
replacer = function (key, value) {
if (key == "ModifiedOn"
|| key == originRecordType + "Id" ||
key == "CreatedOn" || key == "StateCode" ||
key == "StatusCode") {
return undefined;
} else
if (key == "ActualStart"
|| key == "ActualEnd") {
if (value) {
var date =
eval('new ' +
value.replace("/", "").replace("/", ""));
value.replace("/", "").replace("/", ""));
return date.format('yyyy-MM-dd');
//Format the date
}
}
else return
value;
}
//Create new Activity
var oDataUri = serverUrl +
"/xrmservices/2011/OrganizationData.svc/"
+
originRecordType
+ "Set";
jQuery.support.cors = true;
jQuery.ajax({
type: "POST",
contentType: "application/json;
charset=utf-8",
datatype: "json",
url: oDataUri,
async: false, //Synchronous operation
data: JSON.stringify(cloneData, replacer),
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be
returned as
JSON.
XMLHttpRequest.setRequestHeader("Accept",
"application/json");
},
success: function (data, textStatus,
XmlHttpRequest) {
if (data
&& data.d) {
activityId =
data.d.ActivityId;
}
},
error: function (XmlHttpRequest,
textStatus, errorThrown) {
alert("Error : has occured
during creation of
the activity " + originRecordType.text + ": " +
the activity " + originRecordType.text + ": " +
XmlHttpRequest.responseText);
}
});
}
else {
//No data returned
}
},
error: function (XmlHttpRequest,
textStatus, errorThrown) {
alert("Error : has occured during retrieving of the activity
"
+ originRecordType.text + ": "
+
XmlHttpRequest.responseText);
}
});
return activityId;
}
Hope it can be useful
Happy CRM coding