CRM 2011:获取错误

本文关键字:取错误 获取 2011 CRM | 更新日期: 2023-09-27 17:56:13

我在crm 2011中包含多次提取的脚本出错... 错误是密钥dosent存在并且来自:

<condition attribute='bc_type' operator='eq' lable='Credit' value='948110001' />

如果条件不存在记录,则失败,则只需传递并返回 0有什么想法吗?

我声明

decimal TotalDed = 0;
decimal TotalCre = 0;

代码:

string value_sum = string.Format(@"         
            <fetch distinct='false' mapping='logical' aggregate='true'> 
                <entity name='bc_llbalance'>
                    <attribute name='bc_units' alias='ded_sum' aggregate='sum' />
                       <filter type='and'>
                        <condition attribute='bc_learninglicense' operator='eq' value='{0}' uiname='' uitype='' />
                        <condition attribute='bc_type' operator='eq' lable='Deduction' value='948110000' />
                       </filter>
                </entity>
            </fetch>", a);
                    EntityCollection value_sum_result = service.RetrieveMultiple(new FetchExpression(value_sum));
                    foreach (var b in value_sum_result.Entities)
                    {
                        TotalDed = ((Decimal)((AliasedValue)b["ded_sum"]).Value);
                    }
                        string cre_sum = string.Format(@"         
            <fetch distinct='false' mapping='logical' aggregate='true'> 
                <entity name='bc_llbalance'>
                    <attribute name='bc_units' alias='cre_sum' aggregate='sum' />                      
                        <filter type='and'>
                        <condition attribute='bc_type' operator='eq' lable='Credit' value='948110001' />
                        <condition attribute='bc_learninglicense' operator='eq' value='{0}' uiname='' uitype='' />                        
                        </filter>
                </entity>
            </fetch>", a);
                            EntityCollection cre_sum_result = service.RetrieveMultiple(new FetchExpression(cre_sum));
                                foreach (var c in cre_sum_result.Entities)
                                {
                                    TotalCre = ((Decimal)((AliasedValue)c["cre_sum"]).Value);
                                }

谢谢:)

CRM 2011:获取错误

尝试更改foreach循环

foreach (var c in cre_sum_result.Entities)
{ 
    if(c.Attributes.ContainsKey("cre_sum"))
    {
        TotalCre += ((Decimal)((AliasedValue)c["cre_sum"]).Value);
    }
}

在尝试强制转换之前,您需要测试该值是否存在。您收到的错误是通用的 .Net 错误(更多信息在这里)。

如果在特定记录的字段中找不到任何值,则 CRM 将不包含该属性,因此该属性将在集合中丢失。

您也在使用=而不是+=.这意味着总计是最后一条记录的值,而不是总和。

请考虑以下事项:

var ListOfNumbers = new List<int> { 1, 2, 3 ,4 }
var total = 0;
foreach (var c in ListOfNumbers)
{
    total = c;
}
Console.WriteLine(total.ToString());

将输出4

var ListOfNumbers = new List<int> { 1, 2, 3 ,4 }
var total = 0;
foreach (var c in ListOfNumbers)
{
    total += c;
}
Console.WriteLine(total.ToString());

将输出10

看起来那里有一个错字:

operator='eq' lable='Credit'
               ^^^^^

应该是

operator='eq' label='Credit'
               ^^^^^

在 foreach 循环中,您必须检查该别名是否cre_sum和ded_sum包含任何值......

例如

TotalCre =c.Attributes.Contains("cre_sum") ?((十进制)((别名值)c["cre_sum"])。值):0;

因此,它将

检查它是否包含任何值,如果是,它将返回 sum 否则返回 0。

对两个循环执行此操作。

希望这对!!有所帮助