Ich muss ein Bild, das bereits in Web-Ressourcen (JPG) und nach dem Namen des Produkts in meiner Entität das Entityimage ist gefüllt.Set EntityImage mit JavaScript Dynamics CRM
Ich verwende das folgende Skript, aber es funktioniert nicht; Das Ergebnis, das zurückgegeben wird, ist leer und verursacht einen Fehler.
Das Ereignis wird bei Änderung des Felds Produktname ausgelöst und ruft die Funktion SetImage()
auf.
Irgendwelche Vorschläge, wie man das erreicht?
//Hook this method to the "OnChange" event of "creditlimit" field
function SetImage()
{
debugger;
var productId = Xrm.Page.data.entity.getId();
var productName = Xrm.Page.getAttribute("name").getValue();
if (productName != null)
{
//retrieve image onecoinpile.jpg and update product record "EntityImage" attribute
this.UpdateproductRecordWithNewImage(productId, "new_BurgerGift.jpg");
}
}
function UpdateproductRecordWithNewImage(productId, webResourceName)
{
debugger;
this.GetImageWebResource
(
productId,
webResourceName,
this.UpdateProductRecord
);
}
function GetImageWebResource(productId, imageName, successCallback)
{
debugger;
//OData URI to get address information from parent account record
var oDataURI = Xrm.Page.context.getClientUrl()
+ "/XRMServices/2011/OrganizationData.svc/"
+ "WebResourceSet"
+ "?$filter="
+ "Name eq '" + imageName + "'"
+ "&$select=Name,Content";
//Synchronous XMLHttpRequest to retrieve account record
var req = new XMLHttpRequest();
req.open("GET", encodeURI(oDataURI), false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function()
{
debugger;
if (this.readyState == 4 /* complete */)
{
req.onreadystatechange = null; //avoids memory leaks
if (this.status == 200)
{
//parse the response string as a JSON object into the successCallback method.
successCallback(productId, JSON.parse(this.responseText).d);
}
else
{
alert("error");
var errorMsg1 = "GetImageWebResource Error: cannot retrieve image with name = " + imageName + ".";
//display a non-blocking alert dialog
Xrm.Utility.alertDialog(errorMsg1, function() { });
}
}
};
req.send();
}
function UpdateProductRecord(recordId, webResource)
{
debugger;
try{
//var product = {EntityImage:""};
var product = webResource.results[0].Content; //byte[] content of the web resource
alert(product);
var jsonproduct= JSON.stringify(product);
alert(jsonproduct);
}catch(ex){
console.log(ex);
}
//OData URI
var oDataURI = Xrm.Page.context.getClientUrl()
+ "/XRMServices/2011/OrganizationData.svc/"
+ "productSet(guid'" + recordId + "')";
alert(oDataURI);
//Synchronous post
var req = new XMLHttpRequest();
req.open("POST", encodeURI(oDataURI), false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("X-HTTP-Method", "MERGE");
req.onreadystatechange = function()
{
debugger;
if (this.readyState == 4 /* complete */)
{
req.onreadystatechange = null;
if (this.status == 204 || this.status == 1223)
{
//reloads the product record
window.location.reload(false);
}
else
{
alert("error");
var errorMsg2 = "UpdateProductRecord Error: Cannot update product record with productId = " + recordId + ".";
//display a non-blocking alert dialog
Xrm.Utility.alertDialog(errorMsg2, function() { });
}
}
};
req.send(jsonproduct);
alert(jsonproduct);
}
Versuchen Sie, die JPG-Datei WebResource in die Produkt Entitätsdatensatz zu speichern? –
Ja, ich möchte das Bild, das in Web-Ressourcen gespeichert ist, in Produkt-Entität ändern – Faith
Sie haben das zu aktualisierende Attribut "entitymebild" nicht erwähnt? –