Dynamics CRM - 在新创建的实体上访问工作流内的属性

时间:2010-02-09 19:19:44

标签: c# workflow dynamics-crm dynamics-crm-4

我正在创建一个Dynamics CRM工作流程程序集,以便在任何类型的另一条记录上创建新的Note时执行。我需要能够在新创建的Note实体上访问属性Prop1以完成其他任务。

以前我只访问从字段或用户输入的值,但从不访问新创建的实体的属性。任何指导都将不胜感激。

更新 这是关于CRM 4.0。

我等待时的更多信息: 最终,此工作流程程序集将创建一个电子邮件,其中包含指向新创建的Note记录的父实体的链接。我需要获得的属性是AnnotationId。一旦创建了Note记录,我将根据新创建的Note的AnnotationId检索ObjectId和ObjectTypeCode。 (如果你好奇的话)

1 个答案:

答案 0 :(得分:4)

好的,如果您使用4.0自定义工作流而不是3.0标注,则应添加工作流程序集,并使用Context服务并执行工作流程的上下文以从新笔记中提取值。

请参阅下面的示例,了解如何使用上下文服务访问记录以及当前执行上下文的ID(应该是您的注释)

    /// <summary>
    /// The Execute method is called by the workflow runtime to execute an activity.
    /// </summary>
    /// <param name="executionContext"> The context for the activity</param>
    /// <returns></returns>
    protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
    {

        // Get the context service.
        IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
        IWorkflowContext context = contextService.Context;

        // Use the context service to create an instance of CrmService.
        ICrmService crmService = context.CreateCrmService(true);
        BusinessEntity newNote = GetNote(crmService, context.PrimaryEntityId);

        string noteAttrib;

         noteAttrib = newNote.Properties.Contains("AnnotationId") ? ((Lookup)newNote.Properties["annotationid"]).name.ToString() : null;

        return ActivityExecutionStatus.Closed;
    }

GetNotes方法将是ID通过CRM服务调用的标注查询, 这是一个从MSDN略微修改的示例,用于返回注释:

private BusinessEntity getNote(ICrmService service, guid noteid)
{
  // Create the column set object that indicates the fields to be retrieved.
  ColumnSet cols = new ColumnSet();

  // Set the columns to retrieve, you can use allColumns but its good practice to specify:
  cols.Attributes = new string [] {"name"};

  // Create the target object for the request.
  TargetRetrieveAnnotation target = new TargetRetrieveAnnotation();

  // Set the properties of the target object.
  // EntityId is the GUID of the record being retrieved.
  target.EntityId = noteid;

  // Create the request object.
  RetrieveRequest retrieve = new RetrieveRequest();

  // Set the properties of the request object.
  retrieve.Target = target;
  retrieve.ColumnSet = cols;

  // Execute the request.
  RetrieveResponse retrieved = (RetrieveResponse)service.Execute(retrieve);
  return RetrieveResponse;
}
相关问题