Link an asset to a product
This action script example links an asset to a specified product when the filename of an 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 our case, the script executes before the entity is modified.
Use case
- A user edits the filename of an asset.
- If the filename has a specific format with a product number, the asset is linked to the product.
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
The following steps describe how our example action script works:
-
The included libraries are:
RequestResponseusing System.Linq; using System.Threading.Tasks;
-
Since the action executes during pre-commit, we retrieve the
Target
object from theContext
and cast it to IEntity. TheTarget
is the asset involved in the creation or modification event. If the asset does not exist, the script closes:RequestResponsevar asset = Context.Target as IEntity; if(asset == null) return;
-
The
GetPropertyValue("FileName")
method retrieves theFileName
from the asset. Iffilename
is null, or empty, the script returns:RequestResponsevar filename = asset.GetPropertyValue<string>("FileName"); if(string.IsNullOrEmpty(filename)) return;
-
If the filename is in the specified format, it separates in two parts after splitting:
{productNumber}
and{name}
. If not, the script returns.RequestResponsevar splittedFilename = filename.Split('_'); if(splittedFilename.Count() < 2) return;
-
The
GetProductId(productnumber)
method lazy loads theproductNumber
. TheproductNumber
is the number specified on thefilename
:RequestResponsevar productNumber = splittedFilename[0]; var productId = await GetProductId(productNumber);
-
If the
productId
has a value, theSetIds
method links the asset to thePCMProductToAsset
relation: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}!"); }
-
The
GetProductId
method uses theproductNumber
parameter 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 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.
-
-
In the General tab, set the Objective to Entity creation and Entity modification.
-
In the General tab, set the Execution type to In process.
-
In 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.
-
In the Actions tab, add the related action under Pre-commit actions.
-
Save, and activate the trigger.
-