1

I'm trying to get a field to update with each line item on the invoice (without over writing what is already there), using a Query Expression to get the data that needs to be used to update the field.

So far I've been able to get this to work just fine when only 1 line item is present. But whenever I test this against multiple line items I get the " The given key was not present in the dictionary." error.

Any help or nudge in the right direction?

    QueryExpression lineitem = new QueryExpression("invoicedetail");
    lineitem.ColumnSet = new ColumnSet("quantity", "productid", "description");
    lineitem.Criteria.AddCondition("invoiceid", ConditionOperator.Equal, invoiceid);

    EntityCollection results = server.RetrieveMultiple(lineitem);
    Invoice.Attributes["aspb_bookmarksandk12educational"] = "Purchases";
    Invoice.Attributes["aspb_bookmarksandk12educational"] += "\n";
    Invoice.Attributes["aspb_bookmarksandk12educational"] += "Product" + "        " + "Quantity";
    Invoice.Attributes["aspb_bookmarksandk12educational"] += "\n";

    foreach (var a in results.Entities)
            {
    string name = a.Attributes["description"].ToString();
    string quantity = a.Attributes["quantity"].ToString();
    Invoice.Attributes["aspb_bookmarksandk12educational"] += " " + name + " ";
    Invoice.Attributes["aspb_bookmarksandk12educational"] += quantity;
    Invoice.Attributes["aspb_bookmarksandk12educational"] += "\n";
    }
Daniel
  • 39
  • 1
  • 8

1 Answers1

1
"The given key was not present in the dictionary."

Suggests the problem lies in the way you are trying to access attribute values and not with the multiple entities returned. When you try get an attribute value, try to check if the attribute exists before reading the value like so:

if (a.Attributes.ContainsKey("description"))
{
    var name = a.Attributes["description"] as string;
}

Or even better use the SDK extension methods to help do the check and return a default value for you like so:

var name = a.GetAttributeValue<string>("description");
var quantity = a.GetAttributeValue<decimal>("quantity");
dynamicallyCRM
  • 2,980
  • 11
  • 16
  • Thanks. I was thinking that same thing after I posted it. I was just looking up the code. I'll check this out and let you know. – Daniel Jun 29 '16 at 18:12
  • Thanks for your help. But I tried both of those (as well as other ways to catch nulls, as well as verifying that the data exists for all the line items) and it's still giving the same error. Works fine on just one line item, fails for multiples. Anything else? I do appreciate the help greatly – Daniel Jun 29 '16 at 18:33
  • Here is more information.I just used a different test record, and it worked for multiple line items without a problem. So it may be related to something else. The original multi line item I used as 10 line items, which is a rarity. Using only 2 worked fine. So now I know it's probably something else and not this snippet of code. Thank you so much for your help. – Daniel Jun 29 '16 at 18:48
  • 1
    Just confirmed. All are working as expected, except that one record. But when I closed it and re-opened it and tested again? It worked. Go figure. Again. Thanks for your help. I've up voted your reply. – Daniel Jun 29 '16 at 18:54