针对机会实体的crm2011插件问题

时间:2012-05-28 09:52:34

标签: c# dynamics-crm-2011

我创建了在Opportunity上的Update消息(Pre_operation)上触发的crm2011插件。在插件中,我使用电子邮件模板并使用电子邮件模板发送电子邮件。当我运行时,虽然模板ID正确,但插件无法使用电子邮件模板发送电子邮件。我收到此错误消息“对象引用未设置为对象的实例”。请帮帮我......这是我的插件代码..

namespace Ppp.CRM.Plugin.OpptBidOwnerChange
{
 public class BidOwnerChange : IPlugin
 {
    #region Class Level Variables
    IServiceProvider _serviceProvider;
    IOrganizationServiceFactory _serviceFactory = null;
    IOrganizationService _service = null;
    IPluginExecutionContext _context = null;

    Entity _target = null;
    Entity _preImage = null;
    Entity _postImage = null;
    Guid _currentUser;
    Guid pre_bidownerid = Guid.Empty;
    Guid tag_bidownerid = Guid.Empty;
    Guid ownerid = Guid.Empty;
    Guid opportunityid = Guid.Empty;
    #endregion

    #region  IPlugin Members
    public void Execute(IServiceProvider serviceProvider)
    {
        try
        {
            string message = null;
            _serviceProvider = serviceProvider;
            _context = (IPluginExecutionContext)
                                         serviceProvider.GetService(typeof(IPluginExecutionContext));

            _serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            _currentUser = _context.UserId;
            message = _context.MessageName.ToLower();

            if (message == "update")//message == "create" || 
            {
                if (_context.InputParameters.Contains("Target") && _context.InputParameters["Target"] != null)
                    _target = (Entity)_context.InputParameters["Target"];

                if (_context.PreEntityImages.Contains("PreImage") && _context.PreEntityImages["PreImage"] != null)
                    _preImage = (Entity)_context.PreEntityImages["PreImage"];

                if (_context.PostEntityImages.Contains("PostImage") && _context.PostEntityImages["PostImage"] != null)
                    _postImage = (Entity)_context.PostEntityImages["PostImage"];


                if (_preImage.Attributes.Contains("hm_bidowner"))
                {
                    EntityReference owner = (EntityReference)_preImage.Attributes["hm_bidowner"];
                    pre_bidownerid = owner.Id;
                    _target.Attributes["hm_previousbidowner"] = new EntityReference("systemuser", owner.Id);
                }
                if (_target.Attributes.Contains("hm_bidowner"))
                {
                    EntityReference owner = (EntityReference)_target.Attributes["hm_bidowner"];
                    tag_bidownerid = owner.Id;
                }
                if (_preImage.Attributes.Contains("ownerid"))
                {
                    EntityReference owner = (EntityReference)_preImage.Attributes["ownerid"];
                    ownerid = owner.Id;
                }

                opportunityid = _target.Id;

                ActivityParty emailFrom = new ActivityParty { PartyId = new EntityReference("systemuser", _context.UserId) };
                List<ActivityParty> emailTo = new List<ActivityParty>();

                // alert user
                if (_preImage.Attributes.Contains("ownerid"))
                {
                    emailTo.Add(new ActivityParty { PartyId = new EntityReference("systemuser", ownerid) });
                }
                if (_preImage.Attributes.Contains("hm_bidowner"))
                {
                    emailTo.Add(new ActivityParty { PartyId = new EntityReference("systemuser", pre_bidownerid) });
                }
                if (_target.Attributes.Contains("hm_bidowner"))
                {
                    //emailTo.Add(new ActivityParty { PartyId = ((EntityReference)_target["hm_bidowner"]) });
                    emailTo.Add(new ActivityParty { PartyId = new EntityReference("systemuser", tag_bidownerid) });
                }

                Email email = new Email();
                email.From = new ActivityParty[] { emailFrom }; ;
                if (emailTo.Count() > 0)
                    email.To = emailTo;

                Guid templateId = Guid.Empty;
                templateId = new Guid("EC361CDE-6C9E-E111-BF90-D8D3855BE525");//ACE17A0C-279B-E111-9E28-080027332B48

                SendEmailFromTemplateRequest emailUsingTemplateReq = new SendEmailFromTemplateRequest
                {
                    Target = email,
                    TemplateId = templateId,
                    RegardingId = opportunityid,
                    RegardingType = Opportunity.EntityLogicalName
                };


                SendEmailFromTemplateResponse emailUsingTemplateResp = (SendEmailFromTemplateResponse)_service.Execute(emailUsingTemplateReq);

            }
        }
        catch (Exception ex)
        {
            throw new InvalidPluginExecutionException(ex.Message, ex);
        }
    }
    #endregion

    #region CRM Service
    private void CreateCRMService()
    {
        try
        {
            _service = _serviceFactory.CreateOrganizationService(_currentUser);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    #endregion

}

}

2 个答案:

答案 0 :(得分:2)

SendEmailFromTemplateResponse emailUsingTemplateResp = (SendEmailFromTemplateResponse)_service.Execute(emailUsingTemplateReq);
代码中的

_service值仍为 null
这意味着您没有重新初始化该值。

请参阅这些链接

1。Using CRM 2011 Service to Create Record from Code
2。CRM 2011 SecurityNegotiationException trying to access web services
3。Issue with mocking IOrganizationService.Execute in CRM 2011 plugin

答案 1 :(得分:2)

正如Frank刚刚提到的那样_service仍然是null。在Execute方法中使用此模板:

public void Execute(IServiceProvider serviceProvider){

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

// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory   serviceProvider.GetService(typeof(IOrganizationServiceFactory));

IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

}