How to access invoice context with custom buttons

A while back I asked how a Custom Button could know which receipt/invoice it was launched from, because my extension (URA EFRIS fiscalisation) needs to load that exact document. I have it working now, so here is what actually does the job, in case it helps someone else.

What did not work (in case you are going down these paths too):

  • Source = Url: Manager loads the URL in an iframe but passes no query parameters and no obvious context.
  • Placeholders like [Key], {Key}, {{Reference}} in the button content: rendered literally, never substituted.
  • Decoding the same-origin ?q= blob on the custom-button page: it is protobuf, and on my build it only contained the button’s own id and the business name, not the record key.

What works: Manager talks to the embedded page over postMessage. The extension asks the parent for the current page, and Manager replies with the record. My page (loaded as a plain Source = Url, e.g. https://localhost:5443/extension) does this:

window.addEventListener(‘message’, (event) => {
const d = event.data;
if (d && d.type === ‘page-response’) {
const b = d.body || {};
const key = b.query && b.query.key; // the current record’s Key
const path = b.path; // e.g. “/receipt-view” or “/sales-invoice-view”
// load that document by key…
}
});
// ask Manager for the current page/record:
window.parent.postMessage({ type: ‘page-request’ }, ‘*’);

Manager responds with something like:

{ “type”: “page-response”,
“body”: { “handler”: “ReceiptView”, “path”: “/receipt-view”,
“query”: { “key”: “019f7dab-…”, “business”: “Test Sample” } } }

body.query.key is the record Key, and body.path tells you the document type. From there you can read/enrich it however you like (I go through my own relay; you can also proxy Manager API calls back through the parent with an api-request / api-response message pair, which is how some other extensions do it, so you do not even need a token).

Key points that tripped me up:

  • You must send page-request first; Manager will not volunteer the context until asked.
  • It arrives asynchronously, so route on it whenever it lands rather than only at page load.
  • This works with the plain Url source. No inline snippet, no placeholders, no q decoding needed.

Credit: I found the pattern by looking at how existing extensions (the FBR and ZATCA ones) handle context, and by reading Manager’s own form pages, which use exactly this page-request / page-response exchange.

You don’t need to use post message

Instead, you should just use this to get the data:

fetch('/api4/view-v1' + window.location.search, {
    headers: { 'Accept': 'application/json' },
    credentials: 'same-origin',
})

Using api4 you can get path context using window.location.search and prefix view-v1.