web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Retrieve value from look up field.

(0) ShareShare
ReportReport
Posted on by

Hi guys. I have an entity called "order". In this entity i placed look up field which will get customer name from "Product" entity.

I tried to write some code which i found in internet.
here is the code: 

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

                if (entity.LogicalName != "new_order")
                    return;

              
                var product = ((Microsoft.Xrm.Sdk.EntityReference)(entity.Attributes["new_productname"]));
                var productname = service.Retrieve(product.LogicalName, product.Id, new ColumnSet(true));
                var namefield = productname["new_productname"].ToString();
                

                throw new InvalidPluginExecutionException(namefield);
                
            }

But it shows me error message: " The given key was not present in the dictionary."

Can anybody helpme ?

Thanks

EDIT


Hei guys, after searching on internet. Finally i found what i want. Here is the code i've created:

 if (entity.Attributes.Contains("new_productname"))
                {
                    Entity productreference = service.Retrieve("new_product", ((EntityReference)entity["new_productname"]).Id, new ColumnSet(true));

                    if (productreference.Attributes.Contains("new_productquantity"))
                    {
                        string productquantity= productreference.Attributes["new_productquantity"].ToString();

                        
                            entity["new_unitnumber"] = productquantity;
                        

                    }
                    
                }


this is will get product quantity from product entity, and place it into a field within order entity. This code work perfectly when the plugin running on "Update". But when i change to on "Create" its shows me error message. 

Correctly if im wrong, what was happen when i run this plugin on "Create" event is when this code tryng to get value from look up field. Its returning null. 

Any solution for this ?

Thanks

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Saraswati ,

    I have faced such error during my initial development.

    If the Plugin is registered in "OnCreate" then the following may be the Reasons.

    1. If the "new_productname" is not present in the Respective Entity (Either "Order" or Any entity you are requesting for the Field).

    2. If the "new_productname" field is "Empty"/"null" during the Entity Retrieval.

    For the above reasons you will face the "The given Key was not fount in the dictionary" error.

    If the Plugin is registered "OnUpdate" the following may be the Reasons.

    1. In the "Context" only the fields which are changed will be present.

    (For Example: If you have changed "Id" field in a "Customer Information" Entity form only the "Id" field will be carried in the "Context").

    2.1. If the "new_productname" is not present in the Respective Entity (Either "Order" or Any entity you are requesting for the Field ).

    3. If the "new_productname" field is "Empty"/"null" during the Entity Retrieval.

    For the above reasons you will face the "The given Key was not fount in the dictionary" error.

    Thanks & Regards,

    ARJUN RAJ V

    Hitachi Solutions India Pvt Ltd.,

    Hope it Works !! If you find it is working please mark as "Verified".

  • Mahendar Pal Profile Picture
    45,095 on at

    In addition to that you can simplify your code like below

    if(entity.Contains("new_productname"))  //first check if field is available or not

    {

    var productid =entity.GetAttributeValue<EntityReference>("new_productname").Id;

    var productname = service.Retrieve(product.LogicalName, productid, new ColumnSet(true));

                   var namefield = productname.GetAttributeValue<String>("new_productname");

                   throw new InvalidPluginExecutionException(namefield);

    }

    Thanks

  • Community Member Profile Picture
    on at

    Hii. Thanks for your answer.. But i think youre missing something inside your code. Your did't declade var product. Where you call on this line:

    var productname = service.Retrieve(product.LogicalName, productid, new ColumnSet(true));

    can you complete your code please ???

    Thanks

  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    You can simply change it to

    var productname = service.Retrieve("product", productid, new ColumnSet(true));

  • Community Member Profile Picture
    on at

    The "product" there is the name of entity right ?

  • Mahendar Pal Profile Picture
    45,095 on at

    yes, correct.

  • Community Member Profile Picture
    on at

    Hei, please look at the question above. I've edit my question. would you mind to take a read it and tell me a solution for it ?

  • Mahendar Pal Profile Picture
    45,095 on at

    Can you share what error you are getting ??

  • Community Member Profile Picture
    on at
    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Type Mismatch: Type of Attribute: new_callistyorder.new_durasisewa is: System.Decimal. However, Type of passed-in value is: System.StringDetail: 
    <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts">
      <ErrorCode>-2147220989</ErrorCode>
      <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" />
      <Message>Type Mismatch: Type of Attribute: new_callistyorder.new_durasisewa is: System.Decimal. However, Type of passed-in value is: System.String</Message>
      <Timestamp>2015-11-19T04:12:10.629105Z</Timestamp>
      <InnerFault i:nil="true" />
      <TraceText i:nil="true" />
    </OrganizationServiceFault>
    

    Thats the error message. But when i run the plugin on "Update" . I didt get error message like this

  • Mahendar Pal Profile Picture
    45,095 on at

    Here is the issue

    new_callistyorder.new_durasisewa is: System.Decimal. However, Type of passed-in value is: System.StringDetail:

    Make sure you are passing values in correct format. you can refer how to set different data type using below link

    www.magnetismsolutions.com/.../setting_values_programmatically_via_the_crm_2011_sdk

    Thanks

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans