How to Set Lookup Field Value Using JavaScript -Dynamics 365

To set a lookup field value, you need 3 parameters.

  • Id of the lookup record
  • name of the lookup record
  • entity name of the lookup entity

Here is how to use these values to set an account lookup field.

var lookupValue = new Array();
lookupValue[0] = new Object();
lookupValue[0].id = myAccountId;
lookupValue[0].name = myAccountName;
lookupValue[0].entityType = "account";
Xrm.Page.getAttribute("mylookupfield").setValue(lookupValue);

19 responses to “How to Set Lookup Field Value Using JavaScript -Dynamics 365”

  1. Hind avatar
    Hind

    Can I use this to create a “None” value?

    1. furkankaracan avatar
      furkankaracan

      Hi, didn’t get the question sorry

      1. Hind avatar
        Hind

        My apologies. We have a lookup field of users, and there is a requirement to include a None option. Can I use this script to achieve the requirement?

      2. furkankaracan avatar
        furkankaracan

        Hi, I’m afraid you can’t do that. You can’t put a placeholder to the lookup field. But as a workaround you can create your own pcf contro. You can make it look like lookup. Then you can add a none placeholder. Check the pcf controls

      3. Hind avatar
        Hind

        Good to know. Many thanks 🙂

  2. Lawrence Duff avatar
    Lawrence Duff

    getvalue and setvalue uses an array as the object to get or set the value of a lookup field. Does this imply that the lookup field could have multiple values? For example, what would happen if we added a second object …

    var lookupValue = new Array();

    lookupValue[0] = new Object();
    lookupValue[0].id = myAccountId;
    lookupValue[0].name = myAccountName;
    lookupValue[0].entityType = “account”;

    lookupValue[1] = new Object();
    lookupValue[1].id = myOtherAccountId;
    lookupValue[1].name = myOtherAccountName;
    lookupValue[1].entityType = “account”;
    Xrm.Page.getAttribute(“mylookupfield”).setValue(lookupValue);

    And I wonder if the magical “choose any entity lookup Regarding field on Activities takes an array too?

    1. Furkan Karacan avatar

      Hi Lawrence, yes lookup can have multiple values. However this does not apply to all lookup fields. There are different types of lookups in CRM. For the party partylists in activities you can have multiple lookup values. Simply check here. https://learn.microsoft.com/en-us/dynamics365/customerengagement/on-premises/customize/types-of-fields?view=op-9-1#different-types-of-lookups

      1. Lawrence Duff avatar
        Lawrence Duff

        Mmm, I’m trying to enable activities –Appointment, Email, etc.– to appear in both the Activity Feed Timeline AND a Subgrid on the multiple entities related to the activity.

        (1) If the user adds the entity to an activity PartyList (Email.To, Appointment.RequiredAttendees, etc.) then the activity will appear in the Activity Feed Timeline. But it won’t appear in a Subgrid on the entities related to the Activity because there is no Party based relationship to choose from in the Subgrid to make the relationship to the Activity. The relationship is in the “magical” Activity Feed Timeline component, not the Subgrid component.

        (2) Instead, I could use the Regarding field in the Activity to make the link to the Entity. But in the UI the Regarding field only allows one Entity at a time; I need multiple related Entities.

        (3) Instead, I could use my own “Simple” Lookup fields on the Activity, one for each of the multiple Entities required, then the Activity appears in the Subgrid using the relationship of the Simple Lookup. But it does not appear in the Activity Feed Timeline.

        So, I need to write some code that lets one of those 3 lookups be the “master”, and pushes the selected items to the others; thereby the Activity appears in the Activity Feed Timelines and Subgrids on the multiple entities that are related to the Activity.

        Example: Multiple Simple Lookup fields as the UI, pushes the selected Entities to the Party List or the Regarding field. The Party List would work, but looks goofy. The Entities are not to be emailed or invited to the appointment, but would appear in the Appointment.RequiredAttendees, etc. fields alongside the genuine parties . I don’t know if the Regarding field will accept multiple Entities programmatically (it doesn’t through the out-of-the-box UI).

        I’m going to try it, and if you’re interested I’ll post a comment back.

  3. Lawrence Duff avatar
    Lawrence Duff

    Dataverse traps out programmatic adding of a multiple Entities in the array of the setValue(array) method …

    Value should be of type: LookupValue[] of size not more than one.: Parameter Name: value
    Session Id: xxxx-xxxx-xxxx-xxxx-xxxx
    Correlation Id: xxxx-xxxx-xxxx-xxxx-xxxx
    Event Name: onchange
    Function Name: pp.attributeOnChange
    Web Resource Name: pp_AppointmentPartyRegardingSetter
    Solution Name: Active
    Publisher Name: DefaultPublisherorgxxxx
    Time: Fri Aug 04 2023 15:36:01 GMT+0100 (British Summer Time)

    My only option is to concat to the Parties array the Entities chosen from the Simple Lookups on my Appointment/Email etc. Forms, and setValue(array) onto the PartyList.

    It will look goofy and require user education to know that they are nothing to do with the genuine appointment/email invitees (luckily, at least these Entities are not mail enabled), but rather to drive a consistent Subgrid & Activity Feed Timeline.

    1. Furkan Karacan avatar

      Hi Lawrence, rather than confusing users I would try to keep it as simple as possible. Maybe you can consider add an another table and create your lookups there. Then associate with activities. Now you can instead choosing a lookup by creating a record in that table.

  4. Maqsood Ahmed avatar
    Maqsood Ahmed

    Hi Furkan thanks for the post.

    I tried but unable to save it using dataset.save() method

    const lookupValue = new Array();
    lookupValue[0] = new Object();
    lookupValue[0].id = ‘826f7197-375b-ef11-bfe3-000d3a856da8’;
    lookupValue[0].name = ‘test-parent’;
    lookupValue[0].entityType = “businessunit”;

    record.setValue(‘parentbusinessunitid’, lookupValue);
    record.save()

    I am getting this error

    “Network error when using Patch function: Value must be a data entity record”

    1. Furkan Karacan avatar

      Hi Maqsood, where do you run this code? Is it a web resource file?

      1. Maqsood Ahmed avatar
        Maqsood Ahmed

        It’s React component inside PCF control.

        I’m using dataverse tables with relationship. Here is relationship details:

        * BusinessUnit (table):

        1. businessunitid (primary column)

        2. parentbusinessunitid (column pointing towards businessunitid)

        Pls help. Thanks

      2. Furkan Karacan avatar

        Hi, can you please share a screenshot? I think something is missing.

      3. Maqsood Ahmed avatar
        Maqsood Ahmed

        sure Furkan pls take a look https://ibb.co/Wyj39RD

  5. Maqsood Ahmed avatar
    Maqsood Ahmed

    here is my table details https://ibb.co/w6PjmDd

    1. Furkan Karacan avatar

      Hi Maqsood,

      Please debug your record.setValue() function to ensure the parameters you pass are valid. You can also try logging a lookup value to the console to see the expected structure, and then modify your object accordingly.

      1. Maqsood Ahmed avatar
        Maqsood Ahmed

        Hi Furkan, Got it. Thanks. One more thing. Do I also need to set Linking in Dataset before creating new record.

  6. Furkan Karacan avatar

    Hi Maqsood, as far as i know, you should.

Leave a reply to Furkan Karacan Cancel reply