您好,我正在尝试为 ms dynamics crm 2011 编写插件。该插件从合同行中获取相关记录,然后应该为发票行实体创建记录。如果 fetchxml 只返回一条记录,则代码运行完美,但是如果它为具有多个合同行的合同循环,则会引发以下错误:
业务流程错误已添加具有相同键的项目
我想我需要为每条记录提供一个唯一的 ID,或者请查看下面的代码。
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);
//invoiceline.Attributes.Add("invoicedetailid", invoiceid);
//add too database
service.Create(invoiceline);
tracingService.Trace("Created:" + title + " record");
}