Pagination in Freshservice Custom Objects API is not working

Hi All,

Thanks in Advance.

I am creating a custom app in Freshservice to automate the creation of Onboarding Ticket whenever a new employee is hired in my HRMS System.

I have my list of Job titles in a custom objects.
The list is quite big as it consists of 150+ items in it.

I want to get all the custom object records to do a mapping and create the onboarding ticket with proper job title.

The issue that I am facing here is the GET Api for custom object records only return a maximum of 100 records in a single request.
The page query param doesn’t seem to work here.

And When I referred to the API documentations, I came to know that there will be a next_page_link will be sent along with the response which will be further used to get the next page of records.

It is not working.

The below is the URL.
https://lego-us.freshworksapi.com/v1/entities/140671/records?page_size=100&next_token=2OIIo813r66KlskmtWry6ojtSfwYRtQlcECr7Cc0Pk3Z9tNyvDf1LOM7TVHdl&sort_by=bo_updated_at;DESC"

Can anyone help me with this?

Let me know if I am missing something here.

Hi @Sarnith_Balan,

Could you share the GET API request that has been used, along with the query parameters?
Also, is it possible to share the API response with hidden sensitive information?

I’m facing the same Issue with the custom object. Pagination isn’t working. For example i make GET request:

https://xxx.freshservice.com/api/v2/objects/26000005600/records?page_size=2

Answer is:

{
    "records": [
        {
            "data": {
              removed
            }
        },
        {
            "data": {
                removed
            }
        }
    ],
    "next_page_link": "https://lego-euc.freshworksapi.com/v1/entities/341329/records?page_size=2&next_token=2OIIo813r66Kv9VdRqrmJOKU6AK66fyXrNkvxGNOSIK8TRvFAbrcBWdxIyVHZ&sort_by=bo_updated_at;DESC",
    "meta": {
        "total_records": 27
    }
}

The next page link isn’t working. So I use the “next_token” in me new API Get request:

https://xxx.freshservice.com/api/v2/objects/26000005600/records?page_size=2&next_token=2OIIo813r66Kv9VdRqrmJOKU6AK66fyXrNkvxGNOSIK8TRvFAbrcBWdxIyVHZ

But the API sends the same result as on the first request.

Can you help?

I am running into the same issue. Can we get an ETA on when the Next_page_link will be fixed?

I was running into this same issue. Was on with support and they gave my a clunky work-around (passing a /V1 call into a /V2 endpoint).

This worked on the first “next-page” but returns the same token time and again after that. We are at 200 Max records… I guess thats progress…lol

Sorry to necro an old thread, but I ran into this. You aren’t supposed to load the next_page_link URL directly, it’s another query string parameter that you call instead. The docs kind of reference this, but it’s confusing until you run into this issue then it makes sense.

If you look at the Link header of the response you get, it actually gives you the full URL to call to get the next page of results (which is a normal thing to do, and some languages like Powershell have parameters so that you don’t have to call the next URL yourself, you can tell it to follow “rel” links which are returned in the Link header of REST calls to indicate the next page of results.

So depending on how or in what language you’re working with, you either need to make sure it will follow “rel” links and it’ll just work, or you need to use the value from the Link header on the response, parse it to get just the value, and then call that instead.

Found this old thread while trying to use pagination in Custom Objects.
I can’t believe this thread is 1,5 years old, and the problem seems still to exist.

Got a lovely response from FS Support, quote:

I am not a powershell expert, but this is my approach and I think this is how it should work, right? But same issues as all the others: I only get the first page of results, even when utilizing the pagination link in my query. Or did I something wrong here?

$FSAPIKey = "yourFSapiKey"
$apiURL = 'https://yoursubdomain.freshservice.com/api/v2/objects/54000013688/records?page_size=50'

#------------

# Authentication Headers
$EncodedCredentials = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(('{0}:{1}' -f $FSAPIKey, $null)))
$HTTPHeaders = @{}
$HTTPHeaders.Add('Authorization', ('Basic {0}' -f $EncodedCredentials))

#initialize array to store results
$allResults = @()

function Get-AllResults {
    # Make the initial API request
    Write-Host "First API Request: $apiURL"
    $response = Invoke-RestMethod -Uri $apiURL -Headers $HTTPHeaders -ContentType 'applicaton/json' -Method 'Get'

    # Add the current batch of results to the array
    $allResults += $response.records
    Write-Host "All Results: $($allresults.count)"
    Write-Host

    # Check if there is a next page link
    while ($response.next_page_link) {
        #URL encode the nextpage link
        $nplEncoded = [System.Web.HttpUtility]::UrlEncode($response.next_page_link)

        # Update the URL with the next page link
        $apiURLnpl = "$($apiURL)&next_page_link=$($nplEncoded)"

        # Make the next API request
        Write-Host "New Page Link API Request: $apiURLnpl"
        $response = Invoke-RestMethod -Uri $apiURLnpl -Headers $HTTPHeaders -ContentType 'applicaton/json' -Method 'Get'

        # Add the current batch of results to the array
        $allResults += $response.records
        Write-Host "All Results: $($allresults.count)"
        Write-Host
    }
}
Get-AllResults

2 Likes

ok, whoever stumbles on this thread, found the solution.
in my script above I used the method

([URI]::EscapeUriString($response.next_page_link))

to encode the string. Turns out this is not sufficient. Instead use this method to encode the string. I corrected my script above, works now like a charm.

[System.Web.HttpUtility]::UrlEncode($response.next_page_link)
2 Likes

Thank you, this helped so much.

2 Likes

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.