Crm attribute.contains函数返回意外值

时间:2014-06-16 08:45:24

标签: dynamics-crm-2011

在插件中更新消息名称时,我必须保存十进制和货币类型的字段值。但是当我检查字段是否包含值时,意外地包含函数返回实体集合,其他属性返回类型querybyattribute,即使变量是bool,它也可以得到不同类型的值,如果子句不起作用。是什么导致的?

 if (entity.Attributes.Contains("new_flighthotelreservationid"))
     {
       IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 
       IOrganizationService orgService = factory.CreateOrganizationService(context.UserId);

        bool b = entity.Attributes.Contains("new_poscost");//to check what is returning
        bool c = entity.Attributes.Contains("profittl");

        Decimal posTutarı5 = entity.Attributes.Contains("new_poscost") ? ((Money)entity.Attributes["new_poscost"]).Value : 0m;

        Decimal profitTl5 = entity.Attributes.Contains("new_profittl") ? (Decimal)entity.Attributes["new_profittl"] : 0m;

        Decimal totalAmount5 = entity.Attributes.Contains("new_totalamount") ? ((Money)entity.Attributes["new_totalamount"]).Value : 0m;

1 个答案:

答案 0 :(得分:0)

好的,这是我到目前为止所理解的:

new_flighthotelreservationid类型为lookup,所以:

if (entity.Attributes.Contains("new_flighthotelreservationid"))   //Check if NULL
{
           //So flight reservation is not NULL
           //Below is to parse lookup
EntityReference Resrv = (EntityReference)entity["new_flighthotelreservationid"];

}

else
{
       //Flight reservation is NULL
}

if仅在new_flighthotelreservationid不为空时才会执行。

你也可以parse and check

if (((EntityReference)entity["new_flighthotelreservationid"]).Id != null)
{
//Not NULL
}

因此,将值分配给十进制变量存在问题: 尝试如下:

In case your field is of type decimal

Decimal posTutarı5 = (Decimal)(entity.Attributes.Contains("new_poscost")?entity["new_poscost"] : 0m);

In case your field is of type Currency

Money posTutarı5 = (Money)(entity.Attributes.Contains("new_profittl") ? entity["new_profittl"] : 0m);

以下是获取Money值的另一种方法

Money NewProfit=entity["new_profittl"] as Money;
int retailPrice = NewProfit.Value;