web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Get the PrimaryName field logicalname of the current entity in a formContext?

(0) ShareShare
ReportReport
Posted on by 10

I'm trying to create a reusable javascript function that sets the "Name" of the current record based on a few supplied parameters that dictate which columns to concatenate from a lookup record on the form.

When the lookup is cleared, the "Name" is null'd. When the lookup is selected, I do an Xrm.WebApi call to get the lookup entity. I then find the fields named in the function parameters, get their values, concatenate them, then set the form entity "Name" field to this value. Everything works fine. I can call this from any lookup control's onchange event.

To make this more reusable though, I'd like a way to dynamically get the PrimaryName field attribute (or it's logical name) from the current entity. My current solution is to pass the logical name of the field as an additional parameter. Easy enough. But I'd really like to do this step dynamically. Any suggestions?

I have the same question (0)
  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at
    RE: Get the PrimaryName field logicalname of the current entity in a formContext?

    Hello,

    You can use getEntityMetadata(). See learn.microsoft.com/.../getentitymetadata

    Example:

    const metadata = await Xrm.Utility.getEntityMetadata("account");
    const primaryNameAttribute = metadata.PrimaryNameAttribute;

  • Sayen Zhang Profile Picture
    on at
    RE: Get the PrimaryName field logicalname of the current entity in a formContext?

    Hi partner,

    This article is about using JavaScript to get the attributes of a record in lookup field: how to get the attributes of a record in lookup field using JS - Dynamics 365 Sales Forum Community Forum

    I hope this article can help you!

    If you have any questions, you can also reply to let us know. Thank you!

    Best Regards,

    Sayen Zhang

  • Suggested answer
    DAnny3211 Profile Picture
    11,376 on at
    RE: Get the PrimaryName field logicalname of the current entity in a formContext?

    hi

    You can use the Dynamics 365 Web API to dynamically retrieve the metadata for the current entity and then find the Primary Name field. Here is an example of how you could modify your function to accomplish this:

    javascript code:

    function setRecordName(entityName, entityId, nameFieldLogicalName, ...lookupFieldLogicalNames) {

     // Get metadata for current entity

     Xrm.WebApi.retrieveEntity(entityName, null)

       .then(function(entityMetadata) {

         // Find Primary Name field

         var primaryNameField = entityMetadata.PrimaryNameAttribute;

         // Use Primary Name field if nameFieldLogicalName parameter not provided

         if (!nameFieldLogicalName) {

           nameFieldLogicalName = primaryNameField;

         }

         // Retrieve lookup entity

         Xrm.WebApi.retrieveRecord(entityId, lookupEntityName, null)

           .then(function(lookupEntity) {

             // Concatenate fields

             var name = "";

             for (var i = 0; i < lookupFieldLogicalNames.length; i++) {

               name += lookupEntity[lookupFieldLogicalNames[i]] + " ";

             }

             name = name.trim();

             // Set record name

             var record = {};

             record[nameFieldLogicalName] = name;

             Xrm.WebApi.updateRecord(entityName, entityId, record);

           });

       });

    }

    In this example, entityName and entityId are the logical name and ID of the current entity, nameFieldLogicalName is the logical name of the field to set as the record name (defaults to Primary Name field if not provided), and lookupFieldLogicalNames are the logical names of the fields to concatenate from the lookup entity.

    By using the retrieveEntity method, we can get the metadata for the current entity, which includes the logical name of the Primary Name field. We can then use this field name in our function.

    DAniele

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Sahan Hasitha Profile Picture

Sahan Hasitha 337

#2
Sohaib Cheema Profile Picture

Sohaib Cheema 104 User Group Leader

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 79 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans