1

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

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

但是,当通过 SDK 创建该实体的记录而不为任何Money-fields 指定值时,transactioncurrencyid-field 保持为空。Money之后,由于未设置货币,任何用户都无法编辑任何字段:

如果货币字段中存在值,则需要货币。选择一种货币,然后重试。

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

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

所以我的问题是:
有没有办法在没有任何填充属性的情况下通过 SDK 创建记录时将记录的货币设置为默认值Money

4

1 回答 1

2

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

因此,我将以下代码添加到创建后插件中,确保始终填充此字段。在此示例中,我专门使用欧元货币,但可以修改检索货币 guid 的方法以返回默认值。

        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;
    }
于 2017-05-10T13:14:33.373 回答