Stock for every item in every warehouse, in 3 M3 API calls instead of 50,000

· Stewart Rutledge

"How much stock do I have for all items across all warehouses?" is one of the most common things people ask an H5 app to show. It's also where a lot of H5 apps for Infor M3 fall over. Built the usual way, with one M3 API call per record, the screen takes minutes to load or freezes the browser. Built with the M3 bulk API, the same screen loads with three requests.

This article goes through why per-record calls get so slow, how the bulk API in M3 REST v2 works, and what to watch for when you use it in your own H5 apps and scripts.

Why H5 apps that call M3 once per record get slow

Say you have 50 warehouses and 1,000 items. With the traditional H5 tooling, loading a stock overview usually looks like this:

  1. One request per warehouse to list its items.
  2. One request per item and warehouse to read the balance.
  3. More requests to read item details like status and description.

That adds up to 50,000 requests or more for a single view. Each one is a round trip through the network to M3 and back, and even when the browser sends several at a time, the total time is long. The result is a screen that's slow, a browser tab that struggles, or both. The way out is to make far fewer requests, and that's what the bulk API is for.

How the M3 REST v2 bulk API works

The v2 REST API in M3 takes a list of transactions in one request. You send one POST to /m3api-rest/v2/execute with the program, and a list of transactions for that program, each with its own input record. M3 runs them all and answers with one result per transaction, in the same order you sent them.

A request for three customers looks like this:

{
  "program": "CRS610MI",
  "maxReturnedRecords": 1,
  "transactions": [
    { "transaction": "GetBasicData", "record": { "CUNO": "C1001" }, "selectedColumns": ["CUNO", "CUNM"] },
    { "transaction": "GetBasicData", "record": { "CUNO": "C1002" }, "selectedColumns": ["CUNO", "CUNM"] },
    { "transaction": "GetBasicData", "record": { "CUNO": "C1003" }, "selectedColumns": ["CUNO", "CUNM"] }
  ]
}

And the answer has a results list with one entry per transaction. Here, shortened, the third customer doesn't exist:

{
  "results": [
    { "transaction": "GetBasicData", "records": [{ "CUNO": "C1001", "CUNM": "First customer" }] },
    { "transaction": "GetBasicData", "records": [{ "CUNO": "C1002", "CUNM": "Second customer" }] },
    { "transaction": "GetBasicData", "errorMessage": "..." }
  ],
  "nrOfSuccessfullTransactions": 2,
  "nrOfFailedTransactions": 1
}

A few things are worth knowing:

  • One failing transaction doesn't stop the others. Each result carries its own error message, so you handle errors per row instead of losing the whole request.
  • `selectedColumns` picks the output fields per transaction. Ask only for what the screen shows and the response stays small. The name matters: outputFields, the name used by the older script API, is rejected here as an unknown attribute.
  • `maxReturnedRecords` caps how many records a list transaction returns. Set it on purpose for list transactions instead of relying on a default.
  • Company and division can be given on the request when you need something other than the user's defaults.

In our own testing, three GetBasicData calls in one bulk request came back in about a quarter of a second, in a single round trip.

Calling the bulk API from inside H5

An H5 app or script running inside M3 calls the API in the user's own session, so there's no separate login. What it needs is a CSRF token. You fetch it once with a GET to /m3api-rest/csrf, which answers with the token as plain text, and send it back with every POST in the fnd-csrf-token header.

const csrf = await fetch('/m3api-rest/csrf').then((r) => r.text());
const response = await fetch('/m3api-rest/v2/execute', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json', 'fnd-csrf-token': csrf },
  body: JSON.stringify(request),
});

Every call runs as the signed in user, so the bulk API can't give anyone more than their M3 authority already allows.

The stock overview with three bulk requests

Back to the 50 warehouses and 1,000 items. With bulk requests the same view needs three:

  1. One bulk request that lists the items in every warehouse, one transaction per warehouse.
  2. One bulk request that reads the balances for all of them.
  3. One bulk request that reads the item details, one transaction per item.

The screen goes from tens of thousands of round trips to three, and the difference in speed and stability is dramatic. It also makes the app easier to reason about, because every M3 call it makes sits in a few well defined places.

Tips for using the bulk API in your own apps

  • Split very large lists into chunks. A single request with tens of thousands of transactions is hard on everything involved. Send a few hundred at a time and run a small number of chunks in parallel.
  • Ask for the columns you show. selectedColumns is the easiest performance win there is, especially for list transactions with wide records.
  • Treat errors as data. A result with an error for one row usually means that row is missing or invalid, so show it on that row and keep going.
  • Keep the order. Results come back in the order of the transactions, so you can match them to your input by position.
  • Read only what's on screen in scripts. An H5 script that adds a column to a list should read the visible rows in one request when the list loads, instead of one call per row as the user scrolls.
  • Don't build on v1. The bulk API is part of M3 REST v2. If an app still calls /m3api-rest/execute without /v2/, it can't use any of this, and v1 is being retired anyway. We wrote about moving H5 apps and scripts to v2.

How Loupe Code uses it

Everything Loupe Code builds uses the v2 API, and it reads in bulk by default. A list that needs a name from another program gets it with one request for all visible rows, and an app screen that combines data from several programs loads it in a few requests instead of one per record. The H5 script examples include a customer name column on OIS300 built exactly this way.

Questions we get

Does the bulk API work on M3 on premises?
The v2 REST API and bulk requests have been available on premises since M3 13.4, and in the cloud they keep improving.

Can one bulk request call several programs?
The request names one program at the top, with as many of that program's transactions as you need. For transactions from another program, send another request.

Does Infor's standard H5 SDK support the bulk API?
For a long time it didn't, which is a big part of why we built our own tooling for H5 apps. Check which SDK version your apps were built with.

If you have an H5 app that's slow to load, send it our way and we'll show you what it looks like with bulk requests. Reach out at hello@loupeapp.net, or read more about H5 apps built with Loupe Code.

This article started as a post on LinkedIn. Join the conversation there.

Got something on your M3 backlog?

Send it our way and we'll show you what Loupe Code does with it, on your own M3.

Stock for every item in every warehouse, in 3 M3 API calls instead of 50,000