# Issue Parts from Your Own Screen

On a work order's **Parts Used** tab, a technician picks a part, a location and a quantity. A custom technician screen or a barcode scanner workflow does the same thing in code, by calling `system.kanoa.inventory.issuePartToWorkOrderEvent`.

The function deducts the quantity from stock and records it against the work order. It returns the new `inventoryTransactionId` and a `message` that is `None` on success. To read the issues back, call `system.kanoa.inventory.getPartsUsedByWorkOrderEvents`.

The call needs the database ids of five things. Your screen or scanner usually knows names, so the script looks the ids up first.

| Field in `issueInfo` | Meaning | How the script finds it |
| --- | --- | --- |
| `workOrderEventId` | The work order | `system.kanoa.cmms.getWorkOrderEvents` by `workOrderEventName` |
| `partId` | The part | `system.kanoa.inventory.getParts` by `partNumber` |
| `inventoryAssetId` | Where the part came from | `system.kanoa.asset.getAssets` by `assetPath` of the storeroom |
| `assetId` | Where the part was used | `system.kanoa.asset.getAssets` by `assetPath` of the machine |
| `quantity` | How many | Passed in |

`notes`, `unitCost` and `usedBy` are optional.

:::download[Issue a part script]
<a href="/files/inventory-resources/issuePartToWorkOrder.py" download><code>issuePartToWorkOrder.py</code></a>
:::

The script issues `2` of `CF-INS-CNMG432` from `CoreForge\Philadelphia\Storage\Parts Room` to work order `MWO00001`, used on `CoreForge\Philadelphia\Machining\Machine 1`. It then prints the ids, the transaction id and message, and every part issued to that work order.

:::action[Issue a part from the Script Console]

1. In the Maintenance course you raised the `Machine 1 - Coolant leak` work order. In **Maintenance → Work Orders**, note its work order number.
2. In the Ignition Designer, open the **Script Console** (**Tools → Script Console**).
3. Paste in `issuePartToWorkOrder.py`.
4. Change the values at the top of the script. Set `workOrderEventName` to the number from step 1, and set `partNumber` to `CF-HOSE-075` and `quantity` to `3.0`.
5. Run the script.
6. Check the output. The first line lists the five ids. Then you see the new transaction id and `None` as the message. Each part issued to the work order is listed last.
7. In **Inventory → Parts**, expand `CF-HOSE-075`. The new issue is in the transaction history, linked to the work order.

:::

{/*
TODO: confirm on a gateway that workOrderEventName matches the work order number shown on Maintenance > Work Orders for Machine 1 - Coolant leak, as the download script's MWO00001 comment says.
*/}

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

## Read back the parts used

The last lines of the script show the read-back:

```python
data = system.kanoa.utilities.convertDatasetToJSON(
    system.kanoa.inventory.getPartsUsedByWorkOrderEvents({'workOrderEventId': workOrderEventId}))
for row in data:
    print row.get('partNumber'), row.get('quantity'), row.get('transactionTypeCode')
```

`getPartsUsedByWorkOrderEvents` takes optional filters such as `workOrderEventId`, `partId`, `assetId` and `partNumber`. An empty dictionary returns every row. Each row holds the transaction, part and location details, including `quantity`, `transactionTypeCode` and `inventoryAssetPath`.

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

## Use it in your own screen

Move the lookups and the call into a project script library function, such as `inventoryTools.issuePart(workOrderEventName, partNumber, quantity, storeroomPath, assetPath)`. Then a Perspective button or a scanner handler calls one function and shows the returned `message`.

:::tip
If `message` is not `None`, the issue did not happen. Show it to the user instead of assuming success.
:::

:::warning[Use the user's id]
The script issues parts as `SYSTEM`. In a technician screen, look up the signed-in person's id and pass it as `userId`, so the history names the right person. Use `usedBy` to record who consumed the part.
:::

{/*
TODO: confirm on a gateway how to get the signed-in Perspective user's Kanoa user id for userId, and which function to use. The scripting reference here only documents getIDPUserId with a userName.
*/}

You built a script that issues a part from the Parts Room to a work order, reads back the parts used, and is ready to become a library function for your own screen.
