How to get Paging Cookie while retrieving data using Fetch XML Query in Microsoft Flow

By | October 31, 2019

Introduction

Microsoft Flow is bringing new connectors day by day to ease the life of its users.

With the new connector for Common Data Service, while performing the List Records action, now we have the provision to use Select Query, Fetch XML Query and Skip token, rest of the other things remaining the same.

Microsoft Flow

Today, we’ll see how we can use Fetch Xml Query and then get the paging cooking to re-use it for next retrieve request.

Consider a scenario where I have an on demand flow which is called by passing the paging cookie, page number and count as parameters.

Based on the parameters passed, the retrieval takes place and also the necessary actions are performed on the retrieved records. After the retrieval, if there are more records, then the flow returns back the paging cookie for next retrieval.

Let’s take a look at how to design a Microsoft Flow for this request.

Step 1:

Select an HTTP Request Trigger.

Microsoft Flow

Given below is the JSON Schema as per the requirement.

{

"type": "object",

"properties": {

"paging-cookie": {

"type": "string"

},

"page": {

"type": "string"

},

"count": {

"type": "string"

}

}

}

Step 2:

Initialize the paging cookie. This is the paging cookie sent as a parameter in the http request.

Microsoft Flow

Value expression,

if(empty(triggerBody()?['paging-cookie']),'',concat('paging-cookie=',triggerBody()?['paging-cookie'],''))

Note: The above Value expression makes sure that we handle the empty paging cookie scenario as well.

Step 3:

Retrieve the records using Fetch XML.

Microsoft Flow

Here,

count – It is sent over as a parameter in the http request.

page – It is sent over as a parameter in the http request.

pagingCookie – This is the variable initialized in Step 2.

Step 4:

Perform necessary actions on the records retrieved in Step 3.

Microsoft Flow

Step 5:

Parse the JSON of the response from Step 3. This helps us get the paging cookie.

Microsoft Flow

For copying purpose, I am pasting here the JSON.

{

"type": "object",

"properties": {

"@@Microsoft.Dynamics.CRM.fetchxmlpagingcookie": {

"type": "string"

}

}

}

Step 6:

Return back the paging cookie to where the request was originated.

Microsoft Flow

The dynamic parameter used in the above body is a by-product of Step 5.

Note:

Out of the box Pagination brings the data in the multiple of 512, so if you have specific pagination needs, then using paging cookie makes more sense.

Microsoft Flow

Flow in its entirety:

Microsoft Flow

Conclusion

Now, using Microsoft Flow, we can also handle paging of the records as per the requirement.

8 thoughts on “How to get Paging Cookie while retrieving data using Fetch XML Query in Microsoft Flow

  1. MANISH JAIN

    How to use this paging cookie in subsequent requests ? Do we need to extract from pagingCookie= Please help.

    1. Inogic

      You can store the pagingCookie which you get in the response and use it for the next request.

      Thanks!

  2. Niels

    I’m having trouble parsing the parameters into the fetchXML.
    What is the proper way to do this?

    1. Inogic

      Do you mean that you are not able to add paging-cookie variable in the Fetchxml? If yes then use below expression and try again else please share the error details.

      if(empty(triggerBody()?[‘paging-cookie’]),”,concat(‘paging-cookie=’,triggerBody()?[‘paging-cookie’],”))

      Thanks!

  3. Sam

    How do you know what parameters to send and what is the URL format for feeding in those parameters?

    We need this type of flow but without an HTTP Request. We need to fetch all 50,000 records (with a few fields) to compare to another array of record IDs from a SQL. Any recommendation on how to use this without a HTTP Request, but still obtain and process the paging cookie?

    Fetch all account records from SQL
    Fetch all account records from CDS (Dynamics 365 Sales) <— quickly
    Compare accounts. Identify existing & missing
    Update existing records
    Create new accounts for those missing in CDS

  4. Grant Culp

    Hi – it is saying that the expression in step to is invalid when initializing the variable.

    This is the expression I used for the variable:

    if(empty(triggerBody()?[‘paging-cookie’]),”,concat(‘paging-cookie=’,triggerBody()?[‘paging-cookie’],”))

    Please let me know if something happened when I copied and pasted it.
    All the error message says is “The expression is invalid”

    1. Inogic

      Hi,

      It could be a problem with copy-paste. Updated the blog to add the expression in code snippet.

      Hope that helps!
      Thanks.

Comments are closed.