曾经也遇到过这个问题,起初令人困惑,因为它似乎没有模式,但是是的,手动创建的记录会很好,但是由门户网站创建的记录会将 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;
}