Skip to main content

Command Palette

Search for a command to run...

How to Get and Set Lookup Fields in Dynamics 365 CRM (JavaScript, Power Automate, Plugins, Web API)

In Dynamics 365 CRM, lookup fields link records (e.g., Contact to Account). You can get or set them using JavaScript, flows, plugins, or the Web API.

Updated
3 min read

In this blog, we’ll cover how to work with lookups in four ways:

  • JavaScript (form scripts)

  • Power Automate (Flow)

  • Plugins (C#)

  • Web API (OData / REST API)

Finally, we’ll walk through real-world examples you can apply directly in your projects.

🔹 1. Handling Lookups with JavaScript in Dynamics 365 CRM

Form scripts are useful when you want to manipulate lookup fields directly on CRM forms.

✅ Get a Lookup Value in JavaScript

function getLookupValue(executionContext) {
    var formContext = executionContext.getFormContext();
    var lookup = formContext.getAttribute("parentaccountid").getValue();

    if (lookup != null) {
        var id = lookup[0].id;       // GUID of the record
        var name = lookup[0].name;   // Display name
        var entity = lookup[0].entityType; // Entity logical name

        console.log("ID: " + id + " | Name: " + name + " | Entity: " + entity);
    }
}

✅ Set a Lookup Value in JavaScript

function setLookupValue(executionContext) {
    var formContext = executionContext.getFormContext();

    var lookupValue = [{
        id: "B0D4F3F4-1D3A-4D2A-9A54-8A1B2E6A9F6C",
        name: "Contoso Ltd",
        entityType: "account"
    }];

    formContext.getAttribute("parentaccountid").setValue(lookupValue);
}

🔹 2. Handling Lookups in Power Automate (Flow)

In Power Automate, lookup fields need to be set using the @odata.bind notation.

✅ Get a Lookup Value in Flow

When retrieving a Contact, a lookup field looks like this:

"_parentaccountid_value": "b0d4f3f4-1d3a-4d2a-9a54-8a1b2e6a9f6c"

✅ Set a Lookup Value in Flow

To set a lookup, use:

"parentaccountid@odata.bind": "/accounts(b0d4f3f4-1d3a-4d2a-9a54-8a1b2e6a9f6c)"

💡 In Flow expressions, you can build it dynamically:

concat('/accounts(', outputs('Get_Account')?['body/accountid'], ')')

🔹 3. Handling Lookups in Plugins (C#)

In Dynamics 365 Plugins, lookups are handled using the EntityReference class.

✅ Get a Lookup Value in Plugin

if (entity.Contains("parentaccountid") && entity["parentaccountid"] is EntityReference lookup)
{
    Guid id = lookup.Id;
    string name = lookup.Name;       // Sometimes null depending on context
    string logicalName = lookup.LogicalName;
}

✅ Set a Lookup Value in Plugin

entity["parentaccountid"] = new EntityReference("account", 
    new Guid("B0D4F3F4-1D3A-4D2A-9A54-8A1B2E6A9F6C"));

🔹 4. Handling Lookups in Dynamics 365 Web API

When working with the Dataverse Web API, lookup values are accessed using the _fieldname_value convention.

✅ Get a Lookup Value via Web API

GET [Organization URI]/api/data/v9.2/contacts(11111111-2222-3333-4444-555555555555)?$select=fullname,_parentcustomerid_value

Response:

{
  "fullname": "John Doe",
  "_parentcustomerid_value": "b0d4f3f4-1d3a-4d2a-9a54-8a1b2e6a9f6c",
  "_parentcustomerid_value@OData.Community.Display.V1.FormattedValue": "Contoso Ltd",
  "_parentcustomerid_value@Microsoft.Dynamics.CRM.lookuplogicalname": "account"
}
  • _parentcustomerid_value → The GUID of the lookup

  • FormattedValue → The display name

  • lookuplogicalname → The entity type

✅ Set a Lookup Value via Web API

PATCH [Organization URI]/api/data/v9.2/contacts(11111111-2222-3333-4444-555555555555)
Content-Type: application/json

{
  "parentcustomerid_account@odata.bind": "/accounts(b0d4f3f4-1d3a-4d2a-9a54-8a1b2e6a9f6c)"
}

🔹 Real-World Examples

Let’s look at some common business scenarios where you’ll need to get or set lookup fields.

Example 1: Setting a Contact’s Parent Account (JavaScript)

When a user selects an Industry, automatically assign the Contact to a default Account:

if (formContext.getAttribute("industrycode").getValue() === 42) {
    var accountLookup = [{
        id: "D1C5E8F2-91AA-4C3E-8A14-7C32F1B9F77B",
        name: "Default Industry Account",
        entityType: "account"
    }];
    formContext.getAttribute("parentaccountid").setValue(accountLookup);
}

Example 2: Assigning a Record Owner in Power Automate

When a new Lead is created, assign it to a specific user:

"ownerid@odata.bind": "/systemusers(5a4b8f3f-1122-44aa-bbbb-998877665544)"

If a Contact is marked as "VIP", set its Parent Account to “VIP Clients”:

if (entity.Contains("new_vipstatus") && (bool)entity["new_vipstatus"] == true)
{
    entity["parentaccountid"] = new EntityReference("account", 
        new Guid("E2F6B8D1-99F1-4B4D-8CC9-11B2A3D3F1AA"));
}

Example 4: Web API — Linking a Case to a Customer

PATCH [Organization URI]/api/data/v9.2/incidents(11111111-2222-3333-4444-555555555555)
Content-Type: application/json

{
  "customerid_contact@odata.bind": "/contacts(b0d4f3f4-1d3a-4d2a-9a54-8a1b2e6a9f6c)"
}

🎯 Conclusion

  • JavaScript → Use getValue() and setValue() with {id, name, entityType} objects.

  • Power Automate (Flow) → Use @odata.bind with /entityname(guid) syntax.

  • Plugins (C#) → Work with EntityReference.

  • Web API → Use _lookupfield_value for retrieval and @odata.bind for updates.

Understanding how to get and set lookups across different technologies in Dynamics 365 CRM makes your solutions more flexible, scalable, and maintainable.

How to Get and Set Lookup Fields in Dynamics 365 CRM