Hi,
i want to change entity image according to status field if status value is "1", image1 name "new_image1.jpg" will display and if status value is "2" then image2 name "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
function OnAttributeChange_Status() {
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");
}
}
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_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);
}
I call this function "OnAttributeChange_Status" onchange event of status field but still i got an error
here is error:
Microsoft Dynamics CRM Error Report Contents
<CrmScriptErrorReport>
<ReportVersion>1.0</ReportVersion>
<ScriptErrorDetails>
<Message>Unable to get property 'Content' of undefined or null reference</Message>
<Line>67</Line>
<URL>/%7B635300259790000809%7D/WebResources/new_updateimage</URL>
<PageURL>/main.aspx?etc=10010&extraqs=%3f_gridType%3d10010%26etc%3d10010%26id%3d%257bCC7A88A6-14A8-E311-940F-002564C04E9E%257d%26rskey%3d422667900&pagemode=iframe&pagetype=entityrecord&rskey=422667900</PageURL>
<Function>UpdateDealerRecord(recordId,webResource)</Function>
<CallStack>
<Function>UpdateDealerRecord(recordId,webResource)</Function>
</CallStack>
</ScriptErrorDetails>
<ClientInformation>
<BrowserUserAgent>Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729)</BrowserUserAgent>
<BrowserLanguage>en-US</BrowserLanguage>
<SystemLanguage>en-US</SystemLanguage>
<UserLanguage>en-US</UserLanguage>
<ScreenResolution>1280x800</ScreenResolution>
<ClientName>Web</ClientName>
<ClientTime>2014-03-09T22:29:24</ClientTime>
</ClientInformation>
<ServerInformation>
<OrgLanguage>1033</OrgLanguage>
<OrgCulture>1033</OrgCulture>
<UserLanguage>1033</UserLanguage>
<UserCulture>1033</UserCulture>
<OrgID>{9271BA52-3892-E311-93F4-002564C04E9E}</OrgID>
<UserID>{9D4AB36C-3892-E311-93F4-002564C04E9E}</UserID>
<CRMVersion>6.0.0.809</CRMVersion>
</ServerInformation>
</CrmScriptErrorReport>
Please tell me what should I do for resolve this issue?