email casting issue in plugin? RRS feed

  • Question

  • Hello - I'm trying to get a handle to the email entity in the Email post-save plugin.

    The following code *was* working at 1 point because the plugin was able to complete and save:

                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                    // obtain the target entity from the input parameters
                    var email = (Email)context.InputParameters["Target"];

    However, now the last line is returning the following error:

    "Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type 'MyGennedEntities.Email'"

    I also tried using the following code in the last line:

    var email = ((Entity)context.InputParameters["Target"]).ToEntity<Email>();

    However, this code returns the following error at runtime:

    "Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type 'MyGennedEntities.ActivityParty'"

    Any idea what I'm doing wrong here, what might be going wrong, how to fix or debug?
    Friday, July 12, 2013 11:13 PM

All replies

  • Hi

    simply use

    var email = (Entity)Context.InputParameters["Target"];

    Saturday, July 13, 2013 4:37 PM
  • But I like having dot notation auto-complete for the actual entity type of email.

    I lose that if I just use the Entity object.  With the approach you're suggesting I assume that you use Attributes references to get the data?

    Do you figure out those properties by simply instantiating one of those entities in your code for inspection purposes?

    Sunday, July 14, 2013 5:51 AM
  • Hi,

    I had written email plugin, please find below  code that might help you  u  might need to check for party id  as you progress writing the whole plugin.      


                        Entity wod_PluginEntity = (Entity)context.InputParameters["Target"];


                        if (wod_PluginEntity.LogicalName == "email")



                            if (wod_PluginEntity.Contains("directioncode"))



                                if (((Boolean)wod_PluginEntity["directioncode"]) == true)


                                    EntityCollection wod_IncommingParty = null;

                                    wod_IncommingParty = (EntityCollection)wod_PluginEntity["to"];

                                    // Checking if plugin entity From field is activityparty entity object

                                    if (wod_IncommingParty != null && wod_IncommingParty[0].LogicalName == "activityparty")


                                        EntityReference wod_PartyReference = (EntityReference)wod_IncommingParty[0]["partyid"];


                                        if (wod_PartyReference.LogicalName == "account")


                                            // Retrieve sender Account record

                                           // Entity account1 = new Entity("account");

                                            Entity wod_Account = wod_CrmService.Retrieve("account", wod_PartyReference.Id, new ColumnSet(true));

                                            wod_Account = new Entity("account");

                                          wod_Account["emailaddress1"] = "abcd@gmail.com";

                                          wod_Account["accountid"] = wod_PartyReference.Id;


                                            // Your can write your code here..

                                           // throw new Exception("Tracking incomming email from account: " + wod_Account["name"]);


    • Edited by Abhisinha17 Tuesday, July 16, 2013 8:13 AM Update
    • Proposed as answer by Abhisinha17 Tuesday, July 16, 2013 10:04 AM
    Tuesday, July 16, 2013 8:12 AM