获取当前正在运行的系统作业的systemjob id(AsyncOperation Entity)

时间:2016-02-12 07:58:21

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

我正在编写一个Dynamics CRM 2015插件,该插件由sdk消息步骤“assign of Task”触发,并以异步方式运行。

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

1 个答案:

答案 0 :(得分:0)

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

   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。

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