【问题标题】:Get systemjob id of current running system job (AsyncOperation Entity)获取当前正在运行的系统作业的系统作业 ID(AsyncOperation 实体)
【发布时间】:2016-02-12 07:58:21
【问题描述】:

我正在编写一个 Dynamics CRM 2015 插件,它由 sdk 消息步骤“分配任务”触发并异步运行。

Execute(IServiceProvider serviceProvider) 方法中,我想使用QueryExpression 搜索其他当前正在运行的系统作业:

QueryExpression systemjobq = new QueryExpression
{
    EntityName = "asyncoperation",
    ColumnSet = new ColumnSet("name", "createdon", "completedon", "regardingobjectid", "asyncoperationid", "startedon", "messagename", "statuscode"),
    Criteria = new FilterExpression
    {
        Conditions = {
                        new ConditionExpression 
                        {
                            AttributeName = "name",
                            Operator = ConditionOperator.Equal,
                            Values = { "My system jobs name" }
                        }
                     }
    }
};

因为我不想找到“我自己”——当前正在运行的系统作业——我需要找出执行我的插件的当前正在运行的系统作业的asyncoperationid,所以我可以将它包含在我的搜索查询中表达。

如何找到当前正在运行的系统作业的asyncoperationid

【问题讨论】:

    标签: dynamics-crm microsoft-dynamics dynamics-crm-2015


    【解决方案1】:

    如果我理解正确,您希望找到您的异步插件当前正在执行的异步作业的记录。试试下面的扩展方法:

       public static EntityCollection GetCurrentAsyncJob(this IOrganizationService service, ColumnSet cols, Guid EntityId, Guid OwnerId) {
    
                QueryExpression systemjobq = new QueryExpression
                {
                    EntityName = "asyncoperation",
                    ColumnSet = cols,//new ColumnSet("name", "createdon", "completedon", "regardingobjectid", "asyncoperationid", "startedon", "messagename", "statuscode"),
                    Criteria = new FilterExpression
                    {
                        Conditions = 
                        {
                            new ConditionExpression 
                            {
                                AttributeName = "regardingobjectid",
                                Operator = ConditionOperator.Equal,
                                Values = {EntityId}
                            },
                            new ConditionExpression {
                            AttributeName = "statuscode",
                            Operator = ConditionOperator.In,
                            Values = {20}
                            },
                            new ConditionExpression 
                            {
                                AttributeName = "ownerid",
                                Operator = ConditionOperator.Equal,
                                Values = {OwnerId}
                            }
                        }
                    }
                };
                systemjobq.Orders.Add(new OrderExpression { AttributeName = "createdon", OrderType = OrderType.Descending });
                return service.RetrieveMultiple(systemjobq);
            }
    

    您可以检查您是否获得了异步作业的 ID,如下所示,其中 service 是插件中的 IOrganization 服务,taskId 是您的任务 ID,插件当前正在针对该任务运行,并且OwnerId 是插件的当前初始所有者:

         EntityCollection ents = service.GetCurrentAsyncJob(new ColumnSet(new []{"name", "createdon", "completedon", "regardingobjectid", "asyncoperationid", "startedon", "messagename", "statuscode"}), TaskId, OwnerId);
                if (ents.Entities.Count > 0)
                    throw new InvalidPluginExecutionException(ents.Entities.FirstOrDefault().Id.ToString());
                else throw new InvalidPluginExecutionException("M? Really, No?");
    

    这是您从插件上下文中获取插件中当前用户的方式:

    Guid userId = context.InitiatingUserId;
    

    该方法并不完美,如果存在与该实体相关的多个作业,它将返回多个作业,并且同时为同一任务触发分配。我不确定也没有对此进行测试,但我认为只有选择您的插件在同一用户下运行才会有问题。

    注意扩展方法必须放在静态类中。

    注意 2:状态等于 20 表示异步作业仍在运行且 InProgress。

    请尝试看看它是否适合您。

    【讨论】:

    • 您好博扬,谢谢您的回复。事实上,这并不是我要归档的内容。我的意图是找到所有其他当前正在运行的系统作业,不是当前的,我目前正在运行我的代码。我发现你可以从上下文中获取当前正在运行的“线程”的 asyncobjectid 与 @ 987654327@.
    猜你喜欢
    • 1970-01-01
    • 2018-12-25
    • 2011-03-05
    • 2015-08-20
    • 2017-10-31
    • 1970-01-01
    • 2014-08-19
    • 2013-11-16
    • 1970-01-01
    相关资源
    最近更新 更多