How to List All Sales Orders in Zoho Inventory with Pagination

How to List All Sales Orders in Zoho Inventory with Pagination

Introduction:

While working with the Zoho Inventory API, I encountered the limitation of retrieving only 200 records per API call. To overcome this, I implemented pagination by tweaking a solution initially built for Zoho CRM. Below is the solution I used to fetch all sales orders, which can be adapted for other modules (Invoices, Packages, etc.).

Problem:

The Zoho Inventory API has a limit of 200 records per API call, which makes it challenging to retrieve a large dataset like all sales orders. This script helps by implementing pagination and recursively fetching data page by page.

Solution:

Here is the code I used. It loops through a list of page numbers and accumulates all records until no further pages are available:

  1. // *******
  2. // Zoho Inventory API Pagination for Listing All Sales Orders
  3. // *******

  4. // CONFIG: Organization ID
  5. organizationId = "*****";
  6. // CONFIG: Name of Zoho Inventory module (SalesOrders, Packages, Invoices, etc.)
  7. zohoInventoryModule = "salesorders";
  8. // CONFIG: Number of results to return per page. DO NOT exceed the per_page limit of your API, or this function will not perform correctly.
  9. perPageLimit = 200;

  10. // List of page numbers. This should start with '1' and be much longer than the expected number of pages.
  11. pageIterationList = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};

  12. // List to accumulate all records
  13. allRecords = List();

  14. // The 'while' condition for evaluation. While this is 'false', the API requests will continue. 
  15. iterationComplete = false;

  16. // Loop over each page in the page list
  17. for each page in pageIterationList
  18. {
  19.     // Evaluate whether 'while' condition is satisfied
  20.     if(iterationComplete == false)
  21.     {
  22.         // CONFIG: Set the param for OrganizationId if user is set in multiple Organizations
  23.         params = Map();
  24.         params.put("organization_id",organizationId);
  25.         
  26.         // Get records from Zoho Inventory API.
  27.         // CONFIG: Name of Zoho Inventory API Connection
  28.         response = invokeurl
  29.         [
  30.             url :"https://www.zohoapis.eu/inventory/v1/" + zohoInventoryModule + "?page=" + page + "&per_page=" + perPageLimit
  31.             type :GET
  32.             parameters:params
  33.             connection:"zohoinventoryfullcon"
  34.         ];

  35.         // Get records from API response. Add them to allRecords list.
  36.         records = response.get("salesorders");
  37.         allRecords.addAll(records);

  38.         // Update 'while' condition status
  39.         if(response.get("page_context").get("has_more_page") == false)
  40.         {
  41.             iterationComplete = true;
  42.         }
  43.     }
  44. }

  45. // Check for correctness
  46. info "Number of Records: " + allRecords.size();


    • Topic Participants

    • Bjorn