FM_Name_ID ( )

Function stats

Average user rating
5.0000
37
82
9999
Support
FileMaker 8.0 +
Date posted
25 November 2008
Last updated
21 April 2011
Version
Recursive function
No

Author Info
 Fabrice

76 functions

Average Rating 4.4

author_avatar



 

Function overview

Prototype

FM_Name_ID  ( _Name_ID;   _TLFSV;   _fileName;   _layoutName )


Parameters

_Name_ID  a database element name or ID


_TLFSV  the element type (


_fileName  for current file, just send an empty (


_layoutName  only if _TLFSV is


Description

Tags:  value list   table occurrence   script   layout   ID   hard-coding   field   Design   debug  

Hard-coding a field name or a layout name... makes it difficult to rename it without breaking a solution.
Simply use FileMaker internal IDs instead.
To get the ID of a FileMaker item, pass its name to the function, then use the function the other way around in your calculations (pass the ID to it, and get the name).
Full documentation here : http://www.1-more-thing.com/FileMaker-Avoid-Hard-coding.html

Examples

Sample input

FM_Name_ID ( "MyLayout" ; "L" ; "" ; "" )
FM_Name_ID ( 112 ; "L" ; "" ; "" )


Sample output

112
"MyLayout"

 

Function code

/* FM_Name_ID ( _Name_ID ; _TLFSV ; _fileName ; _layoutName )

Fabrice Nordmann

http://www.1-more-thing.com


Avoids hard-coding in FileMaker by using IDs instead of names

Full documentation here : http://www.1-more-thing.com/FileMaker-Avoid-Hard-coding.html

v.1.7, Dec 2010
Accepts table parameters such as FM_Name_ID ( table::field ; "T" ; "" ; "" )
v.1.6, Mar 2009
Accepts field parameters such as FM_Name_ID ( 1065234::24 ; "F" ; "" ; "" )
v.1.5.2, Feb 2009
Removes the repetition number if a fieldname with a repetition number is passed ( field[n]). Does not handle field names with [ anymore.
v.1.5.1, Dec 2008
Documentation update (thanks to Kevin Frank's comment)
v.1.5, Oct 2008
Documentation error fix
Returns an empty result if item not found
v.1.4, Aug 2008
Bug fix (major) : could return erroneous result if an item ID was found in another ID.
v.1.3, July 2008
A field ID can be obtained by passing its full name (with table occurrence) in _Name_ID and leaving _layoutName empty
e.g. FM_Name_ID ( "myTable::myField" ; "F" ; "" ; "" )
v.1.2, June 2008
Doesn't require PositionValue
v.1, May 2008
Requires: PositionValue ( _text ; _searchValue ; _start ; _occurrence )

if _Name_ID is an ID, returns the name
if _Name_ID is a name, returns the ID

_Name_ID is the name or the ID of a Table occurrence, a layout, or a field, a script or a value list
_TLFSV can be set to "Table", "Layout", "Field", "Script", or "ValueList". Or only the first character (T,L,F,S,V)
_fileName is optional (empty means current file)
_layoutName is valid only if _TLFSV contains "Field". Empty means current layout (or the field table occurrence, read the note below). The layout should be tied to the table occurrence of the field.

Note: for fields (_TLFSV = "F"), if you use the full field name (table::fieldname) AND an empty _layoutName parameter, the function will assume you are referring not to current layout but to the table occurrence found in the _TLFSV.

Note: since FileMaker 10 and the GetFieldName function, this function is almost useless with a F (field) parameter. It remains valid for other items.

*/



Let ([

    // _fileName is optional
    _fileName = Case ( IsEmpty ( _fileName ) ; Get ( FileName ) ; _fileName ) ;
    _layoutName = Case ( IsEmpty ( _layoutName ) ; Case ( PatternCount ( _Name_ID ; "::" ) ; GetValue ( Substitute ( _Name_ID ; "::" ; ¶ ) ; 1 ) ; Get ( LayoutName )) ; _layoutName ) ;
    _layoutName = Case ( Int ( _layoutName ) = _layoutName and Length ( _layoutName ) = 7 ; FM_Name_ID ( _layoutName ; "T" ; _fileName ; "" ) ; _layoutName ) ;
    _p = _Name_ID ;

    _t = Left ( _TLFSV ; 1 ) ;     // allow _TLFSV to be only the first character (T,L,F,S,V)
    _t = Case ( _t = "T" ; "Table" ; _t = "L" ; "Layout" ; _t = "F" ; "Field" ; _t = "S" ; "Script" ; _t = "V" ; "ValueList" ) ;
    _p = Case ( _t = "Field" and PatternCount ( _p ; "[" ) ; Left ( _p ; Position ( _p ; "[" ; 10000 ; -1 ) -1 ) ; _p ) ; // remove the repetition number
    _p = Case ( _t = "Field" and PatternCount ( _p ; "::" ) ; Replace ( _p ; 1 ; Position ( _p ; "::" ; 1 ; 1 ) + 1 ; "" ) ; _p ) ; // for fields, do not take TO
    _p = Case ( _t = "Table" and PatternCount ( _p ; "::" ) ; Replace ( _p ; Position ( _p ; "::" ; 1 ; 1 ) ; 99999 ; "" ) ; _p ) ; // for TO, do not take field name
    _endOfString = "( \"" & _fileName & "\"" & Case ( _t = "field" ; "; \"" & _layoutName & "\"" ) & ")" ;
    _names = Evaluate ( _t & "Names" & _endOfString ) ;
    _ids = Evaluate ( _t & "IDs" & _endOfString )
];

Case ( _p = GetAsNumber ( _p ) ;

    // ID -> Name
    Case ( not IsEmpty ( FilterValues ( _p ; _ids )) ;
        GetValue ( _names ; Let ([ _text = _ids ; _item = _p ; _adj = ¶ & _text & ¶ ] ; PatternCount ( Left ( _adj ; Position ( _adj ; ¶ & _item & ¶ ; 1 ; 1 ) + 1 ) ; ¶ )))
    ) ;

// Name -> ID
    Case ( not IsEmpty ( FilterValues ( _p ; _names )) ;
        GetValue ( _ids ; Let ([ _text = _names ; _item = _p ; _adj = ¶ & _text & ¶ ] ; PatternCount ( Left ( _adj ; Position ( _adj ; ¶ & _item & ¶ ; 1 ; 1 ) + 1 ) ; ¶ )))
    )

)
)

// ===================================
/*

    This function is published on FileMaker Custom Functions
    to check for updates and provide feedback and bug reports
    please visit http://www.fmfunctions.com/fid/82

    Prototype: FM_Name_ID( _Name_ID; _TLFSV; _fileName; _layoutName )
    Function Author: Fabrice (http://www.fmfunctions.com/mid/37)
    Last updated: 21 April 2011
    Version: 2.10

*/
// ===================================

 

Comments

Kevin Frank
05 December 2008



Hi Fabrice, that is an amazing CF!

One minor error -- the reference to "_Table_Layout_Field_Script_ValueList" needs to be changed to "_TLFSV"

Regards,
Kevin
 Bug report
Fabrice
05 December 2008



Hi Kevin !
Thanks for the comment. Yes, I use it all the time, and it simply changed my life.
Indeed, I had changed the name of the parameter to make it shorter. It's fixed now.

Thanks !
Fabrice
  General comment
Fabrice
20 December 2008



Thanks to Kevin's comment, I updated the function documentation (was a feature, not a bug, but was undocumented)

Note: for fields (_TLFSV = "F"), if you use the full field name (table::fieldname) AND an empty _layoutName parameter, the function will assume you are referring not to current layout but to the table occurrence found in the _TLFSV.
  General comment
Fabrice
07 February 2009



Just to mention a FileMaker Advisor article with more details about this function (subscribers only) :
http://my.advisor.com/doc/19569
  General comment
Genx
27 February 2009



Yo Fabrice, wipe the Fm Functions injected info from the bottom of your code - it end's up appearing twice if you don't :)
  General comment
kaostika
16 June 2009



I get the use of it now. I used hard coded field names in a script and my coworker change the TO names and a field name. Now I am screwed. So this will help.

  General comment
esod
08 September 2009



When I use this function, or any variation of it (See Brian Dunning's website, IDFromName and NameFromID, or,even, FileMaker Pro 10 Bible pg 493 "Design Functions") FMSv9 crashes. Error code 802 (Unable to open file), and FMS stops serving files all together. FMS computer reboot is required for repair. It's very strange. Does anyone care to comment? Thanks
(Edited by Eric Sod on 08/09/09 )
  General comment
LionAxel
16 October 2012



If you input a name that contains letters and numbers (example: myField_1) the function breaks.
I presume the problem is in
--> Case ( _p = GetAsNumber ( _p )
How can you fix?
  General comment
Fabrice
16 October 2012



Hi,
there must be something in your testcase, because I can't reproduce the issue.
  General comment
LionAxel
18 October 2012



Oh sorry sorry, I must have been a mistake probably caused from my test made late night :)
Forget everything 'and thanks anyway for your prompt reply
Greetings.
  General comment
Mario
15 November 2016



Hello,
I have an error like this (translated from italian):"too many parameters in the function" in the fouth line where i signed with this simbol ###:

 _layoutName = Case ( Int ( _layoutName ) = _layoutName and Length ( _layoutName ) = 7 ; FM_Name_ID ( _layoutName ; "T" ; _fileName ; "" #### ) #### ; _layoutName ) ;

... but I'm not able to solve the problem. (Filemaker 13 Advanced - Windows 7)

Please, can someone help me?
Thanks for patience.
Regards
  General comment