locked
Error while updateing the uplugin RRS feed

  • Question

  • Hello All,

    I am getting "Given Key was not present" error message while updating the record using post update plugin.

    Code is below.

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

    if(Call.Attributes.Contains("OwnerId"))

    {

       //Business Logic

       string Owner = Call.Attributes["OwnerId"].tostring();

    }

    Let me know what is the issue on the above code.

    Thanks

    Kumar R

    Sunday, December 30, 2012 6:37 PM

Answers

  • Hello All,

    Thanks for proving the information and now i can fetch the owner value now.

    Regards,

    Kumar R

    • Marked as answer by Kumar_R Wednesday, January 2, 2013 7:07 AM
    Wednesday, January 2, 2013 7:07 AM

All replies

  • Hi Kumar,

    At first glance, you are checking to see if the attribute exists before using it but:

    1. You could try adding the following around the code to see if it is a problem with the Target not existing (because of the message you've registered against)

    if (context.InputParameters.Contains("Target") &&
        context.InputParameters["Target"] is Entity)
    {
    
    ...
    }
    


    2. The attribute names should be in Lower case

    3. Rather than using the Attributes collection, use the GetAttributeValue<T> method:

    Call.GetAttributeValue<EntityReference>("ownerid")

    4. The tostring is not capitalised correctly, it should be ToString(), but actually the OwnerId is an EntityReference and if you want the OwnerID you need to use the EntityReference.Id property.

    Guid OwnerId = Call.GetAttributeValue<EntityReference>("ownerid").Id;

    Scott


    Scott Durow
    Read my blog: www.develop1.net/public     Follow Me on Twitter
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Sunday, December 30, 2012 8:53 PM
    Answerer
  • Hello Kumar,

    Just want to add one more point in here if you will get those fields in property bag which is modified, otherwise you won't get that attribute, but still if you need to get that attribute, then you can register image in your plugin and can get that attribute from image instead of entity attributes collection.


    Contact Me
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, December 31, 2012 4:03 AM
    Moderator
  • Hello Scott and Mahender Pal,

    I tried  both the senario's and giving the same error..."Given Key was not present".

    @Scott:

    • Modified the code as you suggested and tried to fetch the owner value, but no success.

    @ Mahender:-

    • Register the image and call the required attributes.
    • Modified the code to fetch that preimage attributes and still the same error.

    Monday, December 31, 2012 6:09 AM
  • Could you share you complete code here ??

    Contact Me
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, December 31, 2012 6:13 AM
    Moderator
  • Hi Kumar,

    Sounds like the error may be coming from something else. If you comment out the code, do you still get the error?

    Scott


    Scott Durow
    Read my blog: www.develop1.net/public     Follow Me on Twitter
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Monday, December 31, 2012 8:55 AM
    Answerer
  • Hi, 

    Try this code to set the OwnerId field:

    Call.Attributes["OwnerId"] = new EntityReference("account", (Guid)((Entity)context.InputParameters["Target"]).Id);


    Naren

    Monday, December 31, 2012 12:44 PM
  • Hello Mahender Pal,

    Please find the below complete code.

    namespace Crm.Crm.Calldetails
    {
        public class PostValidation : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                // Obtain the execution context from the service provider.
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                // Obtain the organization service reference.
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);


                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    //Obtain the Entity Details
                    if (context.MessageName.ToLower() == "update")
                    {
                        Entity Call = (Entity)context.InputParameters["Target"];

                        //string Ownerid = Call.Attributes["ownerid"].ToString();

                        if (Call.Attributes.Contains("new_calldetailid"))
                        {
                            // EntityReference CallID = (EntityReference)Call.Attributes["new_calldetailid"];
                            //Call.Attributes["OwnerId"] = new EntityReference("account", (Guid)((Entity)context.InputParameters["Target"]).Id);
                            Entity Owner = null;
                            Owner = (Entity)context.PreEntityImages["ownerid"];
                           
                            //string CallStatus = Call.FormattedValues["new_callstatus"].ToString();
                            if (Call.Attributes.Contains("new_callstatus"))
                            {
                                Entity Systemuser = new Entity("Systemuser");
                            }
                        }
                    }
                }
            }

    Tuesday, January 1, 2013 9:33 AM
  • Hello Mahender Pal,

    While debugging the code, i found the owner value is coming under preimage option. But i am getting the error message while retrieving preimage values.

    Could you let me know how to retrieve the preimage values.

    Thanks

    Kumar R

    Tuesday, January 1, 2013 10:37 AM
  • Hello,

    Try below code

    if(context.PreEntityImages.Contains("PreImageName")) //change preimagename with your preimage name
                                {
     Entity PreImage = (Entity)context.PreEntityImages["PreImageName"];
           if (PreImage.Attributes.Contains("ownerid"))
                                    {
                                        String UserName = ((EntityReference)PreImage.Attributes["ownerid"]).Name;
    }


    Contact Me
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Tuesday, January 1, 2013 3:02 PM
    Moderator
  • Hello All,

    Thanks for proving the information and now i can fetch the owner value now.

    Regards,

    Kumar R

    • Marked as answer by Kumar_R Wednesday, January 2, 2013 7:07 AM
    Wednesday, January 2, 2013 7:07 AM
  • Hi Kumar,

    Great- what was the thing that fixed it?

    Scott


    Scott Durow
    Read my blog: www.develop1.net/public     Follow Me on Twitter
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Wednesday, January 2, 2013 7:12 AM
    Answerer
  • Hello Kumar,

    As Scott said, you should share reason and the fix that you did to help others.


    Contact Me
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, January 2, 2013 7:32 AM
    Moderator