简体   繁体   中英

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

As the title suggests, I do not know how to convert entity.attributes ["subgrid"] as a list of entities, on which to run the multiretrieve:

My code for now:

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

As you are writing code in a custom workflow assembly - the entity at this point does not know anything about the form it is being called from, and does not have a "Subgrid" property to allow you to access the related records.

You will need to do a sperate query to retrieve related contacts (as an example) using the target entity's "accountid" property to relate to the contacts' "parentcustomerid" property.

Assume you are looking for a method to get all associated records for a particular record.

If that is the case, I would have written something like this. Hope that helps.

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

In that based on the role of the other entity change the Primary Entity Role between Referencing and Referenced.

Hope that helps. Let me know if my assumption is wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM