Quantcast
Channel: CRM Development 論壇
Viewing all articles
Browse latest Browse all 10280

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

$
0
0

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);
                            loopint++;
                            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
                            service.Create(invoiceline);
                            tracingService.Trace("Created:" + title + " record");
                        }


Viewing all articles
Browse latest Browse all 10280

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>