locked
Retrieving entity's ID in Plug-in registered on "Create" RRS feed

  • Question

  • Hi Guys,

    So, i have a plugin which is registered on a "Create" of a record. I also have a POST image registered. Now the issue I am facing is getting the ID/GUID for this record from the "PostEntityImages" property of the plugin "context"...

    DynamicEntity deEntity = (DynamicEntity)context.PostEntityImages["postimage"];  //My Dynamic Entity

    I have tried the following below here to get the Guid for deEntity:

    • Guid _guid = ((Key)deEntity.Properties["new_creationid"]).Value;
    • Guid _guid = ((Key)deEntity["new_creationid"]).Value;
    • Guid _guid = ((Key)((DynamicEntity)context.PostEntityImages["postimage"]).Properties["new_creationid"]).Value;

    In debugging my plugin, all the above examples fail with an error  The given key was not present in the dictionary

    Any ideas on how I can retrieve the GUID that I need?

    I need to retrieve this GUID because I then use it to retrieve other entities related to the record.

    Regards

    Hopeful


    • Edited by _Hopeful Thursday, April 7, 2011 2:32 PM
    Thursday, April 7, 2011 2:20 PM

Answers

  • This error has made my life miserable. In past I followed this link http://msdn.microsoft.com/en-us/library/bb955364.aspx and got success. As you are using PostEntityImages therefore not sure will it help you.
    Regards Faisal
    • Marked as answer by _Hopeful Wednesday, April 13, 2011 7:20 AM
    Thursday, April 7, 2011 6:12 PM
    • Marked as answer by _Hopeful Wednesday, April 13, 2011 7:20 AM
    Friday, April 8, 2011 11:25 AM
  • Hi,

    Try out the below code... Hope it will work for you.

    public void Execute(IPluginExecutionContext context)
    {
        if (context.MessageName == "Create")
        {
            if (context.InputParameters.Properties.Contains(ParameterName.Target) && context.InputParameters.Properties[ParameterName.Target] is DynamicEntity)
            {
                DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];

                Guid id;
                id = new Guid(context.OutputParameters.Properties["id"].ToString());           
            }
        }
    }


    Prathmesh http://patelprathmesh.blogspot.com/
    • Marked as answer by _Hopeful Wednesday, April 13, 2011 7:19 AM
    Monday, April 11, 2011 9:52 AM

All replies

  • If this is registered on pre-create you won't have a post image, the guid isn't even determined yet at that point.  If this is registered post-create this should work.
    Jamie Miley
    http://mileyja.blogspot.com
    Linked-In Profile
    Follow Me on Twitter!
    Thursday, April 7, 2011 2:25 PM
    Moderator
  • This is registered on Post-create and it is Synchronous.
    Thursday, April 7, 2011 2:29 PM
  • This error has made my life miserable. In past I followed this link http://msdn.microsoft.com/en-us/library/bb955364.aspx and got success. As you are using PostEntityImages therefore not sure will it help you.
    Regards Faisal
    • Marked as answer by _Hopeful Wednesday, April 13, 2011 7:20 AM
    Thursday, April 7, 2011 6:12 PM
  •  

    You should inspect OutputParameters for Create this will contain the ID generated in the core operation

    Guid objectid = (Guid)context.OutputParameters[ParameterName.Id]

     


    Hope this helps. Amar

    CRM Forum Guidance on how to Help Us Help You


    Thursday, April 7, 2011 9:36 PM
    • Marked as answer by _Hopeful Wednesday, April 13, 2011 7:20 AM
    Friday, April 8, 2011 11:25 AM
  • Hi,

    Try out the below code... Hope it will work for you.

    public void Execute(IPluginExecutionContext context)
    {
        if (context.MessageName == "Create")
        {
            if (context.InputParameters.Properties.Contains(ParameterName.Target) && context.InputParameters.Properties[ParameterName.Target] is DynamicEntity)
            {
                DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];

                Guid id;
                id = new Guid(context.OutputParameters.Properties["id"].ToString());           
            }
        }
    }


    Prathmesh http://patelprathmesh.blogspot.com/
    • Marked as answer by _Hopeful Wednesday, April 13, 2011 7:19 AM
    Monday, April 11, 2011 9:52 AM