locked
crm2013 entityimage issue RRS feed

  • Question

  •  Hi,

    I found an error when i change entity image on status change event I got this.status ==400 or this.statustext=="bad request" How can i resolve this error. Here is my script

    //Hook this method to the "OnChange" event of "new_status" field
    function OnAttributeChange_Status() {
     
        var dealerId = Xrm.Page.data.entity.getId();
        var statusValue = Xrm.Page.getAttribute("new_propertystatusdetail").getValue();
     alert(statusValue);
        if (statusValue == 1)
        {
             //retrieve image new_image1.jpg and update dealer record "EntityImage" attribute
            this.UpdateDealerRecordWithNewImage(dealerId, "new_image1.jpg");
     
        }
        else if (statusValue == 2)
        {
             //retrieve image new_image2.jpg and update dealer record "EntityImage" attribute
            this.UpdateDealerRecordWithNewImage(dealerId, "new_image2.jpg");
     
        }
    }

    function UpdateDealerRecordWithNewImage(dealerId, webResourceName){
     
        this.GetImageWebResource(
            dealerId,
            webResourceName,
            this.UpdateDealerRecord
        );
     
    }

    function GetImageWebResource(dealerId, imageName, successCallback) {
     
        //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(dealerId, JSON.parse(this.responseText).d);
                }
                else {
                    var errorMsg1 = "GetImageWebResource Error: cannot retrieve image with name = " + imageName + ".";
                    //display a non-blocking alert dialog
                    Xrm.Utility.alertDialog(errorMsg1, function () { });
                }
            }
        };
        req.send();
    }

    function UpdateDealerRecord(recordId, webResource) {
     
        var new_dealer = {};
        new_dealer.EntityImage = webResource.results[0].Content; //byte[] content of the web resource
     
        var jsonDealer = JSON.stringify(new_dealer);
     
        //OData URI
        var oDataURI = Xrm.Page.context.getClientUrl()
            + "/XRMServices/2011/OrganizationData.svc/"
            + "new_dealerSet(guid'" + recordId + "')";
     
        //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 dealer record
                    window.location.reload(false);
                }
                else {
                    var errorMsg2 = "UpdateDealerRecord Error: Cannot update dealer record with dealerId = " + recordId + ".";
                    //display a non-blocking alert dialog
                    Xrm.Utility.alertDialog(errorMsg2, function () { });
                }
            }
        };
        req.send(jsonDealer);
    }

    Monday, March 17, 2014 5:53 AM