简体   繁体   English

Dynamics CRM 2016 c#使用尚不存在的实体的ID

[英]Dynamics CRM 2016 c# use id of not yet existing entity

for my project, I have to create multiple Quotes and add products to it. 对于我的项目,我必须创建多个报价并向其中添加产品。
For performance reasons (about 5000 quotes) I am using "ExecuteMultipleRequest()". 出于性能原因(大约5000个引号),我使用“ ExecuteMultipleRequest()”。

This is about what I've got: 这是关于我所拥有的:

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 });

My problem is the quote.Id. 我的问题是quote.Id。 I know it is empty until the server processes the request and creates the quote. 我知道它是空的,直到服务器处理请求并创建报价。

Is there a way to tell the server it should use the quotes new id for the quotedetail? 有没有办法告诉服务器它应该使用quotes new id作为quotedetail?
Or is my only way to create the quote itself and then create all details? 还是我创建报价单然后创建所有详细信息的唯一方法?
What can I do to increase performance if I have to do it this way? 如果必须这样做,该怎么做才能提高性能?

Instead of sending CreateRequests explicity, you could change your code to use the OrganizationServiceContext , which locally tracks changes to objects before submitting them to CRM. 您可以更改代码以使用OrganizationServiceContext ,而不是显式发送CreateRequests,它可以在将对象的更改提交到CRM之前本地跟踪对象的更改。

When using the OrganizationServiceContext, you can use AddRelatedObject to both add an object and link it to another one: 使用OrganizationServiceContext时,可以使用AddRelatedObject添加一个对象并将其链接到另一个对象:

Adds a related entity to the OrganizationServiceContext and creates the link that defines the relationship between the two entities in a single request. 将一个相关实体添加到OrganizationServiceContext并创建一个链接,该链接定义单个请求中两个实体之间的关系。

Alternatively you could manually call AddObject and AddLink . 或者,您可以手动调用AddObjectAddLink

You final code would look similar to the following: 您的最终代码将类似于以下内容:

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.

相关问题 Dynamics CRM 2016 以编程方式创建和限定潜在客户 C# - Dynamics CRM 2016 Create and Qualify a Lead Programmatically C# 在PREM Dynamics CRM 2016 JavaScript上相当于我的C#QueryExpression - On PREM Dynamics CRM 2016 JavaScript equivalent of my C# QueryExpression c#-动态CRM在线插件-使用字段值填充相关实体的属性 - c# - dynamics crm online plugin - use field value to populate attribute of related entity 读取数据集合<Entity>来自使用 C# 的 Dynamics CRM - Reading DataCollection<Entity> from Dynamics CRM using C# 使用QueryExpression选择一个实体? C#Dynamics CRM Online 2015年 - Select an Entity using QueryExpression? C# Dynamics CRM Online 2015 Dynamics 365 (CRM) 9.1 版在线 C# 代码错误:实体 ID 必须与属性包中设置的值相同 - Dynamics 365 (CRM) Version 9.1 online C# code Error: Entity Id must be the same as the value set in property bag Microsoft Dynamics CRM 2016年 - Microsoft Dynamics CRM 2016 如何使用MS动态CRM 2016中的c#检索部分,选项卡? - How can I retrieve sections, tabs by using c# from MS dynamics CRM 2016? 如何构建查询以获取Dynamics CRM 2016 C#中不存在用户X的安全角色 - How to build a query to get a security role where user X does not exists in Dynamics CRM 2016 C# How to Create Case in Microsoft Dynamics CRM 2016 (Version 8) using webservices or REST API in JAVA or C#? - How to Create Case in Microsoft Dynamics CRM 2016 (Version 8) using webservices or REST API in JAVA or C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM