![](/img/trans.png)
[英]Dynamics CRM 2016 Create and Qualify a Lead Programmatically C#
[英]Dynamics CRM 2016 c# use id of not yet existing entity
對於我的項目,我必須創建多個報價並向其中添加產品。
出於性能原因(大約5000個引號),我使用“ ExecuteMultipleRequest()”。
這是關於我所擁有的:
var quote = new Quote
{
QuoteNumber = "123",
Name = "test123",
PriceLevelId = new EntityReference(PriceLevel.EntityLogicalName, Pricelevel.Id),
CustomerId = new EntityReference(Account.EntityLogicalName, Customer.Id),
};
_requests.Requests.Add(new CreateRequest { Target = quote });
var quoteDetail = new QuoteDetail
{
QuoteId = new EntityReference(Quote.EntityLogicalName, quote.Id),
ProductId = new EntityReference(Product.EntityLogicalName, product.Id),
IsPriceOverridden = true,
PricePerUnit = new Money(20),
Quantity = Convert.ToDecimal(5),
};
_requests.Requests.Add(new CreateRequest { Target = quoteDetail });
我的問題是quote.Id。 我知道它是空的,直到服務器處理請求並創建報價。
有沒有辦法告訴服務器它應該使用quotes new id作為quotedetail?
還是我創建報價單然后創建所有詳細信息的唯一方法?
如果必須這樣做,該怎么做才能提高性能?
您可以更改代碼以使用OrganizationServiceContext ,而不是顯式發送CreateRequests,它可以在將對象的更改提交到CRM之前本地跟蹤對象的更改。
使用OrganizationServiceContext時,可以使用AddRelatedObject添加一個對象並將其鏈接到另一個對象:
將一個相關實體添加到OrganizationServiceContext並創建一個鏈接,該鏈接定義單個請求中兩個實體之間的關系。
您的最終代碼將類似於以下內容:
using (var context = new OrganizationServiceContext(_serviceProxy))
{
var quote = new Quote
{
QuoteNumber = "123",
Name = "test123",
PriceLevelId = new EntityReference(PriceLevel.EntityLogicalName, pricelevel.Id),
CustomerId = new EntityReference(Account.EntityLogicalName, customer.Id),
};
context.AddObject(quote);
var quoteDetail = new QuoteDetail
{
ProductId = new EntityReference(Product.EntityLogicalName, product.Id),
IsPriceOverridden = true,
PricePerUnit = new Money(20),
Quantity = Convert.ToDecimal(5),
};
context.AddRelatedObject(quote, new Relationship("quote_details"), quoteDetail);
context.SaveChanges();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.