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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to Retrieve the value of a lookup field within a plugin

(0) ShareShare
ReportReport
Posted on by 4,128

 

 

 

I am trying to retrive the Primary contact name within the account entity in a PlugIn so i can send to an external database, the flooiwng code return error regarding the key is not exist in the dictionary, do i miss any thing? could you help please?

Lookup contactLookup = (Lookup)entity.Properties["primarycontactid"];

 

 

string sPrimaryContact = contactLookup.name;

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at
    RE: How to Retrieve the value of a lookup field within a plugin

    Refer Below link

    aspsolutionkirit.blogspot.in/.../getting-vlaueidname-of-lookup-field-in.html

  • Saddamk206 Profile Picture
    777 on at
    RE: How to Retrieve the value of a lookup field within a plugin

    Get Value of Lookup fields

    EntityReference entref = (EntityReference)item.Attributes[attributeName];

    var LookupId = entref.Id;

    var logicalName = entref.LogicalName;

    Set Value of Lookup fields

    newAccount[attributeName] = new EntityReference(logicalName, LookupId);

    Set Null Value of Lookup fields

    newAccount[attributeName] = Null;

  • Suggested answer
    Aileen Gusni Profile Picture
    44,524 on at
    RE: How to Retrieve the value of a lookup field within a plugin

    Alaa,

    You must change 'Lookup' to 'EntityReference'

    And please be remember that if you use it on update plugin, you might need to register preimage (for pre and postimage for post) to get this record field, it is very important, especially when you update a record but didn't update this primarycontactid, you will get return error, object reference or string was not exists in the dictionary.

    I think your case here is either because you still using lookup (which is CRM 4.0 syntax (I don't know what crm version you are currently using) and my suspect is because you don't have image to get the old value which is not changed when you run the plugin.

    Hope this helps!.

    Thanks.

  • Suggested answer
    Hamzeh Zawahreh Profile Picture
    3,674 on at
    RE: How to Retrieve the value of a lookup field within a plugin

    hi

    here is sample on how get product lookup guid and name:

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

      if (entity.LogicalName == "new_producttaxrate")

     {

      if (entity.Attributes.Contains("new_product"))

        {

        EntityReference ownerLookup = (EntityReference)entity.Attributes["new_product"];

         productName = ownerLookup.Name;

         Guid  productId = ownerLookup.Id;

       }

     }

  • Suggested answer
    Hamzeh Zawahreh Profile Picture
    3,674 on at
    RE: How to Retrieve the value of a lookup field within a plugin

    dear Alaa Ramadan

    and this if you want to use it in javascript:

    var lookupObject = Xrm.Page.getAttribute("yourlookupattributename");

       if (lookupObject != null)
      {

           var lookUpObjectValue = lookupObject.getValue();

           if ((lookUpObjectValue != null))

           {

             var lookuptextvalue = lookUpObjectValue[0].name;

           var lookupid = lookUpObjectValue[0].id;

           }


     }

  • Suggested answer
    Hamzeh Zawahreh Profile Picture
    3,674 on at
    RE: How to Retrieve the value of a lookup field within a plugin

    dear Alaa Ramadan

    you can get lookup value from plugin using entity refrene likr this:

    EntityReference
    contactlookup = (EntityReference)targetEntity.Attributes["new_contact"];

    see this:

    https://social.microsoft.com/Forums/en-US/8a3af20a-fbe4-480e-892c-021fe9605672/crm-2011-get-lookup-text-value?forum=crmdevelopment

  • ashishkhanna Profile Picture
    180 on at
    RE: How to Retrieve the value of a lookup field within a plugin

    What version of CRM are you using? Is it 2011 onwards or 4.0?

    If 2011 or later, then use this instead:

    Guid ContactId = ((EntityReference)entity.Attributes["primarycontactid"]).Id;

    string sPrimaryContact   = ((EntityReference)entity.Attributes["primarycontactid"]).Name;

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: How to Retrieve the value of a lookup field within a plugin

    Hello Alaa Ramadan,

    This might be useful to you,

    https://santoshmscrm.wordpress.com/2015/06/22/how-to-retrieve-lookup-name-and-id-through-plugin/

  • Community Member Profile Picture
    on at
    Re: How to Retrieve the value of a lookup field within a plugin

    In CRM 2011

    You can not use as you were using you can use in the following way

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

       if (targetEntity.LogicalName == "account")

    {

    if (targetEntity.Attributes.Contains("primarycontactid"))

    {

    //// Get the primary contactid;

    EntityReference  contactId=(EntityReference) targetEntity.Attributes["primarycontactid"];

    //// from there get the GUId of the Primarycontactid and again need a call to sdk to get the name of the contact for that particuar GUId.

    /* there is no way to retrive the name vlaue of the lookup as  we do in client sidescripting in plugins */

     

    }

    }

    In CRM 4.0 

    DynamicEntity   targetEntity = (DynamicEntity)context.InputParameters["Target"];

     

    if (targetEntity.Contains("primarycontactid"))

    {

    //// Get the primary contactid;

    Lookup contactId=(Lookup) targetEntity["primarycontactid"];

    //// from there get the GUId of the Primarycontactid and again need a call to sdk to get the name of the contact for that particuar GUId.

    /* there is no way to retrive the name vlaue of the lookup as  we do in client sidescripting in plugins */

     

    }

    }

  • Community Member Profile Picture
    on at
    Re: How to Retrieve the value of a lookup field within a plugin

    are you getting an error message like it?

    "the property not fount in the current dictionary"

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans