簡體   English   中英

CRM工作流程:如何從子網格中獲取所有記錄作為實體列表並執行多次檢索

[英]CRM workflow: How to get all the records from a subgrid as a list of entities and perform multi-retrieve

如標題所示,我不知道如何將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);
        }
    }

在自定義工作流程序集中編寫代碼時-實體此時不知道從其調用表單的任何信息,並且不具有“ Subgrid”屬性以允許您訪問相關記錄。

您將需要使用目標實體的“ accountid”屬性與聯系人的“ parentcustomerid”屬性進行關聯,以進行檢索以檢索相關聯系人(例如)。

假設您正在尋找一種獲取特定記錄的所有關聯記錄的方法。

如果是這樣,我會寫這樣的東西。 希望能有所幫助。

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

在此基礎上,根據另一個實體的角色,在“引用”和“被引用”之間更改“主要實體”角色。

希望能有所幫助。 讓我知道我的假設是否錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM