Command Palette

Search for a command to run...

1C:Enterprise Workflow Hooks

The 1C exchange runs as Medusa workflows, and the plugin exposes hooks in several of them. A hook handler receives what the exchange is about to write to Medusa or send to 1C, and returns a corrected version. That's how you move 1C requisites into product fields, add data Medusa doesn't have to an order, or enable a price list for a customer group, without changing the plugin's code.

What Is a Workflow Hook: 

A workflow hook is a point inside a Medusa workflow where you plug in your own function. Medusa calls it while the workflow runs and passes it data, and the workflow uses whatever the function returns. Read more in the Medusa documentation.


Available Hooks

Import the workflows from . Import the hook input and result types, as well as the functions that read 1C requisites and properties, from .

During the beta, hook names and element shapes may still change based on participants' feedback. After the release, they'll change only in major versions of the plugin.


Register a Handler

Register the handler in a file under in your Medusa project, and Medusa loads it when the server starts. For example, this handler keeps 1C services out of the store:

src/workflows/hooks/onec-catalog.ts
1import { StepResponse } from "@medusajs/framework/workflows-sdk"
2import { importOnecProductsWorkflow } from "@gorgo-store/medusa-erp-1c/workflows"
3import { readRequisite } from "@gorgo-store/medusa-erp-1c/hooks"
4
5importOnecProductsWorkflow.hooks.setProducts(async ({ products }) => {
6 return new StepResponse({
7 products: products.filter(({ source }) => readRequisite(source, "ВидНоменклатуры") !== "Услуга"),
8 })
9})

The handler receives the batch's products and returns them in a , keeping only those whose "ВидНоменклатуры" requisite isn't "Услуга" (service). The function reads a requisite of a 1C product by its name, so you don't have to parse CommerceML yourself.


How an Element Is Built

Every hook except receives an array of elements. An element brings together everything the handler needs to make a decision:

  • – the record the result is built from. On import, it's the 1C record, all the product's offers for , and on export, the Medusa order.
  • – the current Medusa record. It's passed only on import, and only for a record that already exists in Medusa.
  • The target field – what the exchange writes or sends. It's for a product, for a product's variants, and for a price type, and for an order.

The handler edits the target field and returns the elements. works differently. It receives the finished XML as a string and handles changes that the product or document model can't express, for example the schema version your 1C configuration expects.


Result Rules

  • Return . The plugin uses only the array, so you don't need to return the other input keys.
  • Remove an element to skip that record in this exchange. An order removed from the export doesn't get the exported mark and goes out with the next export.
  • Delete a key from , or a variant in to leave that field unchanged. That way a value set in the Medusa Admin survives the exchange. An order goes to 1C in full, and a deleted key turns into an empty node in . So change values in a document instead of deleting them.
  • Don't add elements or change matching keys. The plugin matches a product and a variant by their 1C before calling the hook, and 1C recognizes an order by the document's . If a handler breaks this rule, the plugin stops the exchange with an error naming the hook.
  • Return the same result for the same input. Otherwise, every repeated exchange rewrites the catalog again.

If a handler returns a plain object or nothing, the exchange stops with an error like . Medusa passes the plugin only what's inside the .


One Handler for Each Hook

Medusa allows only one handler per hook in the whole application. If you register a second handler on the same hook, the server fails to start with the error . Combine all of your project's changes for one hook into a single handler and run them one after another inside it.


How a Hook Affects Exchange Speed

The handler runs inside a workflow step, and 1C waits for a response the whole time. , and run on every batch, so with a 10,000-product catalog each of them runs a hundred times. The workflow engine copies the hook's input and result, along with anything you add to an element.

Here's how three kinds of handler affect the exchange:

The measurements used a catalog of 1,000 products and 4,000 offers, which is 10 batches, with a local PostgreSQL where one query takes about a millisecond. The table shows medians of three runs, and a single run for the query-per-product handler. A re-import writes almost nothing, so a handler's cost shows most there. With a remote database, network latency is added to every query, and the gap grows.

To keep a handler from slowing the exchange down:

  • Make one query per batch. Collect the keys of all elements, run one call filtered by that array of keys, and put the response into a .
  • Keep elements lean. Hold lookups that only the handler needs in local variables, not in the element.
  • Write to the database from a handler only with compensation. Pass a rollback function as the second argument when registering the hook, and the data it needs as the second argument of . The plugin commits each batch separately, and a batch that fails rolls back only its own changes.
  • Don't make the result depend on batch order. After a failure, 1C repeats the whole exchange, and the batches may come in a different composition.

This is what a query per product looks like. For a batch of 100 products, it's 100 queries:

1for (const item of products) {
2 const [brand] = readProperty(item.source, properties, "Бренд")
3 const { data } = await query.graph({
4 entity: "product_collection",
5 fields: ["id"],
6 filters: { title: brand },
7 })
8 // ...
9}

One query per batch gets the same data for all products at once:

1const titles = products.flatMap(({ source }) => readProperty(source, properties, "Бренд"))
2const { data } = await query.graph({
3 entity: "product_collection",
4 fields: ["id", "title"],
5 filters: { title: titles },
6})
7const collectionIdByTitle = new Map(data.map((collection) => [collection.title, collection.id]))

The full example with this query is on the Hook Examples page.

Warning: 

On a test catalog of a few products, queries per product go unnoticed. They show up on the full catalog, once the exchange is already running in production.

Measure It Yourself

Start the server with . The plugin logs the duration of every catalog import step, and the hook's step is named after the hook itself:

and run inside the child workflows of the offers import, so their time is part of the overall line. For exports, the plugin doesn't log step durations.


References

Edited Oct 20, 2026·Edit this page