简体   繁体   English

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

[英]crm2011 plugin issue for opportunity entity

I create crm2011 plugin that fires on Update message(Pre_operation) on Opportunity. 我创建了在Opportunity上的Update消息(Pre_operation)上触发的crm2011插件。 In plugin, I use email template and send the email by using email template. 在插件中,我使用电子邮件模板并使用电子邮件模板发送电子邮件。 When I run, the plugin cannot send email with email template although the template id is correct. 当我运行时,虽然模板ID正确,但插件无法使用电子邮件模板发送电子邮件。 I get this error message "Object reference not set to an instance of an object." 我收到此错误消息“对象引用未设置为对象的实例”。 Please help me...Here is the my plugin code.. 请帮帮我......这是我的插件代码..

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

}

} }

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

_service vlaue from your code is still null . 您的代码中的_service vlaue仍然为null
It means that you have not re-initialized that value. 这意味着您尚未重新初始化该值。

Please refer to those links 请参考这些链接

1. Using CRM 2011 Service to Create Record from Code 1. 使用CRM 2011服务从代码创建记录
2. CRM 2011 SecurityNegotiationException trying to access web services 2. CRM 2011 SecurityNegotiationException尝试访问Web服务
3. Issue with mocking IOrganizationService.Execute in CRM 2011 plugin 3. 在CRM 2011插件中模拟IOrganizationService.Execute的问题

As Frank just mentioned _service is still null. 正如弗兰克刚刚提到的那样_service仍然是空的。 Follow this template inside your Execute method: 在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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM