Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

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

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?

  • Suggested answer
    DAnny3211 Profile Picture
    DAnny3211 9,272 Super User 2024 Season 1 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

  • Sayen Zhang Profile Picture
    Sayen Zhang 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
    XM-22040801-0 Profile Picture
    XM-22040801-0 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;

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans