Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

crm2011 plugin issue for opportunity entity

I create crm2011 plugin that fires on Update message(Pre_operation) on Opportunity. 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. 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

}

}

like image 705
Nightmare Avatar asked Mar 05 '26 10:03

Nightmare


2 Answers

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

_service vlaue from your code is still 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
2.CRM 2011 SecurityNegotiationException trying to access web services
3.Issue with mocking IOrganizationService.Execute in CRM 2011 plugin

like image 116
Frank Myat Thu Avatar answered Mar 06 '26 23:03

Frank Myat Thu


As Frank just mentioned _service is still null. Follow this template inside your Execute method:

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

}
like image 44
A Robben Avatar answered Mar 07 '26 00:03

A Robben