Bhubaneswar, Odisha, India
+91-8328865778
support@logyscal.com

Synchronous call from JavaScript to Dynamics 365/ Dataverse Web API using XML HTTP

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

You can enroll now !We are giving 30% discount on our Internship Program

Don’t miss the chance to participate in the upcoming Internship Program which will be done using Microsoft Dot Net Web Development Full Stack Technology. The new batch will be starting from May 20, 2024.  We will have most experienced trainers for you to successfully complete the internship with live project experience.

Why to choose Our Internship Program?

Industry-Relevant Projects
Tailored Assignments: We offer projects that align with your academic background and career aspirations.
Real-World Challenges: Tackle industry-specific problems and contribute to meaningful projects that make a difference.

Professional Mentorship
Guidance from Experts: Benefit from one-on-one mentorship from seasoned professionals in your field.
Career Development Workshops: Participate in workshops that focus on resume building, interview skills, and career planning.

Networking Opportunities
Connect with Industry Leaders: Build relationships with professionals and expand your professional network.
Peer Interaction: Collaborate with fellow interns and exchange ideas, fostering a supportive and collaborative environment.

Skill Enhancement
Hands-On Experience: Gain practical skills and learn new technologies through project-based learning.
Soft Skills Development: Enhance communication, teamwork, and problem-solving skills essential for career success.

Free Demo Class Available