如何在工作流 C# 中检索实体上的活动业务流程?

问题描述 投票:0回答:1

我有一个工作流程,应最终确定/完成与主导实体关联的业务流程,我如何检索活动的业务流程并获取其 ID? 在输入中,我发送entityName(领导)和entityid(领导实体的一些id)

namespace BPF.Workflows
{
    public class Finalize : CodeActivity
    {
        [RequiredArgument]
        [Input("EntityName")]
        public InArgument<string> EntityName { get; set; }
        [RequiredArgument]
        [Input("EntityId")]
        public InArgument<string> EntityId { get; set; }

        protected override void Execute(CodeActivityContext executionContext)

        {
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);


            try
            {
                string leadId = EntityId.Get<string>(executionContext);
                string leadName = EntityName.Get<string>(executionContext);
                EntityReference leadRef = new EntityReference(EntityName.Get<string>(executionContext), new Guid(leadId));

                SetOrderStatusToFulfilled(leadId, service);
                FinalizeActiveBPF(leadId, service);


            }
            catch (Exception ex)
            {
                throw new Exception("Exception.Execute():", ex);
            }

        }

        private void FinalizeActiveBPF(string leadId, IOrganizationService service)
        {
            try
            {
                      //here should be the code
            }
            catch (Exception e)
            {
                throw new Exception("Exception.FinalizeActiveBPF():", e);
            }
        }



        private void SetOrderStatusToFulfilled(string salesOrderId, IOrganizationService service)
        {
            try
            {
                Entity salesOrder = new Entity("lead");
                salesOrder.Id = new Guid(salesOrderId);
                salesOrder["statecode"] = new OptionSetValue(3);
                salesOrder["statuscode"] = new OptionSetValue(100001);

                service.Update(salesOrder);
            }
            catch (Exception e)
            {
                throw new Exception("Exception.SetOrderStatusToFulfilled():", e);
            }
        }


    }
}

我试过了,但这不起作用:

                Entity activeBPF = UserWs.Retrieve(leadName, new Guid(leadId), new ColumnSet("processid"));

                Guid activebpfProcessId = activeBPF.GetAttributeValue<Guid>("processid");

c# microsoft-dynamics crm powerapps
1个回答
0
投票

因此,让我们使用 C# 检索与工作流程中的主导实体关联的活动业务流程 (BPF)。我们会一步一步走下去

  1. 检索实体记录:首先,我们将使用

    IOrganizationService
    Retrieve
    方法来获取主要实体记录。这种方法可以帮助我们获得必要的详细信息。

  2. 访问活动BPF ID:一旦我们有了领导实体,我们就可以访问“processid”属性,它存储活动BPF的ID。

以下是如何更新

FinalizeActiveBPF
方法来实现此目的:

private void FinalizeActiveBPF(string leadId, IOrganizationService service)
{
    try
    {
        // Step 1: Retrieve the lead entity
        ColumnSet columns = new ColumnSet("processid");
        Entity leadEntity = service.Retrieve("lead", new Guid(leadId), columns);

        // Step 2: Get the ID of the active BPF
        Guid activeBpfProcessId = leadEntity.GetAttributeValue<Guid>("processid");

        // You've now successfully obtained the ID of the active BPF process.
        // This ID can be used for further actions, like updating BPF stages/status.

        // For BPF completion, additional steps might be necessary
        // involving the SetProcess and ExecuteWorkflowRequest methods.
        // Keep in mind, the exact steps could differ based on your CRM setup.

        // ...
    }
    catch (Exception e)
    {
        // If something goes wrong, handle the exception gracefully
        throw new Exception("Oops! An error occurred while working with the BPF:", e);
    }
}

希望有帮助

© www.soinside.com 2019 - 2024. All rights reserved.