将任务中的注释复制到案例

时间:2012-06-01 22:23:12

标签: dynamics-crm dynamics-crm-2011

我有一个手动调用的进程,该进程与帐户实体绑定。该过程有许多步骤。其中一个步骤是创建任务并将其分配给某人。预计这个人会添加一些笔记并完成任务。

在此过程中,我有一个创建服务案例的步骤。创建完成后,我想将上述任务中的注释复制到新创建的服务案例中。

我已经创建了一个自定义工作流活动来尝试完成此操作。我已经得到了部署和在我的过程中使用它没有任何错误,它确实将内容复制到服务案例的备注字段,但它复制任务的标题,而不是注释内容,我不能完全了解原因。

public class CopyNotes : CodeActivity
{
    protected override void Execute(CodeActivityContext executionContext)
    {
        //Create the context
        IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
        IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

        //get the notes associated with the source entity
        Guid copyFromId = CopyFrom.Get(executionContext).Id;
        Guid copyToId = CopyTo.Get(executionContext).Id;

        EntityCollection copyFromNotes = RetrieveNotes(service, copyFromId);

        if (copyFromNotes.Entities.Any())
        {
            foreach (Entity e in copyFromNotes.Entities)
            {
                Entity newNote = new Entity("annotation");
                newNote.Attributes["subject"] = e.Attributes["subject"];
                newNote.Attributes["notetext"] = e.Attributes["notetext"];
                newNote.Attributes["objectid"] = new EntityReference() { Id = copyToId, LogicalName = CopyTo.Get(executionContext).LogicalName };
            }
        }
    }

    private EntityCollection RetrieveNotes(IOrganizationService service, Guid relatedObject)
    {

        ConditionExpression condition = new ConditionExpression();
        condition.AttributeName = "objectid";
        condition.Operator = ConditionOperator.Equal;
        condition.Values.Add(relatedObject.ToString());

        ColumnSet columns = new ColumnSet("subject", "notetext");

        QueryExpression query = new QueryExpression();
        query.ColumnSet = columns;
        query.EntityName = "annotation";
        query.Criteria.AddCondition(condition);

        EntityCollection results = service.RetrieveMultiple(query);

        return results;

    }

    [RequiredArgument]
    [ReferenceTarget("task")]
    [Input("Copy notes from item")]
    public InArgument<EntityReference> CopyFrom { get; set; }

    [RequiredArgument]
    [ReferenceTarget("incident")]
    [Input("Copy notes to item")]
    public InArgument<EntityReference> CopyTo { get; set; }
}

2 个答案:

答案 0 :(得分:3)

我认为你需要在定义之后实际创建newNote。

foreach (Entity e in copyFromNotes.Entities)
{
     Entity newNote = new Entity("annotation");
     newNote.Attributes["subject"] = e.Attributes["subject"];
     newNote.Attributes["notetext"] = e.Attributes["notetext"];
     newNote.Attributes["objectid"] = new EntityReference() { Id = copyToId, LogicalName = CopyTo.Get(executionContext).LogicalName };
     service.Create(newNote);
}

一旦我这样做了,你的代码就可以很好地创建一个包含标题和注释文本的新笔记。

答案 1 :(得分:0)

如果异步创建足够快,您可以使用标准工作流执行此操作。

安德烈亚斯