通过 SDK 创建记录时设置货币

Set currency when creating Records via SDK

我有一个实体,它有一个或多个 Money-字段。因此,它还有一个 transactioncurrencyid 字段,用于指定记录的货币。

现在,当我在浏览器中创建该实体的记录时,transactioncurrencyid 字段会预先填充系统或用户的默认货币。

但是,当通过 SDK 创建该实体的记录时没有为任何 Money 字段指定值,transactioncurrencyid 字段保持为空。之后,没有用户能够编辑任何 Money 字段,因为未设置货币:

A currency is required if a value exists in a money field. Select a currency and try again.

奇怪的是,当我为 Money-field 设置一个值时(即使我没有告诉它使用什么货币),这并没有发生,例如:

var entity = new Entity("new_myentity")
{
    Attributes = 
    {
        { "name" = "Name 1" },
        { "amount" = new Money(1000) }
    }
}

所以我的问题是:
在通过 SDK 创建记录时没有任何填充的 Money-Attributes 时,有没有办法将记录的货币设置为默认值?

也遇到过这个问题,起初很困惑,因为它似乎没有模式,但是后来是的,手动创建的记录没问题,但是由门户网站创建的记录会将 transactioncurrencyid 设置为null(仅当创建时未指定货币字段)。

所以我将以下代码添加到 post-create 插件中,确保始终填充此字段。在此示例中,我专门使用了欧元货币,但检索货币 guid 的方法可以修改为 return 默认值。

        IOrganizationService service = localContext.OrganizationService;

        Entity retrievedEntity = localContext.PluginExecutionContext.InputParameters["Target"] as Entity;
        if (retrievedEntity.LogicalName == Account.EntityLogicalName)
        {
            try
            {
                Entity updatedEntity = new Entity(retrievedEntity.LogicalName);
                updatedEntity.Id = retrievedEntity.Id;

                //Autopopulate the Transaction Currency field - only if transactioncurrencyid field is missing in Attributes collection
                if (!retrievedEntity.Attributes.Contains("transactioncurrencyid"))
                {
                    string euroCurrencyId = RetrieveEuroCurrencyId(service);

                    if (euroCurrencyId != null)
                    {
                        EntityReference currencyType = new EntityReference();
                        currencyType.Id = new Guid(euroCurrencyId);
                        currencyType.LogicalName = TransactionCurrency.EntityLogicalName;

                        updatedEntity.Attributes.Add(new KeyValuePair<string, object>("transactioncurrencyid", currencyType));
                        updatedEntity.Attributes["transactioncurrencyid"] = currencyType;
                    }
                }
                localContext.OrganizationService.Update(updatedEntity);
            }
            catch (Exception ex)
            {
                throw;
            }                       
        }

获取货币 guid 的方法如下所示...

    //Use this method to return the Guid for the Euro currency
    public static String RetrieveEuroCurrencyId(IOrganizationService service)
    {
        String result = null;

        QueryExpression query = new QueryExpression();
        query.EntityName = TransactionCurrency.EntityLogicalName;
        query.ColumnSet = new ColumnSet(new string[] { "transactioncurrencyid", "currencyname" });
        query.Criteria = new FilterExpression();

        EntityCollection currencies = service.RetrieveMultiple(query);

        foreach (Entity e in currencies.Entities)
        {
            if (e.Attributes.Contains("currencyname"))
            {
                if (e.Attributes["currencyname"].ToString() == "Euro")
                    result = e.Attributes["transactioncurrencyid"].ToString();
            }
        }

        return result;
    }