Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Key not present in dictionary

Posted on by 215

I understand why i am getting the error, but i cannot find a solution on how to get the attribute that i need in order to pass value to it, code sample of my work so far is bellow, any suggestions are welcomed.

if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];

                    if (entity.LogicalName != Entity_Name)
                        return;

                    Entity productOffer = service.Retrieve(Entity_Name, entity.Id, new ColumnSet(true));
                    Entity vintage = service.Retrieve(Entity_Name, productOffer.GetAttributeValue<EntityReference>(Entity_Name).Id, new ColumnSet("new_netpriceperbottle", "transactioncurrencyid"));

                    if (context.MessageName == "Update" )
                    {
                        if (vintage.Contains("netpriceperbottle"))
                        {
                            vintage.Attributes["netpriceperbottle"] = productOffer.Attributes["new_netpriceperbottle"];
                            vintage.Attributes["transactioncurrencyid"] = productOffer.Attributes["transactioncurrencyid"];
                            service.Update(vintage);
                        }
                        
                    }

*This post is locked for comments

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Key not present in dictionary

    I would try and check if the sonade_netpriceperbottle exists in both entities, not just the vintage entity.

    If it doesn't exist in both, and you still want to write it

    You can use entity.Attributes.Add("sonade_netpriceperbottle", value) to add the attribute to the entity if it does not exist, and that way update it in your Update Request.

  • Filip Vanchevski Profile Picture
    Filip Vanchevski 215 on at
    RE: Key not present in dictionary

    Basically there is no value in vintage.Attributes["new_netpriceperbottle"] and that is why the Retrieve method is not getting it.

    I am wondering if there is a way to still retrieve that attribute from the database?

  • Filip Vanchevski Profile Picture
    Filip Vanchevski 215 on at
    RE: Key not present in dictionary

    Aric, thanks for taking the time to deal with this.

    To answer what EC.Product_Offer.Vintage returns

      - public const string Vintage = "new_vintageid";

      - so instead of productOffer.GetAttributeValue<EntityReference>("new_vintageid").Id

      - I'm using productOffer.GetAttributeValue<EntityReference>(EC.Product_Offer.Vintage).Id

      - "new_vintageid" is a lookup field in Product Offer entity.

    Furthermore after doing some debugging i saw exactly where my code broke for the first time:

      - Line where exception is thrown: vintage.Attributes["new_netpriceperbottle"] = productOffer.Attributes["new_netpriceperbottle"];

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Key not present in dictionary

    In the following code, which you mention is causing an error:

    Entity vintage = service.Retrieve(EC.Vintage.Entity_Name, productOffer.GetAttributeValue<EntityReference>(EC.Product_Offer.Vintage).Id, new ColumnSet(true));

    What does EC.Product_Offer.Vintage return?

    Can you possible add tracking before that line?

  • Filip Vanchevski Profile Picture
    Filip Vanchevski 215 on at
    RE: Key not present in dictionary

    Ok so i just checked to see if it might be the product offer that is causing the problem but it seems to be just fine.

    Under Keys i have "sonade_netpriceperbottle";

    Under Values i have the proper value....so it is populated.

    This means that the problem is in the vintage entity because the key "sonade_netpriceperbottle" is missing, and yeah there is no value for that key in my CRM, thats why im using the plug in in order to populate the price and currency in vintage entity from product offer.

    I just tried to do a fetchXml for the vintage entity using the id of product offer and it still isn't returning the key. Updated code sample with fetch is bellow

    var fetchVintage = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
                                                <entity name='new_vintage'>
                                                    <attribute name='new_vintageid'/>
                                                    <attribute name='new_name'/>
                                                    <attribute name='createdon'/>
                                                    <attribute name='new_netpriceperbottle'/>
                                                    <attribute name='transactioncurrencyid'/>
                                                    <order attribute='new_name' descending='false'/>
                                                    <link-entity name='new_offer' from='new_vintageid' to='new_vintageid' alias='ad'>
                                                <filter type='and'>
                                                    <condition attribute='new_offerid' operator='eq' value='" + productOffer.Id + @"'/>
                                                </filter>
                                                    </link-entity>
                                                </entity>
                                            </fetch>";
    
                        EntityCollection vintages = service.RetrieveMultiple(new FetchExpression(fetchVintage));
                        Entity vintage = vintages[0];
  • Filip Vanchevski Profile Picture
    Filip Vanchevski 215 on at
    RE: Key not present in dictionary

    Yeah they simply return the string value.

    EC.Product_Offer.Entity_Name = "new offer".

  • Filip Vanchevski Profile Picture
    Filip Vanchevski 215 on at
    RE: Key not present in dictionary

    I was thinking that the problem was with my vintage cause i couldn't find "netpriceperbottle" in the attributes that are part of my vintage entity, i am checking to see if  the problem might be the product offer

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Key not present in dictionary

    Seems like you are using your own custom class to replace early binding functionality in your code.

    Are you getting return values from:

    EC.Product_Offer.Entity_Name

    EC.Vintage.Entity_Name

    EC.Product_Offer.Vintage

    Can you put some tracing code, to validate those parameters?

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Key not present in dictionary

    Hi,

     this error usually means that where you are trying to get a value there is no such key.. Looks like the only place in your code that may have that problem is this:

    vintage.Attributes["netpriceperbottle"] = productOffer.Attributes["sonade_netpriceperbottle"];

     Are you sure you have sonade_netpriceperbottle populated in that productOffer record?

  • Filip Vanchevski Profile Picture
    Filip Vanchevski 215 on at
    RE: Key not present in dictionary

    Also at first the line where i am getting the vintage entity attributes i used:

    Entity vintage = service.Retrieve(Entity_Name, productOffer.GetAttributeValue<EntityReference>(Product_Offer.Vintage).Id, new ColumnSet(true));

    still got the same error

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans