0

crm 2011 で複数のフェッチを含むスクリプトでエラーが発生しました...エラーは、キーが存在しないことと、次のものから来ていることです。

<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 にはそのプロパティが含まれないため、コレクションに含まれません。

=and notも使用してい+=ます。これは、合計が合計ではなく、最後のレコードの値であることを意味します。

次の点を考慮してください。

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") ? ((Decimal)((AliasedValue)c["cre_sum"]).Value): 0;

そのため、値が含まれているかどうかを確認し、含まれている場合は合計を返し、そうでない場合は 0 を返します。

両方のループに対してこれを行います。

お役に立てれば !!!

于 2013-01-30T18:56:48.213 に答える