REST API results differ when called directly versus from javascript

I have a REST API (in the Query Library) that takes in a few inputs and works correctly when run directly from the Run button.

I also have some javascript where this same REST API (in the Query Library) is called. It has an asynchronous function that calls the REST API again from the callback within the javascript, but that part seems to be working.

The problem is that the results aren't the same when called from the javascript. I can't figure out why - am I referencing it the wrong way?

Here is my code

// Callback to store the results
function storeQueryResult( queryResult, idx, success ) {
var dateRange = localStorage.values.dateRange;
dateRange[idx].queryResult = queryResult;
localStorage.setValue( "dateRange", dateRange );

setTimeout( function() {
idx = idx + 1;
if ( idx < dateRange.length ) {
triggerNextQuery( idx );
}
}, 5000 );
}

function triggerNextQuery( idx ) {

// Calculate some input values to the query
var startDate = localStorage.values.dateRange[idx].startDate;
var endDate = localStorage.values.dateRange[idx].endDate;

// Put debug info in local storage
localStorage.setValue("currentParams", resourceId.value.toString() + " " + startDate.toISOString() + " " + endDate.toISOString());

// Trigger the REST API from the Query Library that was used in the app
return queryTimeEntries.trigger({
additionalScope: {
resourceId: resourceId.value,
startDateWorked: startDate.toISOString(),
endDateWorked: endDate.toISOString(),
},
onSuccess: function( queryResult ) {
storeQueryResult( queryResult, idx, true );
},
onFailure: function( queryResult ) {
storeQueryResult( queryResult, idx, false );
},
});
}

// Kick off the whole thing
setTimeout( function() { triggerNextQuery( 0 ); }, 5000 );

=====================
Results when called directly:
{
"items": [
{
"id": 9358,
"billingApprovalDateTime": null,
"billingApprovalLevelMostRecent": 0,
"billingApprovalResourceID": null,
"billingCodeID": 29682808,
"contractID": 29683540,
"contractServiceBundleID": null,
"contractServiceID": null,
"createDateTime": "2023-01-09T17:10:20.97Z",
"creatorUserID": 29682909,
"dateWorked": "2023-01-09T00:00:00Z",
"endDateTime": "2023-01-09T15:00:00Z",
"hoursToBill": 1.5,
"hoursWorked": 1.5,
"impersonatorCreatorResourceID": null,
"impersonatorUpdaterResourceID": null,
"internalBillingCodeID": 29682808,
"internalNotes": null,
"isInternalNotesVisibleToComanaged": false,
"isNonBillable": true,
"lastModifiedDateTime": "2023-01-09T17:10:20.97Z",
"lastModifiedUserID": 29682909,
"offsetHours": 0,
"resourceID": 29682909,
"roleID": 29682834,
"showOnInvoice": false,
"startDateTime": "2023-01-09T13:30:00Z",
"summaryNotes": "Discussions with Shane about organizational problems",
"taskID": 12608,
"ticketID": null,
"timeEntryType": 6
},
{
...
}
}

=====================
Results when called from javascript (i.e. the value in queryResult):
{
"items": [],
"pageDetails": {
"count": 0,
"nextPageUrl": null,
"prevPageUrl": null,
"requestCount": 100
}
}

=======================
Shouldn't the results be the same?

Hey @nl-setech!

When you run your JS query are you seeing all the query calls run successfully in your debug console (bottom right corner)?

I'm curious if dateRange[idx].queryResult = queryResult; is causing problems since it doesn't look as though dateRange[idx] has necessarily been instantiated as an object before you try and access the queryResult property on it. You might want to use something like dateRange[idx] = {queryResult} or

dateRange[idx] ??= {};
dateRange[idx].queryResult = queryResult;

Can you let me know what you find?

Yes, thank you for responding! I do see similar debug messages in the console. It even looks like it's sending in my additional scope variables. And, I believe my variables are initialized - I have a couple other setup functions that get called first. You can see the local storage stuff in one of the screenshots. The query is actually returning data, but the data is what I would expect if the additional scope variables are not being passed in. When I run the query manually without data, it returns the same thing.

(Also, I changed the name of my variable from dateRange to queries... Actually, I probably redid the code quite a bit trying out different ways to do Promises and async javascript to see if one of the ways would work. I included a screenshot of that too. The currentParams variable is what I used to check if there was a time delay between queries, and there is. That was before you pointed out where the debug info could be found...)

Ahh bizarre :thinking: would you mind also sharing a screenshot of the query library query as it exists in your app?

It may be worth checking to see if converting it to a regular query changes the behavior at all in case there's an issue interacting with the query library. A screenshot of queryTimeEntries would be great though!

First screenshot is when I run the query with no parameters. Second screenshot is when I run it with the parameters I'm trying to pass in via additionalScope. When I run the javascript script, I remove the hard-coded parameters from the library query, so that I know it's only running with the additionalScope parameters.

===========================

===========================

Ooo! I got something to work, I think. Your suggestion about trying the resource, rather than the library query made me think of a different way to pass in the parameters for some reason. Although in the end, I was still able to use the library query.

Since I was storing my additionalScope values already in local storage, I used the {{}} syntax on the library query to reference the values in local storage.

Then, I commented out the additional scope parameters and took advantage of the fact that these values were being updated in local storage prior to calling the query. Note that resourceId is already an input to my module, so I didn't have to pull that one out of local storage.

no-additional-scope-params

Nice! Glad that you got it working!

This might be a silly question but just to double check - when you were calling the query with additionalScope were you entering the values into the fields? e.g. {{ resourceId }} in the "resourceId" field?

I've tried reproducing the issue using a query library query and module but it seems to be working... it's good you have a workaround but you should be able to use additionalScope as well!

I tried it both ways actually. With default values in the fields, it would work, but it would never run with any other values. With default values empty, it would run, but wouldn't use the additional scope values, so the result returned empty.

Not necessarily with default values or empty values but specifically using a transformer with an undefined variable that matches your additionalScope variable, like this:

It may be counter-intuitive since the editor throws an error - which is why I ask!

I don't believe I did that.