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

  • Saddamk206 Profile Picture
    777 on at
    RE: Set value into lookup field

    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);

  • necsa Profile Picture
    3,455 on at
    RE: Set value into lookup field

    If you don't use ID you can not create a Lookup Field. let to write in log then you can see where is breaking.

    thanks

    happy CRM

  • Community Member Profile Picture
    on at
    RE: Set value into lookup field

    Ok the code is working now

    but the problem the Name is displayed as null  

  • necsa Profile Picture
    3,455 on at
    RE: Set value into lookup field

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

    thanks

    happy CRM

  • Tobias Koller Profile Picture
    on at
    RE: Set value into lookup field

    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
    RE: Set value into lookup field

    Yes you need always id.

    thanks

    Happy CRM

  • Community Member Profile Picture
    on at
    RE: Set value into lookup field

    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?

  • Verified answer
    necsa Profile Picture
    3,455 on at
    RE: Set value into lookup field

    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
    RE: Set value into lookup field

    Yes is a plugin

  • Community Member Profile Picture
    on at
    RE: Set value into lookup field

    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

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
dkrishna Profile Picture

dkrishna 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans