简体   繁体   English

使用JavaScript Dynamics CRM设置EntityImage

[英]Set EntityImage Using JavaScript Dynamics CRM

I need to add an image that is already in web resources (jpg) and according to the name of the product in my entity the entityimage is filled. 我需要添加一个已经存在于Web资源(jpg)中的图像,并根据我的实体中产品的名称填充实体图像。

I'm using the following Script but it's not working; 我正在使用以下脚本,但无法正常工作; the result that is returning is empty, causing an error. 返回的结果为空,从而导致错误。

The event is triggered on change of the product name field and is calling the function SetImage() . 该事件在产品名称字段更改时触发,并正在调用函数SetImage()

Any suggestion on how to achieve this? 关于如何实现这一目标的任何建议?

 //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); } 

Your code looks fine, it should be working when you change the below lines. 您的代码看起来不错,当您更改以下行时,它应该可以正常工作。

var product = {};
product.EntityImage = webResource.results[0].Content; 

You mentioned, result is empty. 您提到,结果为空。 Can you verify by placing small case new_burgergift.jpg instead of new_BurgerGift.jpg . 您能放置小写的new_burgergift.jpg而不是new_burgergift.jpg来进行new_BurgerGift.jpg

Always check before accessing child elements like: 访问子元素之前,请务必进行检查,例如:

if(webResource.results[0])
    product.EntityImage = webResource.results[0].Content;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM