acumatica Exporting Records via REST Contract-Based API Implementing Pagination on Multiple REST Requests


Example

In this example you will explore how to export the following data from Acumatica ERP in batches via the REST Contract-Based API:

  • stock items existing in the application in batches of 10 records
  • all sales orders in batches of 100 records

To export stock items in batches of 10 records with multiple REST calls:

To export first 10 stock items from a local AcumaticaERP instance by using the Default endpoint of version 6.00.001, you should use the following URL: http://localhost/AcumaticaERP/entity/Default/6.00.001/StockItem?$top=10

Accordingly, to request stock items from 10 to 20, you simply extend the URL above with filter parameter: http://localhost/AcumaticaERP/entity/Default/6.00.001/StockItem?$top=10&$filter=InventoryID gt '<InventoryID>'

<InventoryID> is the ID of the last stock item exported with a previous REST call

Below is the sample code written in C# to export all stock items in batches of 10 records by sending multiple REST calls to the Default endpoint of version 6.00.001:

using (RestService rs = new RestService(
    @"http://localhost/StackOverflow/", "Default/6.00.001",
    username, password, company, branch))
{
    var json = new JavaScriptSerializer();
    string parameters = "$top=10";
    string items = rs.GetList("StockItem", parameters);
    var records = json.Deserialize<List<Dictionary<string, object>>>(items);

    while (records.Count == 10)
    {
        var inventoryID = records[records.Count - 1]["InventoryID"] as Dictionary<string, object>;
        var inventoryIDValue = inventoryID.Values.First();
        string nextParameters = parameters + "&" + 
            "$filter=" + string.Format("InventoryID gt '{0}'", inventoryIDValue);
        items = rs.GetList("StockItem", nextParameters);
        records = json.Deserialize<List<Dictionary<string, object>>>(items);
    }
}

To export all sales orders in batches of 100 records with multiple REST calls:

To export first 100 sales orders from a local AcumaticaERP instance by using the Default endpoint of version 6.00.001, you should use the following URL: http://localhost/AcumaticaERP/entity/Default/6.00.001/SalesOrder?$top=100

Since the primary key of the Sales Order entity is composed by the Order Type and the Order Number, in this example you will be using a combination of filter parameters for the Order Type and Order Number fields:

  • to request sales orders from 100 to 200 of the SO type, you should use the following URL: http://localhost/AcumaticaERP/entity/Default/6.00.001/SalesOrder?$top=100&$filter=OrderType eq 'SO' and OrderNbr gt '<OrderNbr>'

<OrderNbr> is the number of the last sales order exported with a previous REST call

  • accordingly, to request first 100 sales orders of the next to SO type, you should use the following URL: http://localhost/AcumaticaERP/entity/Default/6.00.001/SalesOrder?$top=100&$filter=OrderType gt 'SO' and OrderNbr gt ''

Below is the sample code written in C# to export all sales orders in batches of 100 records with multiple REST calls to the Default endpoint of version 6.00.001:

using (RestService rs = new RestService(
    @"http://localhost/StackOverflow/", "Default/6.00.001",
    username, password, company, branch))
{
    var json = new JavaScriptSerializer();
    string parameters = "$top=100";
    string items = rs.GetList("SalesOrder", parameters);
    var records = json.Deserialize<List<Dictionary<string, object>>>(items);

    bool sameOrderType = true;
    while (records.Count > 0 && (records.Count == 100 || !sameOrderType))
    {
        var orderType = records[records.Count - 1]["OrderType"] as Dictionary<string, object>;
        var orderTypeValue = orderType.Values.First();
        var orderNbr = records[records.Count - 1]["OrderNbr"] as Dictionary<string, object>;
        var orderNbrValue = orderNbr.Values.First();

        string nextParameters = parameters + "&" + "$filter=" +
            string.Format("OrderType {0} '{1}'", sameOrderType ? "eq" : "gt", orderTypeValue) + " and " +
            string.Format("OrderNbr gt '{0}'", sameOrderType ? orderNbrValue : "''" );
        items = rs.GetList("SalesOrder", nextParameters);
        records = json.Deserialize<List<Dictionary<string, object>>>(items);
        sameOrderType = records.Count == 100;
    }
}