Thursday 18 May 2017

How to use RecordRef, KeyRef and FieldRef - Dynamics NAV 2017

This is the most unanswered question of all time, so I just thought of writing a blog on this.

This blog covers basic understanding of RecordRef, FieldRef and KeyRef in Dynamics NAV 2009 R2 to 2017 versions.
As we all know, RecordRef and FieldRef plays an important role while programming in NAV.

I hope this blog should help for many beginners.

Following are the topics covered in this blog.

1. How and when to use GETTABLE Function and OPEN Function
2. How and when to use RecordRef
3. How and when to use FieldRef
4. How and when to use KeyRef


1. GETTABLE:

 This function gets the table of a Record variable and causes the RecordRef to refer to the same table. Any filters that are applied to the RecordVar are also applied to 
the RecordRefVar.  Another way to select the table to which a RecordRef refers is to use the OPEN Function(RecordRef) and specify a table number in the parameters.

Ex: TableRecRef.GETTABLE(ContactRec);
Here, ContactRec variable gets the contact table using GETTABLE function and causes TableRecRec (a RecordRef variable) to refer to contact table.

OPEN Function:
Causes a RecordRef variable to refer to a table, which is identified by its Table ID
Ex: SearchIn.OPEN(RecRef.NUMBER);
SearchIn is a RecordRef variable which refers directly to a table, now you will be thinking which is the table it is actually trying to open here. Well, for now lets assume RecRef is holding the value of TableRecRef i.e Contact table.
So, in order to open a table through RecRef you should specify ID of the table, if you 've already used GETTABLE in your code then the same variable's ID can be passed here. That's where actually you see the difference of GETTABLE and OPEN Function but having a close look both does a same job. You can either use GETTABLE or OPEN function to create a reference of the table through RecordRef.

2. RecordRef
Let us assume you have created a function called "Search". This Search function will search for the string "fur" in any part  "Search Name" field of "Contact" table, "Customer" table and "Vendor" table.
Contact Table having Search Name field
Customer Table having Search Name filed


Now, All the 3 tables, "contact", "customer" and "vendor" table has the 3rd field name as "Search Name".
So, here comes the meaning of RecordRef. You use a RecordRef object in functions that must apply to more than one table, not to a specific table.
Instead of writing 3 functions, 1 for customer, 1 for contact and 1 for vendor table we can use one common  function which can use one RecordRef variable, which Gets the required table dynamically based on your selection.

3. FieldRef
Let's say you've selected "contact" table and the RecordRef is holding this table.
Now, you need to use a field "Search Name" from this table to search for the string "fur". How do you do that? That's where FieldRef concept comes into picture. This complex data type identifies a field in a table and gives you access to this field.
Suppose, i've created a fieldref variable "SearchFieldNo". I need this variable to refer to "Search Name" Field using. 
"Search Name" field as a field No. 3 in every existing tables.
So, this is how I can create a Field Reference for "Search Name" field.
Ex.:
SearchFieldNo := 3; //SearchFieldNo is a fieldref variable which should refer to Field No. 3 "Search Name".
SearchInFld := SearchIn.FIELD(SearchFieldNo); //SearchIn is a RecordRef variable which holds selective table.

4. KeyRef
We're now done with most of the things, we have now a RecordRef variable which is holding to the "Contact" table and referred to the field "Search Name". Now, if you need to know what is the key the contact table is holding, then you need to use KeyRef. So, KeyRef is used to Identify a key in a table and the fields in this key.

If you need to identify the primary key fields, then use the index 1 to identify it.
Ex.
KeyNo := 1; //Keynumber of which we want to get the name.
Table.OPEN(RecRef1.NUMBER); //Table is a new RecordRef variable, and RecRef1 will be holding contact table.
Key := Table.KEYINDEX(KeyNo); // Create a key reference to the key we want to get the name from.

I guess, those who read this blog might have understood the best use of RecordRef, FieldRef, GetTable function and Open function.

If you've any questions, please drop in a mail.
mail id:  mail2harikiranbm@gmail.com

1 comment:

  1. The Commercial Credit Information report provides a wide range of data, from multiple sets of credit data based on business needs to business risk scores and more. You can control the financial responsibility of potential commercial lenders and minimize the possibility of current and serial failure. You can check your commercial cibil report online on credit bureau platforms like CreditQ as well as you can report your business defaulters.

    ReplyDelete