在 Dynamics CRM 插件中访问 ProcessId 和 StageId
Access ProcessId and StageId in Dynamics CRM Plugin
我正在使用业务流程在 Campaign 上编写插件。由活动实体中的业务流程创建的字段 ProcessId 和 StageId。我需要为我的插件中的记录检索这些值。
它们不会出现在插件注册工具的步骤图像中。如果我可以在其他字段中填充它们,它们甚至不会出现在 CRM 工作流程中。
关于我如何实现这一点,有没有好的替代方案?
要访问进程和阶段 ID,您可以使用记录的进程 ID 和阶段 ID 字段。我很确定您可以从图像或通过直接读取记录来获取它。此外,您可以重新检查以下文章:
https://deepakexploring.wordpress.com/tag/updating-process-id-in-crm-2013/
您应该能够通过将正确的输入参数传递给此业务流程中的 workflow activity
来检索它。
1) 如果您有这些字符串输入:
[RequiredArgument]
[Input("Process Name")]
public InArgument Process { get; set; }
[RequiredArgument]
[Input("Process Stage Name")]
public InArgument ProcessStage { get; set; }
2) 执行代码Get Process
:
using (var _context= new OrganizationServiceContext(service))
{
// Get the processid using the name provided
var process = (from p in _context.CreateQuery()
where
p.Name == Process.Get(executionContext)
&&
p.StateCode == WorkflowState.Activated
select new Workflow
{WorkflowId = p.WorkflowId}
).FirstOrDefault();
if (process==null)
throw new InvalidPluginExecutionException(string.Format("Process '{0}' not found",Process.Get(executionContext)));
使用提供的名称
获取stage id
var stage = (from s in _context.CreateQuery()
where
s.StageName == ProcessStage.Get(executionContext)
&&
s.ProcessId.Id == process.WorkflowId
select new ProcessStage
{ProcessStageId = s.ProcessStageId}
).FirstOrDefault();
if (stage == null)
throw new InvalidPluginExecutionException(string.Format("Stage '{0}' not found", Process.Get(executionContext)));
您现在可以 Change
Update
检索您的值的阶段 ...
Entity uStage = new Entity(context.PrimaryEntityName);
uStage.Id = context.PrimaryEntityId; //
uStage["stageid"] = stage.ProcessStageId; //retrieved stage
uStage["processid"] = process.WorkflowId; //process id
为什么不直接从您的 IServiceProvider
获取服务并检索字段?
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = CommonPluginLibrary.GetContextFromIServiceProvider(serviceProvider);
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
Entity campaign = service.Retrieve(context.PrimaryEntityId, context.PrimaryEntityName, new ColumnSet("processid", "stageid"));
// ...
// Do your stuff with campaign["processid"], campaign["stageid"]
// ...
}
另一种方法是为 stageid
和 processid
创建两个 shadow 字段(例如 new_stageid
、new_processid
),并使用在 process/stage 更新时触发的 同步工作流 填充这些字段。
然后,您可以在这些 shadow 字段上注册您的插件,因为它们将是您实体的自定义属性。
我正在使用业务流程在 Campaign 上编写插件。由活动实体中的业务流程创建的字段 ProcessId 和 StageId。我需要为我的插件中的记录检索这些值。
它们不会出现在插件注册工具的步骤图像中。如果我可以在其他字段中填充它们,它们甚至不会出现在 CRM 工作流程中。
关于我如何实现这一点,有没有好的替代方案?
要访问进程和阶段 ID,您可以使用记录的进程 ID 和阶段 ID 字段。我很确定您可以从图像或通过直接读取记录来获取它。此外,您可以重新检查以下文章: https://deepakexploring.wordpress.com/tag/updating-process-id-in-crm-2013/
您应该能够通过将正确的输入参数传递给此业务流程中的 workflow activity
来检索它。
1) 如果您有这些字符串输入:
[RequiredArgument]
[Input("Process Name")]
public InArgument Process { get; set; }
[RequiredArgument]
[Input("Process Stage Name")]
public InArgument ProcessStage { get; set; }
2) 执行代码Get Process
:
using (var _context= new OrganizationServiceContext(service))
{
// Get the processid using the name provided
var process = (from p in _context.CreateQuery()
where
p.Name == Process.Get(executionContext)
&&
p.StateCode == WorkflowState.Activated
select new Workflow
{WorkflowId = p.WorkflowId}
).FirstOrDefault();
if (process==null)
throw new InvalidPluginExecutionException(string.Format("Process '{0}' not found",Process.Get(executionContext)));
使用提供的名称
获取stage id
var stage = (from s in _context.CreateQuery()
where
s.StageName == ProcessStage.Get(executionContext)
&&
s.ProcessId.Id == process.WorkflowId
select new ProcessStage
{ProcessStageId = s.ProcessStageId}
).FirstOrDefault();
if (stage == null)
throw new InvalidPluginExecutionException(string.Format("Stage '{0}' not found", Process.Get(executionContext)));
您现在可以 Change
Update
检索您的值的阶段 ...
Entity uStage = new Entity(context.PrimaryEntityName);
uStage.Id = context.PrimaryEntityId; //
uStage["stageid"] = stage.ProcessStageId; //retrieved stage
uStage["processid"] = process.WorkflowId; //process id
为什么不直接从您的 IServiceProvider
获取服务并检索字段?
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = CommonPluginLibrary.GetContextFromIServiceProvider(serviceProvider);
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
Entity campaign = service.Retrieve(context.PrimaryEntityId, context.PrimaryEntityName, new ColumnSet("processid", "stageid"));
// ...
// Do your stuff with campaign["processid"], campaign["stageid"]
// ...
}
另一种方法是为 stageid
和 processid
创建两个 shadow 字段(例如 new_stageid
、new_processid
),并使用在 process/stage 更新时触发的 同步工作流 填充这些字段。
然后,您可以在这些 shadow 字段上注册您的插件,因为它们将是您实体的自定义属性。