Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 79901

Set Image Using JavaScript

$
0
0

Hi All,

I need to add an image according to the name of the product, I'm using the following Script but it's not working, the result that is returning is empty. the event is on change of product name calling the function SetImage

Any help please

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

Viewing all articles
Browse latest Browse all 79901

Trending Articles