locked
SDK Rollup throwing error "Object reference not set to an instance of an object." RRS feed

  • Question

  • Because one of my roll-ups needs more complicated logic, I'm using a plugin rather than the 2015 Rollup field type.

    The plugin is failing on create if there are no records matching the query, or on delete if it's the last record. Both operations are post and sync.

    My error is happening on when I try to access the AliasedValue, which should be 0, because if it wasn't an aggregate there would be 0 records returned.

    Hopefully there's a simple solution to this.

    string fetchSponRev = @"<fetch distinct='false' mapping='logical' aggregate='true'>" +
                        "<entity name='c20_sponsorship'>" +
                            "<attribute name='new_rategbp' aggregate='sum' alias='price_rollup' />" +
                                "<filter type='and'>" +
                                    "<condition attribute='c20_event' operator='eq' value='{" + camId + @"}' />" +
                                    "<condition attribute='c20_sponsorstatus' operator='eq' value='601670000' />" +
                            "</filter>" +
                        "</entity>" +
                "</fetch>";
                            tracer.Trace(fetchSponRev);
    
                            EntityCollection spons = service.RetrieveMultiple(new FetchExpression(fetchSponRev));
                            decimal rate = 0;
                            if (spons.Entities.Count > 0)
                            {
                                tracer.Trace("loop rate");
                                foreach (var c in spons.Entities)
                                {
                                    tracer.Trace("loop rate inner");
                                    if (c.Contains("price_rollup"))
                                        rate = (decimal)c.GetAttributeValue<AliasedValue>("price_rollup").Value;
                                        //rate = (decimal)((AliasedValue)c["price_rollup"]).Value;
                                    else
                                        rate = 0;
    
                                    tracer.Trace("rate: " + rate.ToString());
                                }
                            }

    Tuesday, March 3, 2015 9:41 AM