相关活动记录 [英] Related activity records
问题描述
这就是问题:我创建了一个具有一对多的自定义实体"new_ligneContrat" 与另一个自定义实体"new_produit"的关系.
So here is the thing : I created a custom entity "new_ligneContrat" which has a one-to-many relationship with another custom entity "new_produit".
这里的目标是:创建新记录"new_ligneContrat"时,必须先获取"new_ligneContrat"的"new_produit"记录.该插件建立在"new_ligneContrat"上.
Here is the goal: when a new record of "new_ligneContrat" is created, it has to get the "new_produit" records of the "new_ligneContrat" before it. The plugin is built on "new_ligneContrat.
我想知道FetchExpression + last()是否可以做到,但是现在我还没有找到合适的解决方案...
I was wondering if FetchExpression + last() could do it, but right now I haven't found the proper solution...
提前谢谢!
Edit1::我们决定建立n对n的关系,所以我做了以下事情:
Edit1 : We decided to go for a n to n relationship, so I did the following:
QueryExpression query = new QueryExpression();
query.EntityName = "new_produit";
query.ColumnSet = new ColumnSet("new_produitid");
Relationship relationship = new Relationship();
relationship.SchemaName = "new_new_lignecontrat_new_produit";
RelationshipQueryCollection relatedEntity = new RelationshipQueryCollection();
relatedEntity.Add(relationship, query);
RetrieveRequest request = new RetrieveRequest();
request.RelatedEntitiesQuery = relatedEntity;
request.ColumnSet = new ColumnSet("new_lignecontratid");
request.Target = new EntityReference
{
Id = first.Id,
LogicalName = first.LogicalName
};
RetrieveResponse response = (RetrieveResponse)service.Execute(request);
if (((DataCollection<Relationship, EntityCollection>)(((RelatedEntityCollection)(response.Entity.RelatedEntities)))).Contains(new
> Relationship("new_new_lignecontrat_new_produit")) &&
> ((DataCollection<Relationship,
> EntityCollection>)(((RelatedEntityCollection)(response.Entity.RelatedEntities))))[new
> Relationship("new_new_lignecontrat_new_produit")].Entities.Count > 0)
> {
> response.Results.Remove("new_produitid");
> response["new_lignecontratid"] = new EntityReference(target.LogicalName, target.Id);
对吗?
推荐答案
根据您的插件运行在插件执行的哪个阶段(预验证,操作前/后操作),一种选择是执行LINQ查询这取决于当前时间或新new_ligneContrat
记录的CreatedOn
属性,并调用First
方法以获取最新的new_produit
记录.我在下面提供了一个样机.
Depending on which stage of the plugin execution your plugin is running in (pre-validation, pre/post-operation), one option is to execute a LINQ query that's dependent on the current time or the CreatedOn
property of the new new_ligneContrat
record and call the First
method to get the latest new_produit
record. I've included a mockup below.
DateTime d = DateTime.Now;
// or...DateTime d = targetEntity.Attributes["CreatedOn"].Value;
var usersSettings = (from lc in osc.CreateQuery<new_ligneContrat>()
join p in osc.CreateQuery<new_produit>() on lc.new_produitId equals p.new_produitId.Id
where lc.CreatedOn < d
orderby lc.CreatedOn descending
select p).First();
这篇关于相关活动记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!