[英]How to retrieve all Opportunity Products for the specific Opportunity through c#?
I have the Opportunity as shown in the below image: 我有机会,如下图所示:
Yesterday, I posted a question on how to create the Opportunity Products (Motor Products) & Dave provided me an answer on how to achieve this. 昨天,我发布了一个有关如何创建机会产品(汽车产品)的问题,而Dave为我提供了有关如何实现这一目标的答案 。
Now, my requirement has been extended to delete these existing Motor Products & add new products. 现在,我的要求已得到扩展,可以删除这些现有的电机产品并添加新产品。
I'm thinking to do this by first retrieving all the relative Motor Products from this opportunity. 我正在考虑通过首先从此机会中检索所有相关的汽车产品来做到这一点。
For creating Opportunity Product I used the below code: 为了创建机会产品,我使用了以下代码:
var opportunityProduct = new Entity(entityMotorName);
opportunityProduct["tmeic_opportunitymotorproductid"] = new EntityReference("opportunity", Guid("opportunityid"));
var opportunityProductId = crmService.Create(opportunityProduct);
But, I'm stuck here for retrieing these Motor Products. 但是,我一直在这里报废这些电机产品。 Once I get the Motor Products which is related to this opportunity I can use the below query.
一旦获得了与此机会相关的电机产品,就可以使用以下查询。
crmService.Delete(entityName,Guid);
Note: my opportunity has opportunityid but no tmeic_opportunitymotorproductid & my Motor Product (opportunityproduct) doesn't have opportunityid but has tmeic_opportunitymotorproductid. 注意:我的机会没有机会ID,但没有tmeic_opportunitymotorproductid,而我的汽车产品(机会产品)没有机会ID,但具有tmeic_opportunitymotorproductid。
Only problem is how to retrieve these Motor Products? 唯一的问题是如何检索这些电机产品?
Here's one way to do this: 这是执行此操作的一种方法:
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Collections.Generic;
using System.Linq;
class App
{
private IOrganizationService svc;
public App(IOrganizationService svc)
{
this.svc = svc;
}
public void Run()
{
var list = OppProducts(svc, new Guid("628CF01A-AED1-E411-80EF-C4346BAC7BE8"));
DeleteList(svc, list);
}
public List<Entity> OppProducts(IOrganizationService svc, Guid OppId)
{
var query = new QueryExpression
{
EntityName = "opportunityproduct",
ColumnSet = new ColumnSet("tmeic_opportunitymotorproductid", "opportunityproductid"),
Criteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = "tmeic_opportunitymotorproductid",
Operator = ConditionOperator.Equal,
Values = { OppId }
}
}
}
};
var result = svc.RetrieveMultiple(query);
return result.Entities.ToList();
}
public void DeleteList(IOrganizationService svc, List<Entity> list)
{
list.ForEach(e => svc.Delete(e.LogicalName, e.Id));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.