CRM工作流程:如何从子网格中获取所有记录作为实体列表并执行多次检索

时间:2018-07-23 15:24:36

标签: c# dynamics-crm-2011 workflow crm

正如标题所示,我不知道如何将entity.attributes [“ subgrid”]转换为要在其上运行multiretrieve的实体列表:

我现在的代码:

protected override void Execute(CodeActivityContext executionContext)
    {
        ITracingService tracingService = executionContext.GetExtension<ITracingService>();
        IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
        IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

        var entity = (Entity)context.InputParameters["Target"];

        if (entity.LogicalName != "account")
        {
            return;
        }
        var currentAccountId = entity.Id;
        try
        {
            if (!entity.Contains("Subgrid"))
            {
                return;
            }
            var itemsOnSubgrid = entity.Attributes["Subgrid"];
            if(itemsOnSubgrid == null)
            {
                return;
            }
            else
            {
                //if subgrid exist and is not null
                //List of entities needed
            }
        }
        catch (Exception ex)
        {
            tracingService.Trace("MyWorkflow: {0}", ex.ToString());
            throw new InvalidPluginExecutionException(ex.Message);
        }
    }

2 个答案:

答案 0 :(得分:0)

在自定义工作流程序集中编写代码时-实体此时不知道从其调用表单的任何信息,并且不具有“ Subgrid”属性以允许您访问相关记录。

您将需要使用目标实体的“ accountid”属性和联系人的“ parentcustomerid”属性进行关联查询,以检索相关的联系人(例如)。

答案 1 :(得分:0)

假设您正在寻找一种获取特定记录的所有关联记录的方法。

如果是这样,我会写这样的东西。希望有帮助。

private EntityCollection GetAssociatedRecords(string relationshipName, string relatedEntityName, string entityName, Guid entityId,OrganizationService service)
        {
            EntityCollection result = null;
            try
            {
                QueryExpression query = new QueryExpression();
                query.EntityName = relatedEntityName;
                query.ColumnSet = new ColumnSet(false);
                Relationship relationship = new Relationship();
                relationship.SchemaName = relationshipName;
                relationship.PrimaryEntityRole = EntityRole.Referencing;
                RelationshipQueryCollection relatedEntity = new RelationshipQueryCollection();
                relatedEntity.Add(relationship, query);
                RetrieveRequest request = new RetrieveRequest();
                request.RelatedEntitiesQuery = relatedEntity;
                request.ColumnSet = new ColumnSet(true);

                request.Target = new EntityReference
                {
                    Id = entityId,
                    LogicalName = entityName
                };
                RetrieveResponse response = (RetrieveResponse)service.Execute(request);
                RelatedEntityCollection relatedEntityCollection = response.Entity.RelatedEntities;
                if (relatedEntityCollection.Count > 0)
                {
                    if (relatedEntityCollection.Values.Count > 0)
                    {
                        result = (EntityCollection)relatedEntityCollection.Values.ElementAt(0);
                    }
                }
            }
            catch (Exception exception)
            {
                throw exception;  
            }
            return result;
        }

根据其他实体的角色,在“引用”和“被引用”之间更改“主要实体”角色。

希望有帮助。让我知道我的假设是否错误。