Action script to set a master asset on a product
This action web 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.
Pre-commit means that the script executes before the triggering event. In our case, the script executes before the PCMProductToAsset relation is actually updated.
-
Ensure that the schema includes both
M.Asset andM.Productentity definitions. -
Verify that the
PrimaryImageproperty exists in the product schema for referencing assets.
Script
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
This section steps through the script in execution order, explaining each part. The numbered items describe the sequence, not instructions to perform.
The relation that connects this product to its master asset is named PCMProductToMasterAsset.
const string MasterAssetRelationName = "PCMProductToMasterAsset";-
Retrieve the product entity from the context (
Context.Target), assuming it is the target entity. If the product isnull, meaning it's invalid or not found, the script exits early.RequestResponseIEntity product = Context.Target as IEntity; if (product == null) { return; } -
Load the
AssetsRelationNameandMasterAssetRelationNamerelations for the product entity, making the required data available before proceeding.RequestResponseawait product .LoadMembersAsync( null, new RelationLoadOption(new[] { AssetsRelationName, MasterAssetRelationName })) .ConfigureAwait(false); -
Retrieve the
PCMProductToMasterAssetrelation as anIParentToManyChildrenRelationto connect a product to one or more assets. If the relation isnull, the script exits.RequestResponseIParentToManyChildrenRelation masterAssetRelation = product.GetRelation<IParentToManyChildrenRelation>(MasterAssetRelationName); if (masterAssetRelation == null) { return; } -
Update the
PCMProductToMasterAssetrelation to link the product to a single asset. In this example, the asset ID (12345) is used as the new link.SetLinksensures that only the specified asset is linked to the product as its master asset.RequestResponsemasterAssetRelation.SetLinks(new long[] { lastModifiedAsset.Id });
Setup
To set up your script, action, and trigger:
-
Add an asset to a product.
-
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.
-
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.
-