Asked by:
script debugged error

Question
-
Hi,
Please tell me what did I miss in my script?
here is error image
Saturday, March 8, 2014 5:23 AM
All replies
-
Firstly check your are in which mode of crm,
means if you are in create mode id will be null because record is still not created .
second in IE debugger check Xrm namespace firstly by add watch
Hope this helps. ----------------------------------------------------------------------- Santosh Bhagat If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful"
- Proposed as answer by Mr. Santosh Bhagat Saturday, March 8, 2014 8:38 AM
Saturday, March 8, 2014 8:38 AM -
Hi Santosh,
I am in update mode still same problem occurred
Sunday, March 9, 2014 11:01 AM -
Hi Santosh,
i wanted to change entity image according to status field if status value is "1", "new_image1.jpg" will display and if status value is "2" then "new_image2.jpg" will display
i have an entity name "new_dealer" with optionset field name "new_status"
i have added two images as web resource "new_image1.jpg" , "new_image2.jpg"These are steps which i followed according to your instructions
first of all i added a web resource as javascript file name "new_setImageMethods"
//Hook this method to the "OnChange" event of "new_status" field
OnAttributeChange_Status: function () {
var dealerId = Xrm.Page.data.entity.getId();
var statusValue = Xrm.Page.getAttribute("new_status").getValue();
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");
}
}
UpdateDealerRecordWithNewImage: function(dealerId, webResourceName){
this.GetImageWebResource(
dealerId,
webResourceName,
this.UpdateDealerRecord
);
}
GetImageWebResource: function (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();
}
UpdateDealerRecord: function (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_dealertSet(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);
}
then i added the web resource file in form library and then double click on "new_status" field and set method "OnAttributeChange_Status" on change eventSunday, March 9, 2014 11:13 AM