Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Suggested answer

Xrm.WebApi.createRecord not working in Offline mode

(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
 
 
  • Suggested answer
    Thomas David Dayman Profile Picture
    on at
    Xrm.WebApi.createRecord not working in Offline mode
    Have you added the offlinetimeentry to the mobile offline profile?
  • Ian_P Profile Picture
    on at
    Xrm.WebApi.createRecord not working in Offline mode
    If is useful to include an IF statement to check if the form is running in offline or online so that you can have 2 different parts of code to run, one for online, one for offline.
     
    Example below for only running script when offline:
     
    // Offline check - stop if online - script only for offline use
    if (Xrm.Utility.getGlobalContext().client.getClientState() == "Online") {
      return;
      }
    // End of Offline check
     
    Also _bookingstatus_value  becomes only bookingstatus for offline.
  • Suggested answer
    Dengliang Li Profile Picture
    Microsoft Employee on at
    Xrm.WebApi.createRecord not working in Offline mode
    Hi,
     
    Please try Xrm.WebApi.offline, which is the method provided by Microsoft to create and manage records in offline mode.
    Xrm.WebApi.offline (Client API reference) in model-driven apps - Power Apps | Microsoft Learn

    Best Regards,
    Dengliang Li
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans