Search code examples
c#dynamics-crm-2011dynamics-crmcrmfetchxml

CRM 2011: Fetch Error


I have an error with a script that contains multiple fetch in crm 2011... the error is that the key dosent exist & is coming from:

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

if no recordes exist with the condition it fails rather then just pass & return 0 any ideas?

i do declare

decimal TotalDed = 0;
decimal TotalCre = 0;

The code:

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

Thanks :)


Solution

  • Try changing your foreach loops

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

    You need to test whether the value is there before trying to cast is. The error you are getting is a generic .Net one (more info here).

    If no value is found in the field for a particular record, CRM will not include that property and so it will be missing in the collection.

    You are also using = and not +=. This mean the total is the value of the last record, rather than a sum.

    Consider the following:

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

    would output 4

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

    would output 10