Sitecore Content Hub - Triggers, Actions and Scripts

Triggers:

In Sitecore Content Hub, triggers are events or conditions that initiate automated actions. These can include changes to assets, metadata updates, or other predefined criteria. Triggers serve as the starting point for automation workflows, reacting to specific events within the system.

Actions:

Actions in Sitecore Content Hub are the executable steps triggered by an event or condition. These steps can be a variety of operations, such as updating metadata, moving assets, or triggering notifications. Content Hub provides a set of predefined actions, and users can also create custom actions to suit their specific workflow requirements.

Scripts:

Scripts enhance the automation capabilities in Sitecore Content Hub. Users can write scripts using languages like JavaScript, C# to define custom logic for triggers and actions. These scripts allow for more flexibility and can be leveraged to perform complex operations, extending the automation capabilities beyond what predefined actions offer.

Let's understand the use of Trigger, Action and Script by implementing a specific use case.

Use Case:

Every time a new asset is uploaded in Content Hub, we have to make sure that the "Description" field is not blank. If so, set the default value "Description sample text".

Use Case Implementation:

Step #1 - Create Script
  • First thing we have to create Script. To do that let's got the manage screen and search for the Scripts.
  • Click on +Script button. One modal popup will appear. We need to provide the script name and script type here. 
  • There are different script types available in this dropdown like Action, Metadata processing, User sign-in etc. These are used as per the use case. In our case Action script type is fine.
  • Select the "Action" from dropdown and save this. 
  • Once this is saved, make sure you have enabled it by clicking on the toggle option. 
  • Once it is enabled, click on your Script, it will open Script editor window. To enter your script you need to click on "Edit" button available at the right top of the below screen.
  • Now it's time to write the actual code in the script editor window. As per the requirement we have to update the Description field, if it is blank. 

using Stylelabs.M.Sdk;

var assetLoadConfig = new EntityLoadConfiguration()
{
  PropertyLoadOption = new PropertyLoadOption("Description"),
  RelationLoadOption = RelationLoadOption.None,
  CultureLoadOption = CultureLoadOption.Default
};

var assetID = Context.TargetId.Value;
var asset = await MClient.Entities.GetAsync(assetID, assetLoadConfig);

if(asset != null)
{
	var description = asset.GetPropertyValue<string>("Description");
	
	if(string.IsNullOrEmpty(description))
	{
		asset.SetPropertyValue("Description", "Description sample text");
		
		MClient.Logger.Info($"Description field updated with default value for Asset ID: {assetID}");
	}
	
	await MClient.Entities.SaveAsync(asset).ConfigureAwait(false);
}

  • Copy paste this code in your script editor and save the changes. Once it is saved, build your code. it will take a while to build. Once the build is successful, publish this script.
Step #2 - Create an Action
  • Go to the manage screen again and search for "Actions".
  • Now click on "New action" button, it will open a modal popup window.
  • Give this action some name. Type should be "Action Script.
  • Now select the script which you want to call for execution.
  • In our case we just created "Sample Script" in the previous step, so we need to select the same.
  • Once we have populated everything, just save it.

Step #3 - Create a Trigger
  • Let's go to the manage screen, search for the Triggers.
  • Click on the New trigger button.
  • In this screen you will notice three tabs in "General", "Conditions" and "Actions". Let's go one by one.
  • In General tab first provide the Trigger name and  Description.
  • In the Objective field we have to select the event when this trigger will be called.
  • In our case it is clearly mentioned every time an asset is uploaded, so we need to select the "Entity creation" event.
  • Now you need to select the "Execution type". Possible values are "In process" and "In background".
  • "In Process" script will block the UI during execution and "In background" will run in the background.
  • We should avoid "In process" execution for the long running tasks. "In background" execution type is most suitable in this case.
  • Next got the the "Conditions" tab.
  • In this screen we have to specify the conditions, when meet the trigger will be called.
  • Since we have to perform this action on asset creation, so we need to select "M.Asset" definition.
  • Once the Asset definition is selected we need to add the condition for the Final Lifecycle Status.
  • We should select "Created" here as we have to perform this action when assets are getting uploaded.
  • After adding the condition it should look like the below screen.
  • Now we need to go to "Actions" tab.
  • Here we have to select the Action which has to be performed on the above condition.
  • As we have created "Sample Script" for this action so select the same from the list.
  • We are done with all the steps, now just click on Save and close.
  • Just make sure your trigger is enabled.

We are done with the Trigger, Action and Script creation. Now we need to test this implementation.

Testing Steps:

  • Go to Create page.
  • Import an asset from the Excel file. Use this sample test data where Description field is kept blank intentionally. 
File Title Description
https://sldemoassets.blob.core.windows.net/sldemoassets/dam-pro-final/arno-smit-141735.jpg Image title  
  • Once the Image import job is completed. Go to the asset details page and look at the populated properties. 
  • You should see the Description filed is populated with default value. i.e. "Description sample text".
  • You can also check the Script logs to validate this change. 
  • Go to the Scripts again and select the "Sample Script" we created.
  • Click on "View logs" button, it will show you the change logs it has made.
That's it.

Thanks!

Comments

Popular posts from this blog

Setup Sitecore XM Cloud Dev Environment using Docker

All Blog Posts - 2023