Dynamics crm 通过SDK创建记录时设置货币
我有一个实体,它有一个或多个Dynamics crm 通过SDK创建记录时设置货币,dynamics-crm,microsoft-dynamics,dynamics-crm-365,Dynamics Crm,Microsoft Dynamics,Dynamics Crm 365,我有一个实体,它有一个或多个Money-字段。因此,它还有一个transactioncurrencyid-字段,用于指定记录的货币 现在,当我在浏览器中创建该实体的记录时,transactioncurrencyid-字段将预先填充系统或用户的默认货币 但是,当通过SDK创建该实体的记录而不为任何Money-字段指定值时,transactioncurrencyid-字段保持为空。此后,由于未设置货币,用户无法编辑任何货币-字段: 如果货币字段中存在值,则需要货币。选择一个 然后重试 奇怪的是,当我
Money
-字段。因此,它还有一个transactioncurrencyid
-字段,用于指定记录的货币
现在,当我在浏览器中创建该实体的记录时,transactioncurrencyid
-字段将预先填充系统或用户的默认货币
但是,当通过SDK创建该实体的记录而不为任何Money
-字段指定值时,transactioncurrencyid
-字段保持为空。此后,由于未设置货币,用户无法编辑任何货币
-字段:
如果货币字段中存在值,则需要货币。选择一个
然后重试
奇怪的是,当我为货币字段设置值时(即使我没有告诉它使用哪种货币),这种情况不会发生,例如:
所以我的问题是:在通过SDK创建记录时,如果没有任何填充的
货币属性,是否有方法将记录的货币设置为默认值?也有过此问题,一开始令人困惑,因为它似乎没有模式,但是,是的,手动创建的记录就可以了,但是,由web门户创建的记录将transactioncurrencyid设置为null(仅当创建中未指定货币字段时)
因此,我在一个post-create插件中添加了以下代码,以确保始终填充此字段。在本例中,我专门使用欧元,但是可以修改检索货币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;
}
}
你好,谢谢你的回答!我知道我可以自己设置,我只是想知道是否有任何方法可以让SDK在浏览器中创建记录时像CRM那样运行。我相信这是一个实际的错误(我报告了,目前正在调查)。然而,我同时也采用了类似的解决方案。这不是预期的行为吗?由于crm支持多种货币,我们必须设置我们所需的货币,并且crm必须设置基础值?
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;
}
}
//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;
}