# Load BOMs by Script

Adding a BOM item takes a few clicks per part. For one machine that is fine. For thirty machines it is a job for a script.

`system.kanoa.inventory.addAssetBomItem` adds one BOM line to one asset. It returns the new `assetBomId` and a `message` that is `None` on success. A BOM line is either a `partId` or a `modelId`, never both.

:::download[Machine 1 BOM script]
<a href="/files/inventory-resources/addMachine1Bom.py" download><code>addMachine1Bom.py</code></a>
:::

The script rebuilds the Machine 1 BOM from [Build an Asset BOM](../03-bill-of-materials/1-asset-bom.md). It does these steps:

1. Sets `assetPath` to `CoreForge\Philadelphia\Machining\Machine 1` and lists the BOM lines as tuples of part number, quantity, unit code, position code and position name.
2. Looks up the asset id and the SYSTEM user id.
3. For each line, looks up the part id with `getParts` and the unit id with `getUnitOfMeasures`, then calls `addAssetBomItem`.
4. Prints each part number with its `assetBomId` and message.
5. Reads the BOM back with `getAssetBom` and prints each position, part number and quantity.

:::action[Run the BOM script]

1. In **Configuration → Ops → Assets**, select `Machine 1`, open its **Parts** tab, and delete the three BOM rows you added by hand. This lets you see the script rebuild them.
2. In the Ignition Designer, open the **Script Console** and paste in `addMachine1Bom.py`.
3. Run the script.
4. Check the output. Each line shows a part number, a new `assetBomId` and `None`. Then the BOM prints, in the order `T1`, `S1`, `C1` or similar.
5. Back on the **Parts** tab, refresh. The same three rows are there.

:::

::embed-section{src="../../../systemFunctions/inventory/addAssetBomItem.md" collapsed="true" summary="Reference Materials: addAssetBomItem"}

:::warning[Running it twice adds duplicates]
`addAssetBomItem` adds a new line each time, unless `bomInfo` includes an `assetBomId`. Passing an existing `assetBomId` updates that line instead. Before you re-run a load, either clear the old lines or look up the existing ones with `getAssetBom` and pass their ids.
:::

{/*
TODO: confirm on a gateway whether adding a second line with the same assetId, partId and positionCode is rejected or creates a duplicate row. The function reference does not say.
*/}

## Many machines at once

To load the same parts onto many machines, wrap the loop in a second loop over asset paths:

```python
machinePaths = [
    'CoreForge\\Philadelphia\\Machining\\Machine 1',
    'CoreForge\\Philadelphia\\Machining\\Machine 2',
]
for assetPath in machinePaths:
    assetId = getId('assetId', system.kanoa.asset.getAssets({'assetPath': assetPath}))
    for partNumber, qty, uomCode, positionCode, positionName in bomItems:
        # build bomInfo and call addAssetBomItem as in the script
        pass
```

Check which assets `getAssets` returns for each path before you load.

{/*
TODO: confirm on a gateway whether getAssets({'assetPath': ...}) matches one asset exactly or every asset starting with that path, as getWorkOrderEvents does. Then say whether this loop needs an assetId filter.
*/}

:::tip[Load from a file]
Keep the BOM lines in a CSV or an ERP export instead of in the script. Read the rows into the same tuple structure and the rest of the script does not change.
:::

## Model BOMs

If many machines share a parts list, put it on an equipment model once. `system.kanoa.inventory.addModelBomItem` works like `addAssetBomItem`, with `modelId` in place of `assetId`. A model BOM line holds either a `partId` or a `childModelId`, a sub-assembly modeled as its own model. Read a model BOM with `getModelBom(modelId)`.

`system.kanoa.inventory.addModel` creates a model, and `getModels` finds one by `modelNumber`.

::embed-section{src="../../../systemFunctions/inventory/addModelBomItem.md" collapsed="true" summary="Reference Materials: addModelBomItem"}

::embed-section{src="../../../systemFunctions/inventory/getAssetBom.md" collapsed="true" summary="Reference Materials: getAssetBom"}

You built a script that loads a bill of materials onto an asset, reads it back, and grows into a bulk load for many machines or a model BOM.
