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 :
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

I have the same question (0)
  • HillesumGong Profile Picture
    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
    3,512 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
    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

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 > Customer experience | Sales, Customer Insights, CRM

#1
DAnny3211 Profile Picture

DAnny3211 126

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 117

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 70 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans