Action script with a pre-commit trigger
This example shows how to automatically validate or modify entity data before it's saved, ensuring that all required fields are complete and compliant with your business rules. The web script is connected to a pre-commit trigger that executes before applied changes are saved by the system. This is useful, for example, to ensure images and web-friendly file types are appropriately categorized without manual intervention when uploading assets in bulk.
-
Ensure the assets referenced in the script exist in the Content Hub schema.
-
Define any required properties, such as
TitleorDescription, used in the script logic. -
Verify that users or roles executing the script have the necessary permissions to create or modify the referenced entities.
Script
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 be used in the script. To understand which libraries to include, see Context properties (property types).
-
Define a list of web-compatible file extensions to determine which files qualify as web assets.
-
Make sure that the file name property and asset type relation are loaded. If they are not, they will be lazy loaded.
-
Retrieve the target entity from the context (
Context.Target) and loaded with the necessary members. These include theFileNameproperty and theAssetTypeToAssetrelation. If the entity is invalid or the filename is empty, the script exits.WarningThe
Targetobject type depends on the trigger's objectives (for example, entity, entity definition, or policy). In this case, the trigger's objectives are Entity creation and Entity modification. Hence, we need to castTargettoIEntity. -
Extract the file extension from the filename using the
GetExtensionhelper method. If the extension is missing or not in the list of web-compatible extensions, the script exits. -
Create a query to find the
M.AssetType.Webentity. If the entity is not found, the script exits.NoteThe
MClientobject is always available and can be used by all script types. -
Retrieve the
AssetTypeToAssetrelation from the asset. If the relation exists, it sets the parent of the relation to theM.AssetType.Webentity, linking the asset to the web asset type. If there is no relation, the script exits. -
The helper method
GetExtensionextracts the file extension from the filename, ensuring that only relevant file types are processed.
Setup
-
On the Taxonomy page, create an asset type named Web with the identifier M.AssetType.Web.
-
Create, publish, and enable the script above as an Action script.
-
Create an action of type Action script and link it to the script.
-
Create a new trigger and set the trigger's objectives to Entity creation and Entity modification.
-
In the trigger conditions, add the entity definition Asset then add the condition Filename has changed.
-
In the trigger actions, add the new action to Pre-commit actions.
-
-
Save and activate the trigger.