logo
logo

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).

Note

Pre-commit means that the script executes before the triggering event. In our case, the script executes before the entity is modified.

Use case

  1. A user edits the filename of an asset.
  2. 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:

  1. The included libraries are:

    using System.Linq;
    using System.Threading.Tasks;
  2. Since the action executes during pre-commit, we retrieve the Target object from the Context and cast it to IEntity. The Target is the asset involved in the creation or modification event. If the asset does not exist, the script closes:

    var asset = Context.Target as IEntity;
    if(asset == null) return;
  3. The GetPropertyValue("FileName") method retrieves the FileName from the asset. If filename is null, or empty, the script returns:

    var filename = asset.GetPropertyValue<string>("FileName");
    if(string.IsNullOrEmpty(filename)) return;
  4. If the filename is in the specified format, it separates in two parts after splitting: {productNumber} and {name}. If not, the script returns.

    var splittedFilename = filename.Split('_');
    if(splittedFilename.Count() < 2) return;
  5. The GetProductId(productnumber) method lazy loads the productNumber. The productNumber is the number specified on the filename:

    var productNumber = splittedFilename[0];
    var productId = await GetProductId(productNumber);
  6. If the productId has a value, the SetIds method links the asset to the PCMProductToAsset relation:

    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 for product number {productNumber}!");
    }
  7. The GetProductId method uses the productNumber parameter to return the productId:

    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;
    }

Setup

To set up your script, action, and trigger:

  1. Create an asset (or edit an existing one) with a filename formatted as: {productnumber}_{name}.

  2. Create a script of the Action type; publish it, and enable it.

  3. Create an action of the Action script type, and link it to your script.

  4. Create a trigger:

    • 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.

Can we improve this article ? Provide feedback