Synchronous call from JavaScript to Dynamics 365/ Dataverse Web API using XML HTTP
If you want to call Dataverse/ Dynamics 365 Web API using synchronous calls with XML HTTP object with JavaScript then use the below code and update your code accordingly.
The below code snippet checks if a matching enrollment record exist for the same student and course then alerts message and restricts save operation. This function is called on form save and passed the execution context from FORM to method. This is sync call. You can also make it async call using the flag I mentioned.
function validateEnrollment(ec) { var formContext = ec.getFormContext(); var studentid = formContext.getAttribute("modern_student").getValue()[0].id; studentid = studentid.replace("{",""); studentid = studentid.replace("}",""); var courseid = formContext.getAttribute("modern_course").getValue()[0].id; courseid = courseid.replace("{",""); courseid = courseid.replace("}",""); var fetchXml = "<fetch version='1.0' mapping='logical' no-lock='false' distinct='true'><entity name='modern_enrollment'><attribute name='modern_enrollmentid'/><attribute name='modern_name'/><attribute name='createdon'/><order attribute='modern_name' descending='false'/><filter type='and'><condition attribute='modern_student' operator='eq' value='stid' uitype='modern_student'/><condition attribute='modern_course' operator='eq' value='csid' uitype='modern_course'/></filter></entity></fetch>"; fetchXml = fetchXml.replace("stid",studentid); fetchXml = fetchXml.replace("csid",courseid); var encodedFetchXML = encodeURI(fetchXml); var query = "/api/data/v9.2/modern_enrollments?fetchXml="+encodedFetchXML; var globalContext = Xrm.Utility.getGlobalContext(); var finalpathwithquery = globalContext.getClientUrl() + query; var data = null; var isAsync = false; var req = null; if (window.XMLHttpRequest) { req = new XMLHttpRequest(); } else if (window.ActiveXObject) { req = new ActiveXObject("MSXML2.XMLHTTP.3.0"); } req.open("GET",finalpathwithquery,isAsync); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var result = JSON.parse(this.response); data = result; } else { Xrm.Utility.alertDialog(this.statusText); } } }; req.send(); if(data.value.length >0) { Xrm.Utility.alertDialog("There is already an enrollment exist for the same student with same course."); var saveEvent = ec.getEventArgs(); saveEvent.preventDefault(); } }
Hope this helps.
Follow my blog for more trending topics on Dynamics 365, Azure, C#, Power Portals and Power Platform. For training, Courses and consulting, call to us at +91 832 886 5778 I am working more for community to share skills in Dynamics 365 and Power Platform. Please support me by subscribing my YouTube Channel. My YouTube Channel link is this : https://www.youtube.com/user/sppmaestro