Set a master asset on a product

This action script example links a master asset to a product entity by updating the PCMProductToMasterAsset relation. Use this script when you want to automatically set a specific asset as the product's master asset whenever a new product is created or a new primary image is uploaded for an existing product.

The action is triggered in the pre-commit (In process) phase.

Note

Pre-commit means that the script executes before the triggering event. In our case, the script executes before the PCMProductToAsset relation is actually updated.

Before you begin
  • Ensure that the schema includes both M.Asset and M.Product entity definitions.

  • Verify that the PrimaryImage property exists in the product schema for referencing assets.

Script

RequestResponse
const string MasterAssetRelationName = "PCMProductToMasterAsset";

// gets the product
IEntity product = Context.Target as IEntity;
if (product == null)
{
    return;
}

// ensure relations are loaded
await product
    .LoadMembersAsync(
        null,
        new RelationLoadOption(new[] { AssetsRelationName, MasterAssetRelationName }))
    .ConfigureAwait(false);

// gets the master asset relation
IParentToManyChildrenRelation masterAssetRelation = 
    product.GetRelation<IParentToManyChildrenRelation>(MasterAssetRelationName);
if (masterAssetRelation == null)
{
    return;
}

// sets the master asset relation to a single asset
// 12345 is the asset id
masterAssetRelation.SetLinks(new long[] { lastModifiedAsset.Id });

Script explanation

The relation that connects this product to its master asset is named PCMProductToMasterAsset.

RequestResponse
const string MasterAssetRelationName = "PCMProductToMasterAsset";

The script retrieves the product entity from the context (Context.Target), assuming it is the target entity. If the product is null, meaning it's invalid or not found, the script exits early.

RequestResponse
IEntity product = Context.Target as IEntity;
if (product == null)
{
    return;
}

The script loads the AssetsRelationName and MasterAssetRelationName relations for the product entity, making the required data available before proceeding.

RequestResponse
await product
    .LoadMembersAsync(
        null,
        new RelationLoadOption(new[] { AssetsRelationName, MasterAssetRelationName }))
    .ConfigureAwait(false);

The script retrieves the PCMProductToMasterAsset relation as an IParentToManyChildrenRelation to connect a product to one or more assets. If the relation is null, the script exits.

RequestResponse
IParentToManyChildrenRelation masterAssetRelation = 
    product.GetRelation<IParentToManyChildrenRelation>(MasterAssetRelationName);
if (masterAssetRelation == null)
{
    return;
}

The script updates the PCMProductToMasterAsset relation to link the product to a single asset. In this example, the asset ID (12345) is used as the new link. SetLinks ensures that only the specified asset is linked to the product as its master asset.

RequestResponse
masterAssetRelation.SetLinks(new long[] { lastModifiedAsset.Id });

Setup

To set up your script, action, and trigger:

  1. Add an asset to a product.

  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:

    • 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) and Product (M.PCM.Product) entity definitions.

    • In the Asset (M.Asset) condition, add a new condition for the Products (PCMProductToAsset) relation, and set it to Has changed.

    • In the Product (M.PCM.Product) condition, add a new condition for the Assets (PCMProductToAsset) relation, and set it to Has changed.

    • On the Actions tab, add the related action under Pre-commit actions.

    • Save and activate the trigger.

Do you have some feedback for us?

If you have suggestions for improving this article,