locked
Getting errors in plugin when recordset is empty RRS feed

  • Question

  • Just updated code and I'm getting an error when the FetchXML query returns no records. 

    This is what I have:

                    //Start A
                    string estimatedvalue_sum = string.Format(@" 
                <fetch distinct='false' mapping='logical' aggregate='true'> 
                    <entity name='opportunity'> 
                        <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum' /> 
                        <filter type='and'>
                            <condition attribute='statecode' operator='eq' value='2' />
                            <condition attribute='new_servicecontract' operator='eq' value='{0}' uiname='' uitype='' />
                        </filter>
                    </entity>
                </fetch>", a.Id);
    
                    EntityCollection estimatedvalue_sum_result = service.RetrieveMultiple(new FetchExpression(estimatedvalue_sum));
    
                    if (estimatedvalue_sum_result != null)
                    {
                        foreach (var c in estimatedvalue_sum_result.Entities)
                        {
                            totalAmount = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value;
                        }
    
                        //updating the field on the account
                        Entity acc = new Entity("wse_servicecost");
                        acc.Id = a.Id;
                        acc.Attributes.Add("new_caseanalysisopenbalance", new Money(totalAmount));
                        service.Update(acc);
    
                    }

    Does anyone know how to correct this?

     


    Wednesday, November 20, 2013 6:54 PM

All replies

  • Try to replace if condition like below and test

    if(estimatedvalue_sum_result.Entities.Count>0){}


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, November 20, 2013 7:50 PM
    Moderator
  • Mahendar, I tried the above but I get the same error. The screenshot shows more. Essentially when the recordset is empty I get that error.

    Only when the FetchXML query returns a result, it seems to work.

    Wednesday, November 20, 2013 8:03 PM
  • This error comes when you try to access some property which is not available in propertybag.

    so change your code to validate value before using it like below

    if(c.Contains("estimatedvalue_sum") && c["estimatedvalue_sum"].ToString()!=null)

    {

     totalAmount = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value;
    }

    and do the check for totalAmount as well before setting it in caseanalysisopenbalance.


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, November 20, 2013 8:26 PM
    Moderator