Skip to main content
Dynamics 365 Community / Forums / Field Service forum / Xrm.WebApi.createRecor...
Suggested answer

Xrm.WebApi.createRecord not working in Offline mode

editSubscribe (0) ShareShare
ReportReport
Posted on by
Hi,
 
We are working on functionality where on a button click a given work order's details - like serviceincidents, bookablebookings, timeentries etc -  to be validated and shown a confirmation dialog at the end. We have created a .js file (with namespace) and calling separate methods to validate each category of records. As part of validating the bookings, we need to find bookable bookings with specific statuses and update them as completed and then create a new record in a custom entity called /new_offlinetimeentry/. 
 
Below is the code we have written and it works fine in FS Web. However, in FS App (Windows), it fails when we invoke Xrm.WebApi.createRecord. Tried various options but no luck so far. We are not sure what is wrong here.
 
validateBookings: async function (workOrderId) {
    var msg = //;
    var bookingStatusId= /6eda1f8f-12c2-ea11-a812-000d3a4df1cd/;
    var bookableResourceBookings = null;
    let filterQuery = /$filter=_msdyn_workorder_value eq / + workOrderId + / and statecode eq 0 and _bookingstatus_value ne 6eda1f8f-12c2-ea11-a812-000d3a4df1cd/;;
    if (Xrm.Utility.getGlobalContext().client.isOffline()) {
        filterQuery = /$filter=msdyn_workorder eq / + workOrderId + / and statecode eq 0 and _bookingstatus_value ne 6eda1f8f-12c2-ea11-a812-000d3a4df1cd/;
    }
    //Retrieve pending bookings
    bookableResourceBookings = await Xrm.WebApi.retrieveMultipleRecords(/bookableresourcebooking/, /?$select=name,bookableresourcebookingid,_bookingstatus_value,_msdyn_workorder_value,_resource_value&$expand=BookingStatus($select=description,name,new_workcomplete)&/ + filterQuery);
    //Iterate through pending bookings and close them
    if (bookableResourceBookings.entities.length > 0)
    {
        var userSettings = Xrm.Utility.getGlobalContext().userSettings;
        var userid = userSettings.userId.replace(/{/, //).replace(/}/, //);
        for (var j=0; j< bookableResourceBookings.entities.length; j++) {
            var result =  bookableResourceBookings.entities[j];
            var workcompleted = result.BookingStatus.new_workcomplete;
            if (workcompleted==false)
            {
                var bookableresourcebookingid = result[/bookableresourcebookingid/];
                var workordername = result[/name/]
                if (workordername == undefined)
                    workordername = new Date().toISOString();
                var entity = {};
                entity[/msdyn_name/] = workordername;
                entity[/msdyn_booking@odata.bind/] = //bookableresourcebookings(/+bookableresourcebookingid+/)/;
                entity[/msdyn_BookingStatus@odata.bind/] = //bookingstatuses(/+bookingStatusId+/)/;
                entity[/ownerid@odata.bind/] = //systemusers(/ + userid + /)/;
                entity.msdyn_systemstatus = 690970002;
                entity.msdyn_timestampsource =  690970001;
                entity.msdyn_timestamptime = new Date().toISOString();
                entity.msdyn_generatejournals = false;
 
                await Xrm.WebApi.createRecord(/new_offlinetimeentry/, entity).then(
                    function success(result) {
                        var newEntityId = result.id;
                        Xrm.Utility.alertDialog(/[Async] Success createBookingTimeStamp / + JSON.stringify(newEntityId));
                        var Updaterecord = {};
                        Updaterecord[/bookableresourcebookingid/] =bookableresourcebookingid;
                        Updaterecord[/BookingStatus@odata.bind/] = //bookingstatuses(6eda1f8f-12c2-ea11-a812-000d3a4df1cd)/; // Lookup
                        var updatedRecordId = Xrm.WebApi.updateRecord(/bookableresourcebooking/, bookableresourcebookingid, Updaterecord);
                        Xrm.Utility.alertDialog(/[Async] BookableResource booking updated / + JSON.stringify(updatedRecordId));
                    },
                    function (error) {
                        Xrm.Utility.alertDialog(JSON.stringify(error));
                    }
                );
            }
        }
    }
    return msg;
},
 
Below are the exceptions thrown:
 
{/errorCode/:0,/message/:/Relationship undefined not found on new_offlinetimeentry/,/code/:0,/raw/:////{///////errorCode///////:0,///////message///////:///////Relationship undefined not found on new_offlinetimeentry///////,///////blockErrorReporting///////:false,///////faultedRequestIndex///////:-1,///////errorSource///////:0}////}
 
{/errorCode/:2147746581,/message/:/An error has occurred. Try this action again. If the problem continues, check the Microsoft Dynamics 365 Community for solutions or contact your organization's Microsoft Dynamics 365 Administrator. Finally, you can contact Microsoft Support./,/code/:2147746581,/raw/:/{}/}
 
 
Any help / pointers to resolve will be helpful.
 
Regards