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?
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
CreatedOnproperty of the newnew_ligneContratrecord and call theFirstmethod to get the latestnew_produitrecord. I’ve included a mockup below.