2017-11-10 16 views
0

すでにWebリソース(jpg) にある画像を追加する必要があり、エンティティの製品名に基づいてエンティティイメージが埋め込まれます。JavaScript Dynamics CRMを使用してEntityImageを設定する

次のスクリプトを使用していますが、動作していません。戻り値は空であり、エラーが発生します。

イベントは製品名フィールドの変更時にトリガーされ、関数SetImage()を呼び出しています。

これを達成するための提案はありますか?

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

+0

あなたは、製品のエンティティレコードにJPGファイルのリソースを保存しようとしていますか? –

+0

はい、Webリソースに格納されているイメージを製品エンティティに変更したい – Faith

+0

"entityimage"属性を更新する必要はありませんか? –

答えて

0

あなたのコードは正常に見えるあなたは、以下の行を変更した場合、それが動作するはず。

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

あなたは、結果は空であると述べました。 new_BurgerGift.jpgの代わりに小文字のnew_burgergift.jpgを入力して確認できますか?

は必ずのように子要素にアクセスする前に確認してください。

if(webResource.results[0]) 
    product.EntityImage = webResource.results[0].Content; 
関連する問題