Dynamics CRM Business Process Error An item with the same key has already been added with plugin RRS feed

  • Question

  • Hello

    I'm trying to write a plugin for ms dynamics crm 2011. The plugin fetches related records from contract line and then is supposed to create records for the invoice line entity. The code runs perfect if there is only one record returned by fetchxml however if it loops around for a contract that has more than one contract line the following error is thrown:

    Business Process Error An item with the same key has already been added

    Im thinking i need a unique id for each record or something please have a look at my code below. Cheers

    Entity invoiceline = new Entity("invoicedetail");
                            foreach (Entity contractdetail in contractdetails.Entities)
                                tracingService.Trace("loop no. " + loopint + "  " + "InvoiceID: " + stringinvoiceid);
                                invoiceline["invoiceid"] = new EntityReference("invoice", invoiceid);
                                //collect data from fetchxml
                                var title = contractdetail.Attributes["title"];
                                decimal quantity = Convert.ToDecimal(contractdetail.Attributes["initialquantity"]);
                                var price = contractdetail.Attributes["price"];
                                bool booleanover = true;
                                //add to invoiceline array
                                invoiceline.Attributes.Add("productdescription", title);
                                invoiceline.Attributes.Add("quantity", quantity);
                                invoiceline.Attributes.Add("priceperunit", price);
                                invoiceline.Attributes.Add("isproductoverridden", booleanover);
                                //add too database
                                tracingService.Trace("Created:" + title + " record");

    Tuesday, March 5, 2013 1:47 PM

All replies

  • the line

    Entity invoiceline = new Entity("invoicedetail");

    need to be inside the foreach

    foreach (Entity contractdetail in contractdetails.Entities)
    	Entity invoiceline = new Entity("invoicedetail");
    	// ... your code here

    Tuesday, March 5, 2013 1:52 PM