Forum Discussion
Paginated api call issue - Function do not call the last page
- 1 year ago
freidav1984
Hi, The issue with yourList.Generate()function stopping before retrieving the last page is due to the ExitCondition being triggered too early. Specifically, when thenextfield isnull(or empty),List.Generate()immediately stops before processing the last page's data.
use this codelet
fn_getExpData = (initialUrl as text) =>
let
BaseUrl = "https://xxx.xxxxx.xxxxx.co.hu/2/expenses",AllData = List.Generate(
() =>
[
CurrentUrl = initialUrl,
Data = {},
Continue = true
],
each [Continue], // Keep iterating while Continue = true
each
let
CurrentUrl = [CurrentUrl],// Fetch API response
Response = try Json.Document(Web.Contents(BaseUrl, [Query=if CurrentUrl <> null then [next=CurrentUrl] else [], Headers=[Authorization="Bearer " & Token]])) otherwise null,// Extract Data
DataChunk = try Response[results][expenses] otherwise {},// Add metadata
Timestamp = DateTime.LocalNow(),
DataWithMetadata = List.Transform(DataChunk, each
Record.AddField(
Record.AddField(_, "API_URL", CurrentUrl),
"Response_Timestamp", Timestamp
)
),// Get Next Page URL
NextUrl = try Response[next] otherwise null,// Continue if there's data OR if there's another page
NewContinue = NextUrl <> null
in
[
CurrentUrl = NextUrl,
Data = DataWithMetadata,
Continue = NewContinue
],
each [Data] // Extract only the Data part
),FlattenedData = List.Combine(AllData) // Flatten list
in
FlattenedData
in
fn_getExpDataDid I answer your question? Mark my post as a solution! Appreciate your Kudos !!
hi freidav1984 ,
I would also take a moment to thank johnbasha33 , for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real difference.
May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.
Thank you.
Hi freidav1984 ,
I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If the response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.
Thank you.
- v-menakakota1 year ago
Community Support
Hi freidav1984 ,
I hope the information is helpful. Please let me know if you have any further questions or if you'd like to discuss this further. If this answers your question, please Accept it as a solution and give it a 'Kudos' so others can find it easily.
Thank you.