protected override void ExecuteWorkflowLogic() { var record1 = ConvertToEntityReference(Record1Id.Get(Context.ExecutionContext)); var record2 = ConvertToEntityReference(Record2Id.Get(Context.ExecutionContext)); var record2Query = new QueryByAttribute(record2.LogicalName) { ColumnSet = new ColumnSet(false) }; record2Query.AddAttributeValue($"{record2.LogicalName}id", record2.Id); var relationship = new Relationship(RelationshipName.Get(Context.ExecutionContext)); var retrieveRequest = new RetrieveRequest() { ColumnSet = new ColumnSet(false), Target = record1, RelatedEntitiesQuery = new RelationshipQueryCollection { { relationship, record2Query } } }; var retrieveResponse = (RetrieveResponse)Context.SystemService.Execute(retrieveRequest); var isRelationshipExists = retrieveResponse.Entity.RelatedEntities[relationship].Entities.Count != 0; RelationshipExists.Set(Context.ExecutionContext, isRelationshipExists); }
private ICollection <Guid> GatherKeysInternal(CodeActivityContext executionContext, string primaryAttribute, string secondaryEntity, string secondaryAttribute, string intersection) { var query = new QueryExpression(); var secondaryToIntersection = new LinkEntity(); var intersectionToPrimary = new LinkEntity(); var primaryCondition = new ConditionExpression(); // Chain all links query.EntityName = secondaryEntity; query.LinkEntities.Add(secondaryToIntersection); secondaryToIntersection.LinkEntities.Add(intersectionToPrimary); intersectionToPrimary.LinkCriteria.Conditions.Add(primaryCondition); // First link //secondaryToIntersection.LinkToEntityName = intersection; //secondaryToIntersection.LinkFromAttributeName = //secondaryToIntersection.LinkToAttributeName = secondaryAttribute; // First link -- Modified code secondaryToIntersection.LinkToEntityName = intersection; string relationName = RelationshipName.Get(executionContext); if (relationName == "listcontact_association") { secondaryToIntersection.LinkFromAttributeName = "contactid"; secondaryToIntersection.LinkToAttributeName = secondaryAttribute; } else { secondaryToIntersection.LinkFromAttributeName = secondaryToIntersection.LinkToAttributeName = secondaryAttribute; } // Second link intersectionToPrimary.LinkToEntityName = this.CurrentEntityName; intersectionToPrimary.LinkFromAttributeName = intersectionToPrimary.LinkToAttributeName = primaryAttribute; // Condition primaryCondition.AttributeName = primaryAttribute; primaryCondition.Operator = ConditionOperator.Equal; primaryCondition.Values.Add(this.CurrentRecordId.ToString()); var retrieveRequest = new RetrieveMultipleRequest() { Query = query }; var list = new List <Guid>(); var retrieveResponse = (RetrieveMultipleResponse)this.GetService(executionContext).Execute(retrieveRequest); foreach (var entity in retrieveResponse.EntityCollection.Entities) { list.Add(entity.Id); } return(list); }
protected override void Execute(CodeActivityContext executionContext) { // Extract the tracing service ITracingService tracingService = executionContext.GetExtension <ITracingService>(); try { //Create the context IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); DynamicUrlParser parser = new DynamicUrlParser(); EntityReference primaryEntity = parser.ConvertToEntityReference(service, RecordUrl.Get <string>(executionContext)); string relationshipName = RelationshipName.Get <string>(executionContext); string childEntityName = null, childEntityAttribute = null; if (relationshipName.Contains(';')) { string[] relationshipNames = relationshipName.Split(';'); foreach (string rel in relationshipNames) { OneToManyRelationshipMetadata oneToNRelationship = RetrieveRelationshipInfo(service, primaryEntity, relationshipName); if (oneToNRelationship != null) { childEntityName = oneToNRelationship.ReferencingEntity; childEntityAttribute = oneToNRelationship.ReferencingAttribute; RetrieveAndUpdateRelatedRecords(service, primaryEntity, childEntityName, childEntityAttribute, StateCode.Get <int>(executionContext), StatusCode.Get <int>(executionContext)); } } } else { OneToManyRelationshipMetadata oneToNRelationship = RetrieveRelationshipInfo(service, primaryEntity, relationshipName); if (oneToNRelationship != null) { childEntityName = oneToNRelationship.ReferencingEntity; childEntityAttribute = oneToNRelationship.ReferencingAttribute; RetrieveAndUpdateRelatedRecords(service, primaryEntity, childEntityName, childEntityAttribute, StateCode.Get <int>(executionContext), StatusCode.Get <int>(executionContext)); } } } catch (FaultException <OrganizationServiceFault> ex) { throw new Exception("XrmWorkflowTools.SetStateChildRecords: " + ex.Message); } }
protected override void Execute(System.Activities.CodeActivityContext context) { var workflowContext = context.GetExtension <IWorkflowContext>(); var service = this.RetrieveOrganizationService(context); QueryResult queryResult = ExecuteQueryForRecords(context); Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest metadataRequest = new Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest() { LogicalName = workflowContext.PrimaryEntityName, EntityFilters = EntityFilters.Relationships }; var metadataResponse = service.Execute(metadataRequest) as Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse; var relationship = metadataResponse.EntityMetadata.ManyToManyRelationships.FirstOrDefault(m => m.SchemaName.Equals(RelationshipName.Get(context), StringComparison.InvariantCultureIgnoreCase) || m.IntersectEntityName.Equals(RelationshipName.Get(context), StringComparison.InvariantCultureIgnoreCase)); if (relationship == null) { throw new Exception($"Entity '{workflowContext.PrimaryEntityName}' does not have relationship with schema name or relationship entity name '{RelationshipName.Get(context)}'"); } QueryExpression qe = new QueryExpression(relationship.IntersectEntityName); qe.Criteria.AddCondition(relationship.Entity1IntersectAttribute, ConditionOperator.Equal, workflowContext.PrimaryEntityId); qe.ColumnSet = new ColumnSet(relationship.Entity2IntersectAttribute); var alreadyAssociated = service.RetrieveMultiple(qe).Entities.Select(e => (Guid)e[relationship.Entity2IntersectAttribute]); var delta = queryResult.RecordIds.Except(alreadyAssociated).Select(id => new EntityReference(relationship.Entity2LogicalName, id)).ToList(); service.Associate(workflowContext.PrimaryEntityName, workflowContext.PrimaryEntityId, new Relationship(relationship.SchemaName), new EntityReferenceCollection(delta)); NumberOfRelationshipChanges.Set(context, delta.Count); }
protected override void ExecuteWorkflowLogic() { List<Entity> records; var recordUrl = Record.Get(Context.ExecutionContext); if (!string.IsNullOrEmpty(recordUrl)) { var record = ConvertToEntityReference(Record.Get(Context.ExecutionContext)); var relationshipName = RelationshipName.Get(Context.ExecutionContext); if (string.IsNullOrEmpty(relationshipName)) { throw new InvalidPluginExecutionException("Reationship Name Parameter is empty!"); } var relationship = new Relationship(relationshipName); var retrieveEntityRequest = new RetrieveEntityRequest() { LogicalName = record.LogicalName, EntityFilters = EntityFilters.Relationships, RetrieveAsIfPublished = true }; var retrieveEntityResponse = (RetrieveEntityResponse) Context.SystemService.Execute(retrieveEntityRequest); string childEntityName = null; var ntonRelationship = retrieveEntityResponse.EntityMetadata.ManyToManyRelationships.FirstOrDefault( r => r.SchemaName == relationshipName); if (ntonRelationship != null) { childEntityName = ntonRelationship.Entity1LogicalName == record.LogicalName ? ntonRelationship.Entity2LogicalName : ntonRelationship.Entity1LogicalName; } else { var onetonRelationship = retrieveEntityResponse.EntityMetadata.OneToManyRelationships.FirstOrDefault( r => r.SchemaName == relationshipName); if (onetonRelationship == null) throw new Exception( $"Relationship {relationshipName} is not available for {record.LogicalName} entity"); childEntityName = onetonRelationship.ReferencingEntity; } var childRecordsFetchXml = $@" <fetch> <entity name='{childEntityName}'> <attribute name='{childEntityName}id' />"; var additionalConditions = AdditionalFilterArgument.Get(Context.ExecutionContext); if (!string.IsNullOrEmpty(additionalConditions)) childRecordsFetchXml += $"<filter type='and'>{additionalConditions.Replace('"', '\'')}</filter>"; childRecordsFetchXml += @" </entity> </fetch>"; var retrieveRequest = new RetrieveRequest() { ColumnSet = new ColumnSet(false), Target = record, RelatedEntitiesQuery = new RelationshipQueryCollection { {relationship, new FetchExpression(childRecordsFetchXml)} } }; var retrieveResponse = (RetrieveResponse) Context.UserService.Execute(retrieveRequest); records = retrieveResponse.Entity.RelatedEntities[relationship].Entities.ToList(); } else { var publicView = PublicView.Get(Context.ExecutionContext); var privateView = PrivateView.Get(Context.ExecutionContext); var fetchXml = FetchXmlQuery.Get(Context.ExecutionContext); if (publicView == null && privateView == null && fetchXml == null) throw new InvalidPluginExecutionException("One of 'Record Reference'/'Relationship Name', 'Public View', 'Private View' or 'Fetch Xml Query' inputs has to be populated!"); if (publicView != null) { fetchXml = Context.SystemService.Retrieve(publicView.LogicalName, publicView.Id, new ColumnSet("fetchxml")).GetAttributeValue<string>("fetchxml"); } else if (privateView != null) { fetchXml = Context.SystemService.Retrieve(privateView.LogicalName, privateView.Id, new ColumnSet("fetchxml")).GetAttributeValue<string>("fetchxml"); } records = QueryWithPaging(new FetchExpression(fetchXml)); } var isContinueOnError = IsContinueOnError.Get(Context.ExecutionContext); foreach (var childRecord in records) { try { var recordForOperation = new Entity(childRecord.LogicalName, childRecord.Id); PerformOperation(recordForOperation); } catch { if (!isContinueOnError || Context.IsSyncMode) throw; } } }
protected override void ExecuteWorkflowLogic() { var record = ConvertToEntityReference(Record.Get(Context.ExecutionContext)); var relationshipName = RelationshipName.Get(Context.ExecutionContext); var relationship = new Relationship(relationshipName); var retrieveEntityRequest = new RetrieveEntityRequest() { LogicalName = record.LogicalName, EntityFilters = EntityFilters.Relationships, RetrieveAsIfPublished = true }; var retrieveEntityResponse = (RetrieveEntityResponse) Context.SystemService.Execute(retrieveEntityRequest); string childEntityName = null; var ntonRelationship = retrieveEntityResponse.EntityMetadata.ManyToManyRelationships.FirstOrDefault( r => r.SchemaName == relationshipName); if (ntonRelationship != null) { childEntityName = ntonRelationship.Entity1LogicalName == record.LogicalName ? ntonRelationship.Entity2LogicalName : ntonRelationship.Entity1LogicalName; } else { var onetonRelationship = retrieveEntityResponse.EntityMetadata.OneToManyRelationships.FirstOrDefault( r => r.SchemaName == relationshipName); if (onetonRelationship == null) throw new Exception($"Relationship {relationshipName} is not available for {record.LogicalName} entity"); childEntityName = onetonRelationship.ReferencingEntity; } var childRecordsFetchXml = $@" <fetch> <entity name='{childEntityName}'> <attribute name='{childEntityName}id' />"; var additionalConditions = AdditionalFilterArgument.Get(Context.ExecutionContext); if (!string.IsNullOrEmpty(additionalConditions)) childRecordsFetchXml += $"<filter type='and'>{additionalConditions.Replace('"', '\'')}</filter>"; childRecordsFetchXml += @" </entity> </fetch>"; var retrieveRequest = new RetrieveRequest() { ColumnSet = new ColumnSet(false), Target = record, RelatedEntitiesQuery = new RelationshipQueryCollection { {relationship, new FetchExpression(childRecordsFetchXml)} } }; var retrieveResponse = (RetrieveResponse) Context.UserService.Execute(retrieveRequest); var childRecords = retrieveResponse.Entity.RelatedEntities[relationship].Entities.ToList(); foreach (var childRecord in childRecords) { PerformRelationshipOperation(childRecord); } }