Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

How to retrieve data from lookup field in dynamic crm

(0) ShareShare
ReportReport
Posted on by 15

I want to fetch value from lookup field in plugin. Does anyone have code of it which can guide me how to do it. I am trying but stuck.

Thankyou

  • HillesumGong Profile Picture
    HillesumGong 20 on at
    RE: How to retrieve data from lookup field in dynamic crm

    FYI.

            public void Execute(IServiceProvider serviceProvider)
            {
                ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = factory.CreateOrganizationService(context.UserId);
    
                try
                {
                    //step 1
                    Entity targetEntity = (Entity)context.InputParameters["Target"];
                    //step 2 get value
                    EntityReference contactEr = targetEntity.GetAttributeValue("new_contact");
                    //step 3 set value
                    Entity newEntity = new Entity(targetEntity.LogicalName);
                    newEntity[""] = new EntityReference(contactEr.LogicalName, contactEr.Id);
                }
                catch (Exception e)
                {
                    throw new InvalidPluginExecutionException(e.Message);
                }
            }

  • Suggested answer
    CRMJetty Profile Picture
    CRMJetty 3,508 on at
    RE: How to retrieve data from lookup field in dynamic crm

    Hello Mukhan1833,

    you can refer this code for  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);



    Or you can refer this site for GET/SET of all datatype.
    https://www.crmcrate.com/plugin/how-to-read-and-write-values-in-different-fields-using-plugins/

  • Suggested answer
    Vinit_Bhagat Profile Picture
    Vinit_Bhagat 102 on at
    RE: How to retrieve data from lookup field in dynamic crm

    Hi mukhan1833,

    Below is the sample code:

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

      if (entity.LogicalName == "new_xyz") //entity logical name

     {

      if (entity.Attributes.Contains("new_abc"))  //Lookup field logical name

        {

        EntityReference lookupObj = (EntityReference)entity.Attributes["new_abc"];

         name = lookupObj.Name;

         Guid  id = lookupObj.Id;

       }

     }

    Or you can use below link for refernece.

    plugins - Get Value Dynamically from Lookup in CRM 365 using C# - Stack Overflow

    Hope this will help.

    Please mark as verified if the answer is helpful. image

    Thanks!

    Vinit 

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,436 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans