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 :
Small and medium business | Business Central, N...
Suggested Answer

What is best practice for displaying a non-primary key of a lookup? OnLookup or CalcField 2 seperate fields?

(0) ShareShare
ReportReport
Posted on by

I am struggling to understand the best methodology for handling fields that lookup related tables. For example, let's say I have a table of Contacts and a table of Opportunities. On the Opportunity record, I have a field that is a lookup to a related Contact record. The contact has both a Contact No. value and a Contact Name value, but only the Contact No. value is a unique primary key - however I don't want to display the Contact No. field on the Opportunity record, I only care about the Name.

My options are:

1. Create 2 separate fields, with the Name field being read-only CalcField and updated every time the Contact No. field is changed.

field(50128; "Contact"; Code[20])
    {
        Caption = 'Contact';
        DataClassification = ToBeClassified;
        TableRelation = Contact.No;
    }
field(50130; ContactName; Text[80])
    {
        Caption = 'Contact Name';
        FieldClass = FlowField;
        CalcFormula = lookup(Contact."Name" where("No." = field("Contact")));
    }

2. Create an OnLookup trigger to reset the Contact value to Contact.Name

field(50100; Contact; Text[100])
        {
            DataClassification = CustomerContent;
            Caption = 'Contact';

            trigger OnLookup()
            var
                Contact: Record Contact;
            begin
                Contact.Reset();
                if Page.RunModal(Page::Contact, Contact) = Action::LookupOK then
                    Contact := Contact.Name;
            end;
        }

While creating 2 fields seems clunky and unnecessary, if I only use a single field and the OnLookup trigger I am unable to properly reference this field later, as I only have access to the non-unique / non-primary key value of name, which makes doing a lookup of Contact by name inaccurate.

I have the same question (0)
  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,111 Moderator on at

    I would say that option 1 is best practice, it is at least how i would do it myself and i think that is the way Business Central generally does it.

  • Community Member Profile Picture
    on at

    Do you have any tips for only needing to display 1 field (the name field)?

    As of right now it's poor UX to make my users identify that the Name field is incorrect, navigate to the No. field, update it, then review the Name field to make sure it was calculated correctly.

  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,111 Moderator on at

    Well you need to contact no. to do the lookup but if you know it holds a value you do not have to display it.

    Then you can just set the visible property for the field to false in the page. Then only the name will be displayed.

    field(50128; "Contact"; Code[20])

       {

           Caption = 'Contact';

           Visible = false;

           DataClassification = ToBeClassified;

           TableRelation = Contact.No;

       }

  • Community Member Profile Picture
    on at

    But what if I want to update this value? I cannot click on the Contact.Name field - correct? I must update the Contact.No field again and recalculate Contact.Name, unless there is something I am not understanding?

  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,111 Moderator on at

    If you open the opportunity page for the contact page then you will most likely already have the contact no. relation established and then the contact no. field will already be initiated with the correct contact no.

    For that you use the RunPageLink propety.

    docs.microsoft.com/.../devenv-runpagelink-property

  • Suggested answer
    Community Member Profile Picture
    on at

    I believe I've solved the issue using a combination of both. The lookup field continues to store the display the name and allows the lookup to be updated, but I've also created a second static field to simply hold the number.

            field(50101; Contact; Text[250])
            {
                DataClassification = CustomerContent;
                Caption = 'Contact';
    
                trigger OnLookup()
                var
                    Contact: Record Contacts;
                begin
                    Contact.Reset();
                    if Page.RunModal(Page::Contacts, Contact) = Action::LookupOK then
                        Contact := Contact.Name;
                        ContactNo := Contact.No;
                end;
            }

    Now the user only has to maintain a single field, but the primary key / no. value is always up to date and accessible while still being hidden!

  • matthias_rabus Profile Picture
    160 on at

    Looks nice, one small issue:

    You need a begin/end here:

    if Page.RunModal(Page::Contacts, Contact) = Action::LookupOK then BEGIN

     Contact := Contact.Name;

     ContactNo := Contact.No;

    END;

    Otherwise good solution

  • Suggested answer
    YUN ZHU Profile Picture
    95,729 Super User 2025 Season 2 on at

    Hi, here are some ideas that hopefully will give you some hints as well.

    https://yzhums.com/22220/

    Thanks.

    ZHU

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 > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,664

#2
YUN ZHU Profile Picture

YUN ZHU 960 Super User 2025 Season 2

#3
Jainam M. Kothari Profile Picture

Jainam M. Kothari 773 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans