在CRM 2011插件中合并注释时出错

时间:2012-07-26 18:46:30

标签: plugins dynamics-crm-2011

我正在开发一个CRM插件,给定两个实体GUID,共享它们之间的注释/注释。我有一个自定义功能区按钮,它获取两个选定实体的GUID,并将它们作为字符串添加到mergetrigger实体。然后我有一个插件链接到mergetrigger实体的创建,该实体使用两个GUID来合并注释。

但是,每当我单击该按钮时,插件都会引发以下错误:

  

MergeNotes_Plugin插件中发生错误:父对象类型无效

以下是代码:

public class MergeNotes_Plugin : IPlugin
{
    public const string CrmOrgPrefix = "***_";
    public const string ENTITY = "mergetrigger";

    public void Execute(IServiceProvider serviceProvider)
    {
        //Extract the tracing service for use in debugging sandboxed plug-ins.
        ITracingService tracingService =
            (ITracingService)serviceProvider.GetService(typeof(ITracingService));

        if (tracingService == null)
            throw new InvalidPluginExecutionException("MergeNotes_Plugin: Failed to retrieve the tracing service.");



        // Obtain the execution context from the service provider.
        IPluginExecutionContext context = (IPluginExecutionContext)
            serviceProvider.GetService(typeof(IPluginExecutionContext));

        tracingService.Trace("MergeNotes_Plugin: context depth is " + context.Depth + "\n(It should be < 3)");

        Entity triggerImage = new Entity();

        //Get the postImage
        if (context.PostEntityImages.Contains(CrmOrgPrefix + "postImage_"+ENTITY) &&
                context.PostEntityImages[CrmOrgPrefix + "postImage_" + ENTITY] is Entity)
        {
            triggerImage = (Entity)context.PostEntityImages[CrmOrgPrefix + "postImage_" + ENTITY];                     triggerImage[CrmOrgPrefix + "pluginstate"] = "PostImage Loaded Successfully";
            tracingService.Trace("MergeNotes_Plugin: PostImage successfully loaded");
        }



        try
        {
            //Create an instance of the organization Service
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            triggerImage[CrmOrgPrefix + "pluginstate"] = "Services loaded successfully";

            //Extract the GUID's the postImage of the MergeTrigger                
            Guid GUID1 = new Guid((String)triggerImage[CrmOrgPrefix + "guid1"]);
            Guid GUID2 = new Guid((String)triggerImage[CrmOrgPrefix + "guid2"]);

            triggerImage[CrmOrgPrefix + "pluginstate"] = "GUID\'s loaded successfully";

            //Check the GUID's are populated
            if (GUID1 != null && GUID1 != Guid.Empty && GUID2 != null && GUID2 != Guid.Empty)
            {
                tracingService.Trace("MergeNotes_Plugin: GUID\'s successfully loaded");
                triggerImage[CrmOrgPrefix + "pluginstate"] = "GUID\'s not empty";
                //MERGE NOTES


                EntityCollection copyFromNotes1 = RetrieveNotes(service, GUID1);
                EntityCollection copyFromNotes2 = RetrieveNotes(service, GUID2);

                triggerImage[CrmOrgPrefix + "pluginstate"] = "Notes retrieved";

                if (copyFromNotes1.Entities.Any())
                {
                    foreach (Entity e in copyFromNotes1.Entities)
                    {
                        Entity newNote = new Entity("annotation");
                        newNote.Attributes["subject"] = e.Attributes["subject"];
                        newNote.Attributes["notetext"] = e.Attributes["notetext"];
                        newNote.Attributes["objectid"] = new EntityReference() { Id = GUID2 };                        
                        service.Create(newNote);
                    }
                }
                triggerImage[CrmOrgPrefix + "pluginstate"] = "First note cycle complete";
                if (copyFromNotes2.Entities.Any())
                {
                    foreach (Entity e in copyFromNotes2.Entities)
                    {
                        Entity newNote = new Entity("annotation");
                        newNote.Attributes["subject"] = e.Attributes["subject"];
                        newNote.Attributes["notetext"] = e.Attributes["notetext"];
                        newNote.Attributes["objectid"] = new EntityReference() { Id = GUID1 };                                 
                        service.Create(newNote);
                    }
                }
                triggerImage[CrmOrgPrefix + "pluginstate"] = "Second note cycle complete";

                //Clean up the Mergetrigger
                service.Delete(CrmOrgPrefix + ENTITY, new Guid(triggerImage[CrmOrgPrefix + ENTITY + "id"].ToString()));

                tracingService.Trace("MergeNotes_Plugin: MergeTrigger was deleted");
            }
            else
            {
                tracingService.Trace("MergeNotes_Plugin: Error loading entity GUID\'s");
            }

        }
        catch (FaultException<OrganizationServiceFault> ex)
        {
            throw new InvalidPluginExecutionException("An error occurred in the MergeNotes_Plugin plug-in.\n" + ex.Detail.Message, ex);
        }
        catch (Exception ex)
        {
            tracingService.Trace("MergeNotes_Plugin: {0}", ex.ToString());
            throw;
        }
    }

    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;
    }   
}

有人能指出我这个问题的原因吗?

0 个答案:

没有答案