1. 製品のマスター アセットの設定

製品のマスター アセットの設定

Version:
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

このアクション スクリプトの例では、新しいアセットが製品に追加されたときに、製品内で最後に変更されたアセットをマスター アセット (カバー画像) として設定します。

このアクションはコミット前の段階 (処理中) で実行されます。

[!注] コミット前ということは、このスクリプトがトリガー イベントの前に実行されることを意味します。 この例では、PCMProductToAsset リレーションが実際に更新される前にスクリプトが実行されます。

ユース ケース

  1. ユーザーが製品に新しいアセットを追加します。
  2. 最後に変更されたアセットが製品のマスター アセットになります。

    [!注]

    ほとんどの場合、最後に変更されたアセットが最後に追加されたアセットですが、常にそうであるとは限りません。

スクリプト

using System;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using Stylelabs.M.Framework.Essentials.LoadOptions;
using Stylelabs.M.Framework.Essentials.LoadConfigurations;

var entityLoadConfiguration = new EntityLoadConfiguration();
entityLoadConfiguration.PropertyLoadOption = new PropertyLoadOption("ProductName");
entityLoadConfiguration.RelationLoadOption = new RelationLoadOption("PCMProductToAsset", "PCMProductToMasterAsset");

var entity = Context.Target as IEntity;
if (entity == null) return;

IParentToManyChildrenRelation relation = entity.GetRelation<IParentToManyChildrenRelation>("PCMProductToAsset");
IParentToManyChildrenRelation rel = entity.GetRelation<IParentToManyChildrenRelation>("PCMProductToMasterAsset");
if (rel == null) return;

IList<long> children = relation.Children;
DateTime d1 = new DateTime(2019, 11, 20, 6, 20, 40);
var id = 0;
foreach (long item in children)
{
    IEntity temp_entity = await MClient.Entities.GetAsync(item, entityLoadConfiguration);
    DateTime temp_date = (DateTime)temp_entity.ModifiedOn;

    if (DateTime.Compare(d1, temp_date) < 0)
    {
        d1 = temp_date;
        id = (int)temp_entity.Id;
    }
}

rel.Clear();
rel.Children.Add(id);
await MClient.Entities.SaveAsync(entity);

スクリプトの説明

この例のアクション スクリプトは次のように動作します。

  1. インクルードされるライブラリは次のとおりです。
この記事を改善するための提案がある場合は、 お知らせください!