2017-05-10 65 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設置基礎值..? –