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.