How to resolve a text string variable to an object reference

jonwlongjonwlong Member Posts: 11
I want to be able to refer to an object, such as a table or a field, using a string parameter. I'm familiar with the RecordRef and FieldRef DataTypes. Below is an overview of my concept.
Assuming that MyRecordRef is a RecordRef object, MyField is a FieldRef object and TableNo is an integer.
MyRecordRef.OPEN( intTableNo );
MyFieldRef := MyRecordRef.FIELD( 1 );
This allows me to do Get, Validate etc.. using the ref's. Notice that in both cases, an integer is used as a parameter.

My question is: How to resolve a table name to an integer. Or how can I refer to a table or field, using only it's name. Something like this:
Assume strTableName and strFieldName are string variables
MyRecordRef.OPEN( DATABASE::strTableName );
MyFieldRef := MyRecordRef.FIELD( FIELDS::strFieldName );
Below is an interesting and valid method of resolving a Table reference to an integer. However, even though "Customer" below resolves to an object it is not derived from a string variable, which is what I am trying to accomplish.
MyRecordRef.OPEN( database::"Customer" );
Any discussion on this appreciated.
Thanks,
Jon
Jon Long - ArcherPoint
MCP - Dynamics NAV

Comments

  • fbfb Member Posts: 246
    Here's an idea that uses the Object table:
    PROCEDURE GetTableIDByName(searchName : Text[30]) : Integer
    VAR
      Obj : Record 2000000001;
    BEGIN
      WITH Obj DO BEGIN
        SETCURRENTKEY(Type,Name);
        SETRANGE(Type,Type::Table);
        SETRANGE(Name,searchName);
        IF FIND('-') THEN
          EXIT(ID);
      END;
      EXIT(0);
    END;
    
  • jonwlongjonwlong Member Posts: 11
    I thought about that way but I was really looking for a navision function. I think your object method way will work just fine. Your example is excellent.

    Thanks,
    Jon Long - ArcherPoint
    MCP - Dynamics NAV
  • darrencdarrenc Member Posts: 16
    I am about a year late, but I thought I would post this in case anyone else is searching for a way to convert a field's name to its No. I just found out about the existence of the Field system table.

    variable rec_Field is a record variable with subtype Field

    rec_Field.SETRANGE(TableNo, intTableNo);
    rec_Field.SETRANGE(FieldName, strFieldName);
    rec_Field.FIND('-')
  • peterkozmapeterkozma Member Posts: 1
    darrenc wrote:
    I am about a year late, but I thought I would post this in case anyone else is searching for a way to convert a field's name to its No.

    Wow, thanks! I've been looking for this since ages...
Sign In or Register to comment.