Hi,
I'm facing some trouble in my JS which is I not able to set value into SLA lookup filed from the case form when I trigger js by using Assign button.
The Javascript is able to retrieve the value correctly, but it failed to update without any error message or stopper.
Beside this, it's working fine when I manually assign by using owner field.
Please refer to the image and the full Javascript below:
(Please search "// Issue code below:" from the script)
function SLAset(context) { debugger; alert ("start"); var Formtype = Xrm.Page.ui.getFormType(); if (Formtype == 1) { var id = Xrm.Page.context.getUserId(); var ticketnumber = Xrm.Page.getAttribute('ticketnumber').getValue(); if (ticketnumber != null){ SLADefaut(id); } } else if (Formtype = 2) { var Owner = Xrm.Page.data.entity.attributes.get("ownerid"); var id = Owner.getValue()[0].id; var entityName = Owner.getValue()[0].entityType; var OwnerName = Owner.getValue()[0].name; alert (OwnerName); if (entityName == "systemuser") { SLADefaut(id); } } } function SLADefaut(id) { debugger; var DefaultTeam; var odataSelect = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/SystemUserSet?$select=etq_defaultteam&$filter=SystemUserId eq guid'" + id + "'"; //Write odata query here var isCMU = Xrm.Page.getAttribute("etq_cmuspecific").getValue(); var caseorigincode = Xrm.Page.getAttribute('caseorigincode').getSelectedOption(); var classification = Xrm.Page.getAttribute('etq_classification').getSelectedOption(); var priority = Xrm.Page.getAttribute('prioritycode').getSelectedOption(); var category = Xrm.Page.getAttribute('etq_category').getSelectedOption(); var ticketnumber = Xrm.Page.getAttribute('ticketnumber').getValue(); $.ajax({ type: "GET", async: false, contentType: "application/json; charset=utf-8", datatype: "json", url: odataSelect, beforeSend: function(XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function(data, textStatus, XmlHttpRequest) { DefaultTeam = data.d.results[0].etq_defaultteam; var id = DefaultTeam.Id; var name = DefaultTeam.Name; debugger; if (name == retrieveSetting()) { alert ("isTeam"); debugger; oDataSelect = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/etq_slamappingSet?$filter=etq_category/Value eq " + category.value + " and etq_team/Id eq guid'" + id + "'"; $.ajax({ async: false, type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: oDataSelect, beforeSend: function(XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function(data, status) { var results = data.d.results; if (results != null) { if (results.length == 1) { var SLA = new Array(); SLA[0] = new Object(); SLA[0].entityType = "SLA"; SLA[0].name = results[0].SLAId.Name; SLA[0].id = results[0].SLAId.Id Xrm.Page.getAttribute('slaid').setValue(SLA); } else if (results.length > 1) { var type = Xrm.Page.getAttribute('etq_type').getValue()[0].id; for (i = 0; i < results.length; i++) { slaid = results[i].SLAId.Id; oDataSelect = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/etq_typeSet?$filter=etq_slamapping/Id eq (guid' " + slaid + "') and etq_typeId eq (guid'" + type + "')"; $.ajax({ async: false, type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: oDataSelect, beforeSend: function(XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function(data, status) { var results2 = data.d.results; if (results2 != null) { var SLA = new Array(); SLA[0] = new Object(); SLA[0].entityType = "SLA"; SLA[0].name = results[i].SLAId.Name; SLA[0].id = results[i].SLAId.Id Xrm.Page.getAttribute('slaid').setValue(SLA); } }, error: function(XmlHttpRequest, textStatus, errorThrown) { // alert(errorThrown); } }); } } } }, error: function(XmlHttpRequest, textStatus, errorThrown) { // alert(errorThrown); } }); } else if (isCMU == true) { alert ("isCMU"); var pagecontext = Xrm.Page.context; var serverUrl = pagecontext.getClientUrl(); var oDataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc/"; var oDataSelect = ""; debugger; oDataSelect = oDataPath + "etq_slamappingSet?$filter=etq_priority/Value eq " + priority.value + " and etq_cmuspecific eq " + isCMU; $.ajax({ async: false, type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: oDataSelect, beforeSend: function(XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function(data, status) { var results = data.d.results; if (results != null) { if (results.length == 1) { var SLA = new Array(); SLA[0] = new Object(); SLA[0].entityType = "SLA"; SLA[0].name = results[0].SLAId.Name; SLA[0].id = results[0].SLAId.Id Xrm.Page.getAttribute('slaid').setValue(SLA); } else if (results.length > 1) { var type = Xrm.Page.getAttribute('etq_type').getValue()[0].id; for (i = 0; i < results.length; i++) { slaid = results[i].SLAId.Id; oDataSelect = oDataPath + "etq_typeSet?$filter=etq_slamapping/Id eq (guid' " + slaid + "') and etq_typeId eq (guid'" + type + "')"; $.ajax({ async: false, type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: oDataSelect, beforeSend: function(XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function(data, status) { var results2 = data.d.results; if (results2 != null) { var SLA = new Array(); SLA[0] = new Object(); SLA[0].entityType = "SLA"; SLA[0].name = results[i].SLAId.Name; SLA[0].id = results[i].SLAId.Id Xrm.Page.getAttribute('slaid').setValue(SLA); } }, error: function(XmlHttpRequest, textStatus, errorThrown) { // alert(errorThrown); } }); } } } }, error: function(XmlHttpRequest, textStatus, errorThrown) { // alert(errorThrown); } }); } else if (isCMU == false || ticketnumber != null) { alert ("isnotCMU"); var pagecontext = Xrm.Page.context; var serverUrl = pagecontext.getClientUrl(); var oDataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc/"; var oDataSelect = ""; debugger; oDataSelect = oDataPath + "etq_slamappingSet?$filter=etq_classification/Value eq " + classification.value + " and etq_cmuspecific eq " + isCMU; $.ajax({ async: false, type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: oDataSelect, beforeSend: function(XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function(data, status) { var results = data.d.results; if (results != null) { if (results.length == 1) { debugger; // Issue code below: var SLA = new Array(); SLA[0] = new Object(); SLA[0].entityType = "SLA"; SLA[0].name = results[0].SLAId.Name; alert (results[0].SLAId.Name); SLA[0].id = results[0].SLAId.Id alert (results[0].SLAId.Id); SLA[0].type = "lookup"; Xrm.Page.getAttribute("slaid").setValue(SLA); // Issue code ablove: } else if (results.length > 1) { var type = Xrm.Page.getAttribute('etq_type').getValue()[0].id; for (i = 0; i < results.length; i++) { slaid = results[i].SLAId.Id; oDataSelect = oDataPath + "etq_typeSet?$filter=etq_slamapping/Id eq (guid' " + slaid + "') and etq_typeId eq (guid'" + type + "')"; $.ajax({ async: false, type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: oDataSelect, beforeSend: function(XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function(data, status) { var results2 = data.d.results; if (results2 != null) { debugger; var SLA = new Array(); SLA[0] = new Object(); SLA[0].entityType = "SLA"; SLA[0].name = results[i].SLAId.Name; SLA[0].id = results[i].SLAId.Id Xrm.Page.getAttribute('slaid').setValue(SLA); } }, error: function(XmlHttpRequest, textStatus, errorThrown) { // alert(errorThrown); } }); } } } }, error: function(XmlHttpRequest, textStatus, errorThrown) { // alert(errorThrown); } }); } else { var SLA = new Array(); SLA[0] = new Object(); SLA[0].entityType = "SLA"; SLA[0].name = results[i].SLAId.Name; SLA[0].id = results[i].SLAId.Id Xrm.Page.getAttribute('slaid').setValue(null); } }, error: function(XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + textStatus + errorThrown + odataSelect); } }); } function retrieveSetting() { debugger; var result = ""; var serverUrl = Xrm.Page.context.getClientUrl(); var Odata = serverUrl + "/XRMServices/2011/OrganizationData.svc/etq_systemsettingSet?$select=etq_Value&$filter=etq_name eq 'Team'"; $.ajax({ async: false, type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: Odata, beforeSend: function(XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function(data, status) { var results = data.d.results; if (results != null) { if (results.length >= 0) { if (results[0].etq_Value != null) { result = results[0].etq_Value; } } } }, error: function(XmlHttpRequest, textStatus, errorThrown) { // alert(errorThrown); } }); return result; }
I will be so appreciated if anyone can help with it!
Thank You
Kenny Chan