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

Announcements

News and Announcements icon
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,130

 

 

 

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)
  • Community Member Profile Picture
    on at

    are you getting an error message like it?

    "the property not fount in the current dictionary"

  • Community Member Profile Picture
    on at

    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 */

     

    }

    }

  • Suggested answer
    Community Member Profile Picture
    on at

    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/

  • ashishkhanna Profile Picture
    180 on at

    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
    Hamzeh Zawahreh Profile Picture
    3,674 on at

    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

  • Suggested answer
    Hamzeh Zawahreh Profile Picture
    3,674 on at

    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

    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
    Aileen Gusni Profile Picture
    44,524 on at

    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.

  • Saddamk206 Profile Picture
    777 on at

    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
    Community Member Profile Picture
    on at

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans