Reputation: 229
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 :)
Upvotes: 0
Views: 744
Reputation: 1
In both the foreach loop you have to check whether that alias cre_sum and ded_sum contains any value...
for example,
TotalCre =c.Attributes.Contains("cre_sum") ? ((Decimal)((AliasedValue)c["cre_sum"]).Value): 0;
so it will check whether it contains any value if yes it will return sum else return 0.
Do this for both loops.
Hope This Helps !!!
Upvotes: 0
Reputation: 6715
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
Upvotes: 2
Reputation: 23300
Looks like there's a typo there:
operator='eq' lable='Credit'
^^^^^
should be
operator='eq' label='Credit'
^^^^^
Upvotes: 0