Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Power Apps Pro Dev & ISV
Unanswered

FetchXML query for empty date field not working with Xrm.WebApi.offline.retrieveMultipleRecords

(0) ShareShare
ReportReport
Posted on by 8

Hi, 

I have the following code which works fine online but not offline:

 

 

private static async validateWorkOrderIncidents(workorderId: string): Promise<string>
	{
		const fetchXml = `?fetchXml=<fetch mapping='logical'>
									<entity name='msdyn_workorderincident'>
										<filter type='and'>
											<condition attribute='msdyn_workorder' operator='eq' value='${workorderId}'/>
											<filter type='or'>
												<condition attribute='new_startdate' operator='null' />
												<condition attribute='new_enddate' operator='null' />
											</filter>
										</filter>
									</entity>
								</fetch>`;

		let serviceRequest;
		if (Xrm.Utility.getGlobalContext().client.isOffline())
		{
			serviceRequest = await Xrm.WebApi.offline.retrieveMultipleRecords(EntityNames.WorkorderIncident, fetchXml);
		}
		else
		{
			serviceRequest = await Xrm.WebApi.retrieveMultipleRecords(EntityNames.WorkorderIncident, fetchXml);
		}

		if (serviceRequest?.entities?.length > 0)
		{
			return 'Check if work order incident has start and end date!';
		}
		return 'OK!';
	}

 

 

Is there anything wrong in my code? Or is it even possible to query date fields offline.

 

I use this code to do some validation on the field service mobile app and even can't find a way to debug this 😕

 

Any help is welcome.

 

If further information is necessary, please let me know.

 

