locked
Inconsistent query results through the visual collection RRS feed

  • Question

  • I made a query which returns inconsistent results when used through the visual collection but it returns consistent results when used directly from the data workspace.

    Here is the problematic query (Set to return One Results):

    Private Sub Current_Visit_PreprocessQuery(Patient_ID As System.Nullable(Of Integer), ByRef query As System.Linq.IQueryable(Of LightSwitchApplication.Visit))
        query = query.Where(Function(Entity) Entity.Patient.ID = Patient_ID And EntityFunctions.TruncateTime(Entity.DateTime) = Today).Take(1)
    End Sub
    

    The query Current_Visit is bound to the query Current_Patient which work perfectly well:

    Private Sub Current_Patient_PreprocessQuery(ByRef query As System.Linq.IQueryable(Of LightSwitchApplication.Patient))
        query = query.Where(Function(Entity) Entity.Contact.UserName = Application.User.Name).Take(1)
    End Sub
    

    I made two functions to test the data availability and be sure the query Current_Visit is working well, when calling GetValueFromDataWorkspace I always get the Visit ID but when I call GetValueFromVisualCollection I sometime get the Visit ID and sometime get Visit is Undefined.

    Function getting the value directly from the data workspace:

    myapp.Test.GetValueFromDataWorkspace_execute = function (screen) {
        query = screen.details.dataWorkspace.ClinikData
            .Current_Visit(Patient.ID);
        promise = query.execute().then(function (result) {
            Visit = result.results[0];
        });
        promise.done(function () {
            if (Visit == undefined)
                msls.showMessageBox("Visit is Undefined!");
            else
                msls.showMessageBox("Visit ID = " + Visit.ID);
        });
    };
    

    Function getting the value through the visual collection:

    myapp.Test.GetValueFromVisualCollection_execute = function (screen) {
        screen.getCurrent_Visit().done(function (Visit) {
            if (Visit == undefined)
                msls.showMessageBox("Visit is Undefined!");
            else
                msls.showMessageBox("Visit ID = " + Visit.ID);
        });
    };
    

    Any help to fix this issue would be appreciated.

    Thanks!

    Thursday, January 16, 2014 4:27 PM

All replies

  • Clean and rebuild your application first.

    Friday, January 17, 2014 9:40 AM
  • I did that numerous times and I am also running in Debug Mode but it doesn't display any error.
    Friday, January 17, 2014 3:27 PM
  • I suspect Visit is always undefinded in the collection test because the the screen.get... method doesn't return a single entity, rather a collection (specifically a promise object having the data in the first parameter of the .then method) visits.data is as object array containing each item in your collection on the screen.

    I'd suggest using different variable names for Visit in your two test methods so you can be sure one is not setting the other. 

    Then try something like this (air-code not tested):

    screen.getCurrent_Visit().then(function (visits) { visits.data.forEach(function (visit) { if (visit.details["ID"]) { msls.showMessageBox("Visit ID = " + visit.details["ID"]); } });

    });

    HTH,

    Josh





    • Edited by joshbooker Friday, January 17, 2014 6:58 PM
    Friday, January 17, 2014 4:37 PM
  • It is set to a single entity not a collection and I would get an error if it was not the case because I would need to use selectedItem.

    I also said it is inconsistant, sometime I get the result so it is not always undefined.

    Friday, January 17, 2014 4:53 PM
  • I thought .get... methods always return a collection, maybe not.

    My point was, perhaps it's inconsistent because first test is setting Visit variable since the variable names are the same. (?)

    Have a great day!

    Josh


    • Edited by joshbooker Friday, January 17, 2014 6:57 PM
    Friday, January 17, 2014 5:02 PM
  • Well I use the same variable but it's only to store the results so this should not make any difference as long as I display the variable content right after storing the results into it.

    Have a great day!

    Alain

    Friday, January 17, 2014 5:27 PM
  • After some more testing I found that the query Current_Visit which is bound to the query Current_Patient is executed before getting the parameter value from Current_Patient.

    I done that test with a clean new screen without any code behind, just my two queries bound together with a breakpoint in Current_Visit_PreprocessQuery and when I check Patient_ID it is set to Nothing.

    Does anyone know how I could force the parameterized query Current_Visit to wait for the query Current_Patient to be ready before executing.

    Both queries are set to returns a single entity.


    • Edited by VAlain Friday, January 17, 2014 6:18 PM
    Friday, January 17, 2014 6:17 PM
  • How is your screen binding Patient_ID parameter in Current_Visit query?  Is Parameter Binding Property set to Current_Patient.Id? 

    I still don't understand what you mean by set to return a single entity.  My understanding is that all queries return a collection even if .take(1) makes them return a collection of one.

    Friday, January 17, 2014 7:23 PM
  • Yes my Parameter Binding is set to Current_Patient.ID and the query is working sometimes so it appear that it is done properly or it would never work.

    The .Take(1) is not enough of course I also had to set the query property Number of Results Returned to One instead of the default Many.

    After some more testing I found that the query Current_Visit is sometime executed before it's parameter value is filled with the bound value so the Patient_ID parameter is then set to Nothing.

    It look like there is a bug with the binding when two single entity queries are bound together and since I don't have much control on that I think I will embed Current_Patient into Current_Visit, it would solve the problem.

    Friday, January 17, 2014 8:06 PM
  • Sorry I forgot about Number of Result Returned. 

    One more thought... if the query parameter optional, does the query still execute early if the param was not optional?

    Friday, January 17, 2014 8:15 PM
  • The parameter is not optional even if it is still represented as Nullable in the DataService.
    Friday, January 17, 2014 8:29 PM