Related activity records

纵然是瞬间 提交于 2019-12-12 01:56:26

问题


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".

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.

I was wondering if FetchExpression + last() could do it, but right now I haven't found the proper solution...

Thanks in advance !

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

Is that correct?


回答1:


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


来源:https://stackoverflow.com/questions/8537096/related-activity-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!