0

我在 crm 2011 中包含多个 fetch 的脚本出现错误...错误是密钥存在并且来自:

<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);
                                }

谢谢 :)

4

3 回答 3

2

尝试改变你的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

于 2013-01-28T12:10:07.310 回答
0

那里似乎有一个错字:

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

应该

operator='eq' label='Credit'
               ^^^^^
于 2013-01-28T11:11:36.223 回答
0

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

例如,

TotalCre =c.Attributes.Contains("cre_sum") ? ((十进制)((AliasedValue)c["cre_sum"]).Value): 0;

所以它会检查它是否包含任何值,如果是,它将返回总和,否则返回 0。

对两个循环都这样做。

希望这可以帮助 !!!

于 2013-01-30T18:56:48.213 回答