Here is my code :
// JavaScript source code
function setproblemtype(econtext) {
var subject;
var problemcodename;
if (Xrm.Page.data.entity.attributes.get("subjectid").getValue() != "" && Xrm.Page.data.entity.attributes.get("subjectid").getValue() != null)
{
subject = Xrm.Page.data.entity.attributes.get("subjectid").getValue()[0].name;
}
var Severity = Xrm.Page.data.entity.attributes.get("severitycode");
var ServiceType = Xrm.Page.data.entity.attributes.get("md_service");
var id = "";
if (Xrm.Page.data.entity.attributes.get("subjectid").getValue() != "" && Xrm.Page.data.entity.attributes.get("subjectid").getValue() != null)
{
var ClientUrl = Xrm.Page.context.getClientUrl();
var ODataPath = ClientUrl + "/XRMServices/2011/OrganizationData.svc";
var retrieveReq = new XMLHttpRequest();
var filter = "/md_problemcodeSet?" +
"$select=md_problemcodeId,md_name" + "&$filter=md_name eq '" + subject + "'";
retrieveReq.open("GET", ODataPath + filter, false);
//alert(ODataPath + filter);
retrieveReq.setRequestHeader("Accept", "application/json");
retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
retrieveReq.send(null);
var retrievedRecords = JSON.parse(retrieveReq.responseText).d;
if (retrievedRecords.results.length > 0) {
var f = retrievedRecords.results[0];
if (f.md_problemcodeId != null) {
id = f.md_problemcodeId;
}
if (f.md_name != null) {
problemcodename = f.md_name;
}
}
if (id != "") {
if (subject == "Extension of Payment") {
Severity.setValue(100000001);
ServiceType.setValue(100000000);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
}
else if ((subject == "Deferring of Payment") || (subject == "Refund") || (subject == "Waiver") || (subject == "Discount Request") || (subject == "Price Adjustment")) {
Severity.setValue(100000003);
ServiceType.setValue(100000000);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
}
else if (subject == "Plot Size Discrepancy") {
Severity.setValue(100000004);
ServiceType.setValue(100000001);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
}
else if (subject == "Floor Plan Request") {
Severity.setValue(100000000);
ServiceType.setValue(100000001);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
}
else if (subject == "Plot Plan Request") {
Severity.setValue(100000001);
ServiceType.setValue(100000001);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
}
else if (subject == "Name Rectification") {
Severity.setValue(100000002);
ServiceType.setValue(100000002);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
}
else if (subject == "Name Change") {
Severity.setValue(100000003);
ServiceType.setValue(100000002);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
}
else if (subject == "Customer Information Update") {
Severity.setValue(100000001);
ServiceType.setValue(100000002);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
}
else if (subject == "Reinstatement") {
Severity.setValue(100000005);
ServiceType.setValue(100000003);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
}
else if (subject == "Consolidation") {
Severity.setValue(100000005);
ServiceType.setValue(100000004);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
}
else if (subject == "Primary Market Transfer") {
Severity.setValue(100000001);
ServiceType.setValue(100000005);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
}
else if (subject == "Mortgage Discharger") {
Severity.setValue(100000001);
ServiceType.setValue(100000006);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
}
else if (subject == "Mortgage Registration") {
Severity.setValue(100000003);
ServiceType.setValue(100000006);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
}
else if (subject == "Upgrade") {
Severity.setValue(100000002);
ServiceType.setValue(100000007);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
}
else if (subject == "Downgrade") {
Severity.setValue(100000002);
ServiceType.setValue(100000007);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
}
else if (subject == "Merger") {
Severity.setValue(100000005);
ServiceType.setValue(100000007);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
}
else if (subject == "Swap") {
Severity.setValue(100000005);
ServiceType.setValue(100000007);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
//Xrm.Page.getAttribute("md_problemcodeid").setSubmitMode("always");
}
else if (subject == "Sale Cancellation") {
Severity.setValue(100000001);
ServiceType.setValue(100000008);
SetLookupValue("md_problemcodeid", id, problemcodename, "md_problemcode");
}
else {
Severity.setValue("");
ServiceType.setValue("");
Xrm.Page.getAttribute("md_problemcodeid").setValue(null);
}
}
else {
Severity.setValue("");
ServiceType.setValue("");
Xrm.Page.getAttribute("md_problemcodeid").setValue(null);
}
}
else {
Severity.setValue("");
ServiceType.setValue("");
Xrm.Page.getAttribute("md_problemcodeid").setValue(null);
}
var dirtyflag = "false";
var attributes = Xrm.Page.data.entity.attributes.get();
var formType = Xrm.Page.ui.getFormType();
if (formType != 1) {
for (var i in attributes) {
var attribute = attributes[i];
if (attribute.getIsDirty()) {
dirtyflag = "true";
//alert(attribute.getName());
Xrm.Page.getAttribute(attribute.getName()).setSubmitMode("always"); //force save flag
}
if (dirtyflag == "true") {
Xrm.Page.data.entity.save();
}
}
}
}
function SetLookupValue(fieldName, id, name, entityType) {
if (fieldName != null) {
var lookupValue = new Array();
lookupValue[0] = new Object();
lookupValue[0].id = id;
lookupValue[0].name = name;
lookupValue[0].entityType = entityType;
Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
}
}