Is field part of primary key?

DarkSide
Member Posts: 46
Is there a way through code to tell if a field is part of a tables primary key?
I would like to be able to determine this through code because the table is being referenced via RecordRef. So I don't necessarily know what table I'm dealing with. I want to be able to determine if the field I'm working with is part of the primary key of RecordRef. This way if I need to change the value of the field I'll know if I need to do a MODIFY or a RENAME.
Sorry to sound repetitive but I want to be clear, this may seem like an odd request.
Thanks in advance.
I would like to be able to determine this through code because the table is being referenced via RecordRef. So I don't necessarily know what table I'm dealing with. I want to be able to determine if the field I'm working with is part of the primary key of RecordRef. This way if I need to change the value of the field I'll know if I need to do a MODIFY or a RENAME.
Sorry to sound repetitive but I want to be clear, this may seem like an odd request.
Thanks in advance.
My mommy says I'm special
0
Answers
-
If your are working with RecordRef, you can also work with KeyRef. Have a look at Codeunit 423 Change Log Management, where you find some code to extract the primary key of a table:
KeyRef1 := RecRef.KEYINDEX(1); FOR i := 1 TO KeyRef1.FIELDCOUNT DO BEGIN KeyFldRef := KeyRef1.FIELDINDEX(i); IF i = 1 THEN ChangeLogEntry."Primary Key" := STRSUBSTNO('%1=%2',KeyFldRef.CAPTION,FormatValue(KeyFldRef,RecRef.NUMBER)) ELSE IF MAXSTRLEN(ChangeLogEntry."Primary Key") > STRLEN(ChangeLogEntry."Primary Key") + STRLEN(STRSUBSTNO(', %1=%2',KeyFldRef.CAPTION,FormatValue(KeyFldRef,RecRef.NUMBER))) THEN ChangeLogEntry."Primary Key" := COPYSTR( ChangeLogEntry."Primary Key" + STRSUBSTNO(', %1=%2',KeyFldRef.CAPTION,FormatValue(KeyFldRef,RecRef.NUMBER)), 1,MAXSTRLEN(ChangeLogEntry."Primary Key")); CASE i OF 1: BEGIN ChangeLogEntry."Primary Key Field 1 No." := KeyFldRef.NUMBER; ChangeLogEntry."Primary Key Field 1 Value" := FormatValue(KeyFldRef,RecRef.NUMBER); END; 2: BEGIN ChangeLogEntry."Primary Key Field 2 No." := KeyFldRef.NUMBER; ChangeLogEntry."Primary Key Field 2 Value" := FormatValue(KeyFldRef,RecRef.NUMBER); END; 3: BEGIN ChangeLogEntry."Primary Key Field 3 No." := KeyFldRef.NUMBER; ChangeLogEntry."Primary Key Field 3 Value" := FormatValue(KeyFldRef,RecRef.NUMBER); END; END; END;
This code will give you the numbers of the fields which appears in the Primary Key. The name of the field can be looked up in the Virtual Table "Field".No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)0 -
Because I had the same problem in the past, I wrote a small function for that:
IsPrimaryKeyField(RecRef : RecordRef) : Boolean FOR I := 1 TO RecRef.KEYINDEX(1).FIELDCOUNT DO IF RecRef.KEYINDEX(1).FIELDINDEX(I).NUMBER = FieldID THEN EXIT(TRUE); EXIT(FALSE);
Timo Lässer
Microsoft Dynamics NAV Developer since 1997
MSDynamics.de - German Microsoft Dynamics Community - member of [clip]0 -
Thanks! You really helped me out!My mommy says I'm special0
-
Sounds great - perhaps you could put [Solved] in the 1st posts Subject Line.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions