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