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 Lookup value

(0) ShareShare
ReportReport
Posted on by 2,909

Hi all,

This has my mind spinning. I need to set the lookup value of a new record via a web API. I currently do this for an option set using the below code to return the value based on a text match. I believe that the value I have to set for the lookup is ? a GUID rather than value as below.

So if the record to be created is in the Canditate entity and the lookup is in the Course entity how would I go about setting it. I have played with using EntityReference however no success to date.

if (!string.IsNullOrWhiteSpace(formValues.Get("Standard")))
                {
                    string selectedValue = formValues.Get("Standard");
                    RetrieveAttributeRequest optionSetRequest = new RetrieveAttributeRequest();
                    optionSetRequest.EntityLogicalName = "Canditate";
                    optionSetRequest.LogicalName = "dev_standard";
                    optionSetRequest.RetrieveAsIfPublished = true;
                    RetrieveAttributeResponse optionSetResponse = (RetrieveAttributeResponse)service.Execute(optionSetRequest);
                    PicklistAttributeMetadata retrievedPicklistAttributeMetadata = (PicklistAttributeMetadata)optionSetResponse.AttributeMetadata;
                    OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
                    int selectedOptionValue = 0;
                    foreach (OptionMetadata oMD in optionList)
                    {

                        if (string.Equals(oMD.Label.UserLocalizedLabel.Label, selectedValue, StringComparison.OrdinalIgnoreCase))
                        {
                            selectedOptionValue = oMD.Value.Value;
                            break;
                        }
                    }
                    if (selectedOptionValue == 0)
                    {
                        selectedOptionValue = 1;
                    }
                    target["dev_standard"] = new OptionSetValue(selectedOptionValue);
                }

*This post is locked for comments

I have the same question (0)
  • antc Profile Picture
    2,909 on at

    The code was showing how I currently set the value of an optionset from the API.

    I explained that I need to now do this on an entity but for a lookup instead of an options set.

    candidate (entity) dev_course (lookup field)

    has a N:1 relationship with

    course (entity) dev_course_name (text field)

    My understanding is I have to set the entityreference for the dev_course field using the GUID and logical name from dev_coursename.

    I have been reading for hours and have tried to mock up some queryexpressions etc but just keep getting more confused hence no real code for the lookup being posted.

    Any guidance or links would be appreciated.

    Thanks,

    Antony

  • Suggested answer
    Waqar Sohail Profile Picture
    on at

    Hi Antc,

    Remember lookup is simple. Just consider N:1 that lookup will be part of N not the 1.

    So if you want to set the lookup in C#, you need two things. Entity LogicalName and Entity GuiD (Id).

    For lookup type you need to make EntityReference lookup = new EntityReference();

    you can then set its LogicalName and ID.

    Same if you want to retrieve it then you will need to parse the attribute value with EntityReference.

    You can follow this link as well

    social.msdn.microsoft.com/.../how-to-set-a-lookup-value-in-crm-2011-using-c

  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hi,

    You can write a retrievemultiple request to query your lookup entity based on some criteria may be name and when it will return value you can set your looup using it's id property.

    You can refer example(GetUserID method) in my below post to get , once you have id you can set it using entityreference

    http://himbap.com/blog/?p=791

    for example something like below

    entityobject["lookpufieldname"]=new EntityReference("targetentitylogicalname",GetID());

    Thanks

  • antc Profile Picture
    2,909 on at

    Hi all,

    Thanks for the ideas so far still seem to be having a mental hurdle with this although I suspect it will click at some point.

    Can't test at the moment however do you think this is on the right track?

    //Query for the GUID of the Course using CourseName

      if (!string.IsNullOrWhiteSpace(formValues.Get("CourseName")))

                   {

                       string cname = formValues.Get("CourseName");

    }

    QueryExpression query = new QueryExpression("bc_course");

    query.Criteria.AddCondition("bc_name", ConditionOperator.Equal, cname);

    Entity course = service.RetrieveMultiple(query).Entities.First();

    //Set the lookup

    Candidate can = new Candidate();

    ......

    ......

    can.Attributes["de_20_coursename"] = new EntityReference("bc_course", course.Id);

    service.Create(can);

  • Verified answer
    Waqar Sohail Profile Picture
    on at

    Hi Antc,

    Your code looks good but your can make it little better like this.

    QueryExpression query = new QueryExpression("bc_course");

    ConditionExpression condition = new ConditionExpression("bc_name",ConditionOperator.Equal, cname);

    query.Criteria.AddCondition(condition );

    EntityCollection courses = service.RetrieveMultiple(query);

    if(courses !=null && courses.Entities.count>0)

    {

       //get the first entity

    Entity course = courses.Entities[0];

    Candidate can = new Candidate();// You can also use Entity can = new Entity(LogicalName);

    ......

    ......

    can.Attributes["de_20_coursename"] = new EntityReference(course.LogicalName, course.Id);

    service.Create(can);

    }

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