Example #1
0
        protected override void AddField(ref Dictionary <string, object> request, string fieldName)
        {
            var fetchXml = FetchXmlQuery.Get(Context.ExecutionContext);

            var record = Context.UserService.RetrieveMultiple(new FetchExpression(fetchXml)).Entities
                         .FirstOrDefault();

            if (record != null)
            {
                request.Add(fieldName, record.ToEntityReference());
            }
            else if (IsSetToNull.Get(Context.ExecutionContext))
            {
                request.Add(fieldName, null);
            }
        }
        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;
                }
            }
        }