Link an asset to a product
This action script example links an asset to a specified product when the filename of that asset changes to {product number}_{name}. The action executes in the pre-commit phase (in-process).
Pre-commit means that the script executes before the triggering event. In that case, the script executes before the entity is modified.
-
Ensure the schema includes entities and relations for
M.AssetandM.Product. -
Verify that the asset filenames match the requirements for linking, such as formatting or naming conventions.
Script
Script explanation
This section steps through the script in execution order, explaining each part. The numbered items describe the sequence, not instructions to perform.
-
Include the libraries to use in the script.
-
Since the action executes during pre-commit, retrieve the
Targetobject from theContextand cast it toIEntity. TheTargetis the asset involved in the creation or modification event. If the asset does not exist, the script exits: -
Using the
GetPropertyValuemethod, retrieve theFileNamefrom the asset. If the filename is null or empty, the script exits. -
If the filename is in the specified format, split it into two parts:
{productNumber}and{name}. If not, the script exits. -
Using the
GetProductId(productnumber)method, lazy load theproductNumber. TheproductNumberis the number specified on thefilename: -
If the
productIdhas a value, using theSetIdsmethod, link the asset to thePCMProductToAssetrelation. -
Using the
GetProductIdmethod, use theproductNumberparameter to return theproductId.
Setup
To set up your asset, script, action, and trigger:
-
Create an asset (or edit an existing one) with a filename formatted as:
{productnumber}_{name}. -
Create a script of the Action type; publish it, and enable it.
-
Create an action of the Action script type, and link it to your script.
-
-
On the General tab, set the Objective to Entity creation and Entity modification.
-
On the General tab, set the Execution type to In process.
-
On the Conditions tab, add a condition for the Asset(M.Asset) entity definition.
-
In this condition, add a new condition for the Filename(FileName) entity definition, and set it to Has changed.
-
On the Actions tab, add the related action under Pre-commit actions.
-
Save, and activate the trigger.
-