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
using System.Linq;
using System.Threading.Tasks;
var asset = Context.Target as IEntity;
if(asset == null) return;
var filename = asset.GetPropertyValue<string>("FileName");
if(string.IsNullOrEmpty(filename)) return;
var splitFilename = filename.Split('_');
if(splitFilename.Count() 2) return;
var productNumber = splitFilename[0];
var productId = await GetProductId(productNumber);
if(productId.HasValue){
var productRelation = asset.GetRelation("PCMProductToAsset");
productRelation.SetIds(new long[]{ productId.Value });
MClient.Logger.Info($"Asset {asset.Id} is linked to product {productId.Value}.");
}
else{
MClient.Logger.Warn($"No product found with product number {productNumber}!");
}
async Task<long?> GetProductId(string productNumber)
{
var query = Query.CreateQuery(entities =>
from e in entities
where e.Property("ProductNumber") == productNumber
&& e.DefinitionName =="M.PCM.Product"
select e);
var productId = await MClient.Querying.SingleIdAsync(query);
MClient.Logger.Info(productId.ToString());
return productId;
}
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.
RequestResponseusing System.Linq; using System.Threading.Tasks; -
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:RequestResponsevar asset = Context.Target as IEntity; if(asset == null) return; -
Using the
GetPropertyValuemethod, retrieve theFileNamefrom the asset. If the filename is null or empty, the script exits.RequestResponsevar filename = asset.GetPropertyValue<string>("FileName"); if(string.IsNullOrEmpty(filename)) return; -
If the filename is in the specified format, split it into two parts:
{productNumber}and{name}. If not, the script exits.RequestResponsevar splittedFilename = filename.Split('_'); if(splittedFilename.Count() 2) return; -
Using the
GetProductId(productnumber)method, lazy load theproductNumber. TheproductNumberis the number specified on thefilename:RequestResponsevar productNumber = splittedFilename[0]; var productId = await GetProductId(productNumber); -
If the
productIdhas a value, using theSetIdsmethod, link the asset to thePCMProductToAssetrelation.RequestResponseif(productId.HasValue){ var productRelation = asset.GetRelation("PCMProductToAsset"); productRelation.SetIds(new long[]{ productId.Value }); MClient.Logger.Info($"Asset {asset.Id} is linked to product {productId.Value}."); } else{ MClient.Logger.Warn($"No product found for product number {productNumber}!"); } -
Using the
GetProductIdmethod, use theproductNumberparameter to return theproductId.RequestResponseasync Task<long?> GetProductId(string productNumber) { var query = Query.CreateQuery(entities => from e in entities where e.Property("ProductNumber") == productNumber && e.DefinitionName =="M.PCM.Product" select e); var productId = await MClient.Querying.SingleIdAsync(query); MClient.Logger.Info(productId.ToString()); return productId; }
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.
-