Datensätze mittels Javascript kopieren

Hier erhalten Sie den gesamten Code, den Sie zum Kopieren eines Datensatzes in Microsoft Dynamics CRM benötigen. Eine passende Schritt-für-Schritt Anleitung finden sie in diesem Blog-Artikel:

Anleitung: So kopieren Sie Ihre Datensätze in Microsoft Dynamics 365 CRM mittels JavaScript.

function copyAccount(){
//Die ID des Accounts auslesen
var idAcc = Xrm.Page.data.entity.getId();
var id_Account = idAcc.replace("{", "").replace("}", ""); //Die geschweifte Klammern lˆschen

//FetchXML
var fetchData = {
accountid: ""+id_Account+""
};
var fetchXml = [
"<fetch top='50'>",
" <entity name='account'>",
" <all-attributes />",
" <filter>",
" <condition attribute='accountid' operator='eq' value='", fetchData.accountid, "'/>",
" </filter>",
" </entity>",
"</fetch>",
].join("");


Xrm.WebApi.retrieveMultipleRecords("account", "?fetchXml= " + fetchXml).then(
function success(result) {
for (i = 0; i < result.entities.length; i++){
var data = result.entities[i];
data["transactioncurrencyid@odata.bind"] = "/transactioncurrencies("+data._transactioncurrencyid_value+")";

delete data["accountid"];
delete data["_createdby_value"];
delete data["_createdby_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
delete data["_createdby_value@OData.Community.Display.V1.FormattedValue"];
delete data["_modifiedby_value"];
delete data["_modifiedby_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
delete data["_modifiedby_value@OData.Community.Display.V1.FormattedValue"];
delete data["_ownerid_value"];
delete data["_ownerid_value@Microsoft.Dynamics.CRM.associatednavigationproperty"];
delete data["_ownerid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
delete data["_ownerid_value@OData.Community.Display.V1.FormattedValue"];
//usw ...

//Ein neuer Datensatz erstellen.
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/accounts", false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204) {
var uri = this.getResponseHeader("OData-EntityId");
var regExp = /\(([^)]+)\)/;
var matches = regExp.exec(uri);
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(data));
}},
function (error) {
console.log("Error: ", error);
return null;
});
}

Anleitung: So kopieren Sie Ihre Datensätze in Microsoft Dynamics 365 CRM mittels JavaScript.

Wie können wir Dir weiterhelfen?

Hast Du konkrete Fragen zu Microsoft Dynamics 365 Produkten, den Themen Digitalisierung, Kunden- und Leadmanagement? Dann zögere bitte nicht und kontaktiere uns gerne direkt. 

Andreas_Rosenbauer
Andreas Rosenbauer
Vertriebsleitung
Frauke_Mueller
Frauke Müller
Beratung CRM