flycastpartnersinc / FreshservicePS

Powershell Module to interface with Freshworks Freshservice REST API.
MIT License
23 stars 5 forks source link

Get-FreshServiceCustomObjectRecord filter returning one page of results #11

Open rasimmers opened 1 year ago

rasimmers commented 1 year ago

Expected Behavior

Using the filter for Custom Object Record (https://api.freshservice.com/#list_all_custom_object_records) should filter records and return all pages.

Current Behavior

The Filter applies, but there is no next page link and incrementing the page does not return different results resulting in an infinite loop.

Possible Solution

Waiting on response from Freshworks development on how pagination works. Remarked -Filter parameters until remediated.

KiskaSanchez commented 8 months ago

@rasimmers test

KiskaSanchez commented 8 months ago

@rasimmers test

What is the status of this?

KiskaSanchez commented 8 months ago

To do:

r-wilkins commented 8 months ago

When reviewing what was causing the issue after reenabling the filter switch I noticed the logic of the Loop condition variable and the if statement for the filter switch was flawed because Freshservice seems to keep returning values for any page number when doing a filter for a custom object. This indeed seems like a bug but there are two different work arounds we can do.

The first one is the update the IF Statement and the Loop Condition variable The If Statement on lines 243-254 ran every single time and didn't check to see if it needed to loop again as well. This resulting it setting loopcondition back to false everytime because it always found 2 records.

 if ($PSBoundParameters.ContainsKey('filter')) {
                    #Pagination is manual for results returned from filter
                    Write-Verbose ('Using filter pagination for page {0}' -f $page)
                    #Manually increment page
                    $page++
                    #Update query
                    $qry['page'] = $page
                    $uri.Query = $qry.ToString()
                    $uriFinal = $uri.Uri.AbsoluteUri
                    #Update loop condition based on return results
                    $loopCondition = $content."$($objProperty)".Count -eq 0
                }

By including an additional condition here and updating the initial loop condition variable to say $result.Headers.Link -like "" this will aid in the loop logic easier than saying !$result.Headers.Link I made a change to our Module by updating the $loopCondition Variable on line 241 and the filter if statement on line 243. from

$loopCondition = !$result.Headers.Link

($PSBoundParameters.ContainsKey('filter'))

To

$loopCondition = $result.Headers.Link -like ""

if ($PSBoundParameters.ContainsKey('filter') -and !$loopCondition) {

This resulted in it fixing the filter logic for us for custom objects. I saw something similar happen with the get-fsasset filter switch too but haven't made any changes to that yet to test.

2 The other method is to possibly incorporate additional counting of the loop using the $content.meta.total_records value.

We other vendors with similar API outputs that allow us to see how many records are in a result for a specific table. Freshservice previously did not provide this from what I saw last year. So it shouldn't be too hard to implenment some additional counters to keep track of how many records it just read compared to the total per page compared to the overall total. I don't have the overall logic written out just yet for this particular module due to how I have to test my changes but I can find something works to be reviewed by the Flycast Partners team.

r-wilkins commented 8 months ago

The other Modules that have the same issue with their filter looping are Get-FreshServiceAgent Get-FreshServiceAsset Get-FreshServiceCustomObjectRecord Get-FreshServiceProjectTask Get-FreshServiceRequester Get-FreshServiceTicket

I was able to find these by referencing the $loopCondition Variable in any other Modules. I have confirmed that updated the asset module correct an issue where it would always warn about 40 page limit even on a empty result for a filter.

DavidHummingbird commented 1 month ago

Any updates on this? I need to use -Filter on Get-FreshServiceCustomObjectRecord, but because it's remarked out I can't use it currently :(