2012-05-28 41 views
3

我創建了crm2011插件,針對商機更新消息(Pre_operation)。在插件中,我使用電子郵件模板並使用電子郵件模板發送電子郵件。當我運行時,雖然模板ID是正確的,但插件無法發送帶有電子郵件模板的電子郵件。我收到此錯誤消息「對象引用未設置爲對象的實例」。請幫我...這裏是我的插件代碼..crm2011針對商機實體的插件問題

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 

} 

}從代碼

+0

你能告訴代碼給你不設置到object',這樣我可以給你建議的一個實例是錯誤'對象引用的是哪一行? –

+0

這行代碼給了我錯誤「對象引用未設置爲對象的實例」SendEmailFromTemplateResponse emailUsingTemplateResp =(SendEmailFromTemplateResponse)_service.Execute(emailUsingTemplateReq); – Nightmare

回答

2

弗蘭克剛纔提到_Service仍然是空。按照這個模板您的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); 

}