Examples of using await in Dynamics 365 JavaScript

Below are just some examples of using await inside of JavaScript for Dynamics 365. I know that there are many ways of achieving this but these are just some examples which I have used in the past which work very well. One thing you need to make sure is that you place the await clause inside of an async function otherwise the JavaScript will not work correctly.


Retrieving a single record

Below is an example of retrieving the Field Service Status field of the Booking Status

var fsstatus = await CRM.BookableResourceBooking.GetFieldServiceStatus(bookingStatus[0].id.replace("{", "").replace("}", ""));
CRM.BookableResourceBooking.GetFieldServiceStatus = function (bookingStatus) {
    return new Promise(function (resolve, reject) {
        Xrm.WebApi.retrieveRecord("bookingstatus", bookingStatus, "?$select=msdyn_fieldservicestatus").then(
            function success(result) {
                var msdyn_fieldservicestatus = result["msdyn_fieldservicestatus"];
                resolve(msdyn_fieldservicestatus);
            },
            function (error) {
                Xrm.Navigation.openAlertDialog(error.message);
            }
        );
    });
};

Retrieving multiple records using FetchXML

Below is an example of retrieving all Work Order Incidents to a Bookable Resource Booking

var WorkOrderIncidents = await CRM.BookableResourceBooking.GetWorkOrderIncidents(bookingId);

for (var i = 0; i < WorkOrderIncidents.length; i++) {
   //Do Stuff
}
CRM.BookableResourceBooking.GetWorkOrderIncidents = async function (bookableresourcebookingid) {
    return new Promise(function (resolve, reject) {
        var fetchData = {
            bookableresourcebookingid: bookableresourcebookingid
        };
        var fetchXml = [
            "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
            "  <entity name='msdyn_workorderincident'>" +
            "    <attribute name='msdyn_incidentresolved' />",
            "    <attribute name='msdyn_workorderincidentid' />",
            "    <attribute name='msdyn_resourcerequirement' />",
            "    <order attribute='msdyn_name' descending='false' />" +
            "    <filter type='and'>" +
            "      <condition attribute='msdyn_incidentresolved' operator='eq' value='0' />" +
            "      <condition attribute='statecode' operator='eq' value='0' />" +
            "    </filter>" +
            "    <link-entity name='msdyn_resourcerequirement' from='msdyn_resourcerequirementid' to='msdyn_resourcerequirement' link-type='inner' alias='ah'>" +
            "      <link-entity name='bookableresourcebooking' from='msdyn_resourcerequirement' to='msdyn_resourcerequirementid' link-type='inner' alias='ai'>" +
            "        <filter type='and'>" +
            "          <condition attribute='bookableresourcebookingid' operator='eq' value ='", fetchData.bookableresourcebookingid, "'/>" +
            "        </filter>" +
            "      </link-entity>" +
            "    </link-entity>" +
            "  </entity>" +
            "</fetch>",
        ].join("");

        Xrm.WebApi.retrieveMultipleRecords("msdyn_workorderincident", "?fetchXml=" + fetchXml).then(
            function success(result) {
                resolve(result.entities);
            },
            function (error) {
                resolve("error");
            }
        );
    });
};

Leave a Reply

Your email address will not be published. Required fields are marked *