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)

Set value into lookup field

(0) ShareShare
ReportReport
Posted on by

Hi

I need to set a value to a lookup field but I don't know how to do that

this is the code I have

var entity = context.GetEntityFromContext().ToEntity<pwc_contract>();

var aircraftid = aircraftRetrieved.Entities[0].Attributes["pwc_aircraftid"];
var aircraftGuid = new Guid(aircraftid.ToString());
entity.Attributes["pwc_aircraftid"] = new EntityReference("pwc_aircraft", aircraftGuid);
                                
service.Update(entity);


the lookup field is "pwc_aircraftid" 

I hope you can help me

thanks 

*This post is locked for comments

I have the same question (0)
  • necsa Profile Picture
    3,455 on at

    Hi,

    Which way do you want use. Using javascript or C#?

    please let me know also

    are you trying to read value from another entity or you want to set a value as you wish?

    thanks

    Happy CRM

  • Tobias Koller Profile Picture
    on at

    what exactly is not working? are you using this code inside a plugin?

  • Community Member Profile Picture
    on at

    The code is c#

    and I'm doing a query to the same entity pwc_aircraft and I want to set the result value to the field

  • Community Member Profile Picture
    on at

    Yes is a plugin

  • Verified answer
    necsa Profile Picture
    3,455 on at

    Lookup fields in CRM 2011 are EntityReference, this means you need to know the LogicalName of the entity the lookup is pointing and the Id of the record.

    as example:

    Account acc = new Account();
    acc.Attributes["name"] = "Ram"; // this values got inserted
    acc.Attributes["age"] = "22"; // this values got inserted
    acc.Attributes["lookupfieldid"] = new EntityReference("contact", contactId); // if lookupfieldid is pointing to contact entity
    service.Create(acc); // to create account

    if you want to update;

    as example:

    Entity recordToUpdate = service.Retrieve("contact", contactId, new ColumnSet(true));
    recordToUpdate["parentcustomerid"] = new EntityReference("account", accountId);
    service.Update(recordToUpdate);

    first get the record to update, after set the lookup field with an EntityReference and after you save the record.

    thanks

    Happy CRM

  • Community Member Profile Picture
    on at

    I have the same code

    entity.Attributes["pwc_aircraftid"] = new EntityReference("pwc_aircraft", aircraftGuid);

    but the lookup field seems to be null, is that because I need to set the Name with the ID in the entityReference?

  • necsa Profile Picture
    3,455 on at

    Yes you need always id.

    thanks

    Happy CRM

  • Tobias Koller Profile Picture
    on at

    I don't know exactly what you are doing with "context.GetEntityFromContext" (I get my context a liitle bit different) but I don't think this is a Problem here ;)

    are you using earlybindings ?

    if you want to set the lookup inside the entity for which the plugin is running and you registered it in "pre"-step, the plugin runs before the record is updated and you don't have to call "Service.Update(entity).

    You only need to call the Update-Method manually when you want to update other entities or your plugin is running in Post-Mode.

  • necsa Profile Picture
    3,455 on at

    please give more detail and add your code section where do you want to update.

    thanks

    happy CRM

  • Community Member Profile Picture
    on at

    Ok the code is working now

    but the problem the Name is displayed as null  

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