Thanks Chris

  • Diana Birkelbach Profile Picture
    3,072 Most Valuable Professional on at
    Re: FetchXML query for empty date field not working with Xrm.WebApi.offline.retrieveMultipleRecords

    Hi @ChrisTP , 

     

    It's a good idea to open the case. Can you please share here, in case you get a solution...?

     

    In the meanwhile, maybe it helps to use the fetchXml without filter (only in offline mode). Then you can filter using javascript.
    In offline you probably don't have that much data. Besides that, the data is already locally, so you have no latency. It seems to me to be pretty fast.

    I know, it's not a good practice, but it might unblock you for now.

  • Chris Penalty Profile Picture
    8 on at
    Re: FetchXML query for empty date field not working with Xrm.WebApi.offline.retrieveMultipleRecords

    Hello you two,

     

    first of all, thank you for your detailed analysis of my problem.

     

    I think @DianaBirkelbach has already summarized the whole problem very well.

     

    Also my tests indicate that it must be a problem with the offline SDK.

     

    When I create an incident initially without dates, the return value for the date fields is 'undefined' and the filter does what it should.

     

    InitBothDateMissingApp.JPG

    If I then set date fields and remove them again, the values are no longer 'undefined'. The values are then simply empty.

    BothDateMissing.JPG

    I don't know what kind of filter to use to catch this. And the suggested workaround is not practical for me.

     

    I will open a case for this and see what they can do.

     

    Thanks

    Chris

  • Diana Birkelbach Profile Picture
    3,072 Most Valuable Professional on at
    Re: FetchXML query for empty date field not working with Xrm.WebApi.offline.retrieveMultipleRecords

    Hi @ChrisTP , 

     

    I've made some tests in offline.

    I could

    •  make a fetch (the filter on date is null was applied),
    •  added new record 
      •  made the fetch again, and I've got one record more
    •  changed the date of the created record to something not null
      • made the fetch again, and the new record was not in the result anymore.

     

    It seems to me that the fetch works fine.

    BUT, it looks to me that the update of the date to null doesn't work; but only if the value was not null before.

    I've made a few updates on the same record. In the most cases there was no error on update, But making a fetch without any filter , I could see that the date was not updated tu null.

    Since it's an error in the offline sdk, I cannot help much. As a desperate workarround you could try to set the date to a very old date, instead of null.  Not a Date(0) - that seemed th broke the record-, but something like 1.Jan.1970 could work. Then you would need to change the filter on both "null" and 1970.

    Hope this helps!

     

  • eleung83 Profile Picture
    232 on at
    Re: FetchXML query for empty date field not working with Xrm.WebApi.offline.retrieveMultipleRecords

    Reading between the lines of what you said, you're expecting it to not satisfy the 1st if condition when you're clearing both date fields?

     

    If I've got that right, I would suggest trying to output the attributes of whatever entities are being returned to see what they contain. I would be surprised if it's a caching issue running in offline mode, as I would have thought it would have recognized there had been changes made to the data.

     

    Fyi, in the past it was possible to debug the JS within the mobile app, by using the Visual Studio Process debugger (I did it a few years back when I last worked on the FS mobile app), but I'm not sure if it's still possible now

  • Chris Penalty Profile Picture
    8 on at
    Re: FetchXML query for empty date field not working with Xrm.WebApi.offline.retrieveMultipleRecords

    Hi Diana,

    Thank you for your reply and the tip about not switching between online and offline. I did not know that the WebApi does this automatically. Yes, the table "msdyn_workorderincident" is available offline and I can create and see incidents without any problems. Thanks also for the tip with try catch. I will use this now for debugging purposes.

     

    I change my code now to the following:

     

    private static async validateWorkOrderIncidents(workorderId: string): Promise<string>
    	{
    		const fetchXml = `?fetchXml=<fetch mapping='logical'>
    									<entity name='msdyn_workorderincident'>
    										<filter type='and'>
    											<condition attribute='msdyn_workorder' operator='eq' value='${workorderId}'/>
    											<filter type='or'>
    												<condition attribute='new_startdate' operator='null' />
    												<condition attribute='new_enddate' operator='null' />
    											</filter>
    										</filter>
    									</entity>
    								</fetch>`;
    
    let serviceRequest;
    		try {
    			serviceRequest = await Xrm.WebApi.retrieveMultipleRecords(EntityNames.WorkorderIncident, fetchXml);
    		} catch (error) {
    			await Xrm.Navigation.openAlertDialog({text: `${error}`});
    		}
    
    		if (serviceRequest?.entities?.length > 0)
    		{
    			return 'Check if work order incident has start and end date!';
    		}
    		return 'OK!';
    	}

     

    But it behave like before. There is no error while retrieving the records. Actually, the filter works quite well, too. If I create new incidents without date, it finds them and if I fill the date fields, the filter recognizes that as well. So that the function successfully returns the string with 'OK! The main problem and why I actually opened the post is that a subsequent change to the date fields is not recognized. If I subsequently delete values from the date fields, the filter does not recognize that, so that 'OK!' is always returned as a string.
    When I am online, I do not have this behavior and it recognizes subsequent changes to the date fields without any problems.


    I hope you can read out what the real problem is and you can help me.

    Thanks a lot

  • Diana Birkelbach Profile Picture
    3,072 Most Valuable Professional on at
    Re: FetchXML query for empty date field not working with Xrm.WebApi.offline.retrieveMultipleRecords

    Hi @ChrisTP , 

     

    You don't need to make the switch between online and offline. The Xrm.WebApi will automatically do that for you.
    Is the table "msdyn_workorderincident" available in offline mode (defined in your offline profile)?

    Until we find a better debugging way, you could use some try-catch and use Xrm.Navigation.openAlertDialog to show the error. I know, it's not nice to work that way...but maybe helps you to get further.

  • Chris Penalty Profile Picture
    8 on at
    Re: FetchXML query for empty date field not working with Xrm.WebApi.offline.retrieveMultipleRecords

    Thanks for your reply.

    Good point but I have done this before and just missed to add "await" again from my previous tests.

    So this is still not working even with "await". 😕

    I have corrected this in my original post.

  • eleung83 Profile Picture
    232 on at
    Re: FetchXML query for empty date field not working with Xrm.WebApi.offline.retrieveMultipleRecords

    I've not yet had a reason to use the offline method version of the retrieveMultipleRecords, but seeing as the docs point to the same versions used for both offline and online, shouldn't you also "await" the offline retrieveMultipleRecords method response as it returns a promise that needs to be resolved?

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Leaderboard > Power Apps - Power Apps Pro Dev & ISV

#1
WarrenBelz Profile Picture

WarrenBelz 89 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 72 Super User 2025 Season 1

#3
mmbr1606 Profile Picture

mmbr1606 71 Super User 2025 Season 1

Overall leaderboard