2017-05-10 63 views
1

我有一个实体有一个或多个Money-Field。因此,它还有一个transactioncurrencyid -field,它指定记录的货币。通过SDK创建记录时设置货币

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

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

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

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

所以我的问题:

奇怪的是,当我为钱场(即使我不告诉它使用何种货币),如设置一个值,这不会发生是:
有没有一种方法来设置一个记录的货币通过SDK创建时没有任何填充的默认值Money -Attributes?

回答

2

曾经有过这个问题,起初很混乱,因为它似乎没有模式,但是是的,手动创建的记录会很好,但是由Web门户创建的记录会将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; 
    } 
+0

你好,感谢您的回答!我知道我可以自己设置,我只是想知道是否有什么方法使SDK在浏览器中创建记录时像CRM一样行事。 – nozzleman

+0

我相信这是一个实际的错误(我报告,目前正在调查)。不过,我在此期间刚刚寻求类似的解决方案。 – nozzleman

+0

这不是预期的行为?由于crm支持多种货币,我们必须设置货币我们需要和crm设置基础值..? –