locked
Plugin that sends email getting error when Owner changes RRS feed

  • Question

  • Hello everyone,

    I have been asked to write a post plugin (fires on Update) that notifies the owner of an Account (and other entities) when changes have been made to the account. In testing the plugin works fine for everything except if the owner gets changed.

    I have tried this with a few different entities (Account, Contact, Opportunity, a custom entity) with the same result. The plugin goes through the entire execution up till the

    SendEmailResponse res = (SendEmailResponse)crmService.Execute(req);

    and then an exception is thrown with the message "An unexpected error occurred."

    The stack trace of the exception gives this

    at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.InprocessServiceProxy.ExecuteCore(OrganizationRequest request)
       at My.Microsoft.CRM.Plugins.AccountUpdate_Email.Execute(IServiceProvider serviceProvider)

    The AccountUpdate_Email is my plugin

    I get the to field information like so

    EntityCollection toField = new EntityCollection();
    if (((EntityReference)postImage["ownerid"]).LogicalName.Equals("systemuser"))
    {
        Entity notifyuser = new Entity("activityparty");
        Guid userid = Guid.Empty;
        userid = ((EntityReference)preImage["ownerid"]).Id;
        Entity owner = new Entity();
    
        owner.LogicalName = "activityparty";
        owner.Attributes["partyid"] = new EntityReference("systemuser", userid);
        toField.Entities.Add(owner);
    }


    putting the email together is done as follows

    email["to"] = toField;
    Entity from = new Entity();
    from.LogicalName = "activityparty";
    from.Attributes["partyid"] = new EntityReference("systemuser", context.UserId);
    email["from"] = from;
    email["subject"] = "Account '" + postImage["name"] + "' has been updated";
    
    email["description"] = "The Account '" +
    "<a href='" + serverName + "main.aspx?etn=account&pagetype=entityrecord&id=" + postImage.Id + "&rof=false'>" +
    postImage["name"] + "</a>" + "' has been updated by " + ((EntityReference)postImage["modifiedby"]).Name + ".";
    email["regardingobjectid"] = new EntityReference(postImage.LogicalName, postImage.Id);
    
    Guid emailId = crmService.Create(email);
    SendEmailRequest req = new SendEmailRequest();
    req.EmailId = emailId;
    req.TrackingToken = String.Empty;
    req.IssueSend = true;
    
    
    SendEmailResponse res = (SendEmailResponse)crmService.Execute(req);

    This has been working without issue until I tested by changing the owner.

    It is my understanding that changing the owner uses the Assign message instead of Update. One of the things I am planning on trying is writing another plugin (or step for the current plugin) that would be tied to the Assign message that does pretty much this same thing hoping that anything that is different in the Update plugin would work as an Assign plugin.

    I appreciate your help.

    Friday, June 29, 2012 5:27 PM

Answers

  • Yes you are correct ownership change is assigment not update message. You will have to register the plugin in assign and update message.

    Just out of curiosity why are you not using Workflow for this?  I will recommend that you use crm no code workflows to send out an email when ever any thing changes on the record. It is asynchronous and a record link can be added to the email out of box.  Though the ownership change might not send an email to the old owner.

    Hope this helps


    -Devashish
    http://thecrmworld.wordpress.com
    http://ebizartisans.com


    • Edited by Devashish Bajpai Friday, June 29, 2012 6:26 PM
    • Marked as answer by JWnet Friday, June 29, 2012 6:52 PM
    Friday, June 29, 2012 6:21 PM

All replies

  • Yes you are correct ownership change is assigment not update message. You will have to register the plugin in assign and update message.

    Just out of curiosity why are you not using Workflow for this?  I will recommend that you use crm no code workflows to send out an email when ever any thing changes on the record. It is asynchronous and a record link can be added to the email out of box.  Though the ownership change might not send an email to the old owner.

    Hope this helps


    -Devashish
    http://thecrmworld.wordpress.com
    http://ebizartisans.com


    • Edited by Devashish Bajpai Friday, June 29, 2012 6:26 PM
    • Marked as answer by JWnet Friday, June 29, 2012 6:52 PM
    Friday, June 29, 2012 6:21 PM
  • I have been doing a number of Email notification that required jumping 3 and 4 entities away which was not possible within the workflow. Also, there was a different part where we listed out the changes on the entity, but if the assign is the only problem they will be sufficient as a workflow and make sure the plugin doesn't run when a new owner is assigned

    • Edited by JWnet Friday, June 29, 2012 7:09 PM
    Friday, June 29, 2012 6:52 PM
  • Your code is fine. I suspect the error is "Object address not found on party or party is marked as non-emailable" which is because of the recipient systemuser not having an email address.

    If it's not the root cause of the problem, try catching the SoapException as follows so that we'll know more details about that error.

    catch (System.Web.Services.Protocols.SoapException ex)
    {
        throw new InvalidPluginExecutionException(ex.Message + ". " + ex.Detail.InnerText, ex);
    }

    Friday, June 29, 2012 6:52 PM