# Best way to check FileMaker “Context”?

**URL:** <https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500>\
**Category:** Lounge (Discussions)\
**Created:** [November 27, 2020, 2:57pm UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500 "2020-11-27T14:57:23Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonywhitelive](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/tonywhitelive/32/208_2.png) [@tonywhitelive](https://the.fmsoup.org/u/tonywhitelive)\
**Post date:** [November 27, 2020, 2:57pm UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/1 "2020-11-27T14:57:24Z")

</div>

What is the best way to check FileMaker “Context”?

In FileMaker, Context is determined by the "Layout" \> "Table Occurrence" \> "Source Table" that you are “on”.

When scripting, there is more than one way to check “Context”…

This first image uses a string returned from the Get ( LayoutTableName ) function.

- Pro?: Might be faster?
- Con: Breaks if you rename the Table Occurrence that Get ( LayoutTableName ) returns  
 ![image](https://canada1.discourse-cdn.com/flex030/uploads/fmsoup/original/2X/8/8e0b2e23638e6385a12ec5424f343958b5c6ecaf.png)

The second uses a reference.

- Pro: Does NOT break if you rename the Table Occurrence that Get ( LayoutTableName ) returns
- Con?: Might (or might not) be slower (if you have many 'Else If' branches)?  
 ![image](https://canada1.discourse-cdn.com/flex030/uploads/fmsoup/original/2X/2/2c47729a8c255422755336786fad74cd010eca03.png)

Thoughts?

---

<div class="post-metadata">

**Author:** ![xochi](https://avatars.discourse-cdn.com/v4/letter/x/0ea827/32.png) [@xochi](https://the.fmsoup.org/u/xochi)\
**Post date:** [November 27, 2020, 4:44pm UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/2 "2020-11-27T16:44:24Z")

</div>

GetFieldName( field) will return the Table::Field name, so you could do something like this:

`if [Get(LayoutTableName) = LeftValues ( Substitute ( GetFieldName ( Location ); "::" ; "¶") ;1 )]`

this will tell you which Table you are on, and is resistent to fields or tables being renamed.

But I think I like your IsValid() solution better.

---

<div class="post-metadata">

**Author:** ![Malcolm](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/malcolm/32/196_2.png) [@Malcolm](https://the.fmsoup.org/u/Malcolm)\
**Post date:** [November 27, 2020, 8:26pm UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/3 "2020-11-27T20:26:54Z")

</div>

I use a custom function named "cf CorrectContext." It has one parameter, getFieldNameX.

Internally it contains this code:

```
/*
Given a fully qualified field name, return true if the field matches the current layout table name 
*/

Let (
	parts = Substitute ( getFieldNamex ; "::" ; ¶ ) ; 
	GetValue ( parts ; 1 ) = Get(LayoutTableName)
)

```

This allows me to use the function in script steps and other calculations:

```
If [cf correctContext ( GetFieldName ( field-ref ) )] 
 // code here
End if
```

---

<div class="post-metadata">

**Author:** ![bdbd](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/bdbd/32/620_2.png) [@bdbd](https://the.fmsoup.org/u/bdbd)\
**Post date:** [November 27, 2020, 9:25pm UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/4 "2020-11-27T21:25:59Z")

</div>

I use the same method as @xochi exclusively in my solutions. I see an issue with the IsValid approach: It could return true whether it is the context or a record of the table is reachable by the context.

---

<div class="post-metadata">

**Author:** ![steve\_ssh](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/steve_ssh/32/160_2.png) [@steve\_ssh](https://the.fmsoup.org/u/steve_ssh)\
**Post date:** [November 27, 2020, 9:58pm UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/5 "2020-11-27T21:58:11Z")

</div>

Similar to @Malcolm, I use a CF.

Like Malcolm's CF, it is based on comparing Get( LayoutTableName ) with the TableOccurrence name parsed from the result of GetFieldName.

Function is named something like: CurrentContextMatches( fieldRef )

It's a small convenience, but one that I appreciate having a lot, mostly for readability.

---

<div class="post-metadata">

**Author:** ![Malcolm](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/malcolm/32/196_2.png) [@Malcolm](https://the.fmsoup.org/u/Malcolm)\
**Post date:** [November 28, 2020, 2:44am UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/6 "2020-11-28T02:44:22Z")

</div>

@steve_ssh, I used the parameter name getFieldNameX to remind me which function to pass. In the early days it was also handy because it meant less typing. Nowadays, type-ahead will recognise "gfn", which I find easiest.

---

<div class="post-metadata">

**Author:** ![tonywhitelive](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/tonywhitelive/32/208_2.png) [@tonywhitelive](https://the.fmsoup.org/u/tonywhitelive)\
**Post date:** [November 28, 2020, 3:19am UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/7 "2020-11-28T03:19:29Z")

</div>

> [@xochi](#):
>
> Get(LayoutTableName) = LeftValues ( Substitute ( GetFieldName ( Location ); "::" ; "¶") ;1 )

If you go down that road, this might be better...

Get ( LayoutTableName ) = GetValue ( Substitute ( GetFieldName ( TO::FN ) ; "::" ; "¶" ) ; 1 )

...as LeftValues produces a trailing return.

See below for IsValid() comment.

Thanks.

---

<div class="post-metadata">

**Author:** ![tonywhitelive](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/tonywhitelive/32/208_2.png) [@tonywhitelive](https://the.fmsoup.org/u/tonywhitelive)\
**Post date:** [November 28, 2020, 3:23am UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/8 "2020-11-28T03:23:06Z")

</div>

> [@bdbd](#):
>
> I see an issue with the IsValid approach: It could return true whether it is the context or a record of the table is reachable by the context.

Good point. I use Anchor-Buoy where, as far as I can tell, that should not come up. That said, an approach that works on all types of Relationship Graphs offers advantages.

---

<div class="post-metadata">

**Author:** ![tonywhitelive](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/tonywhitelive/32/208_2.png) [@tonywhitelive](https://the.fmsoup.org/u/tonywhitelive)\
**Post date:** [November 28, 2020, 3:28am UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/9 "2020-11-28T03:28:51Z")

</div>

> [@Malcolm](#):
>
> I use a custom function named "cf CorrectContext." It has one parameter, getFieldNameX.

Below is a riff on your approach....

This would be more readable if I used the Let function. I wonder if there is a performance difference?

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/fmsoup/original/2X/9/94b314add8e1ecb5ea062ae48d82ae2268011462.png)

---

<div class="post-metadata">

**Author:** ![Malcolm](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/malcolm/32/196_2.png) [@Malcolm](https://the.fmsoup.org/u/Malcolm)\
**Post date:** [November 28, 2020, 4:52am UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/10 "2020-11-28T04:52:26Z")

</div>

Comments on these two methods

**method one:**  
Using a hard-coded string in the comparison will come back to bite you. Use a dynamic method to obtain the layout table name.  
As for speed, knowing that your code will not break is better.

**method two:**  
**IsValid()** will not tell you that you are on the correct layout. It will only tell you that you are on a layout that is _within the same Table Occurrence Group_ as the target field being tested.

**IsValid()** will return true for a field reference whenever that field can be found through the chain of relationships. If you are on a layout based on "Invoices" and you ask test Invoice\_Items::id using IsValid() it will return true.

The custom function that I use checks the name of a referenced field against the current layout table name. The object names are obtained dynamically, the field names can change, the layout table occurrence names can change, and it keeps working.

---

<div class="post-metadata">

**Author:** ![Malcolm](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/malcolm/32/196_2.png) [@Malcolm](https://the.fmsoup.org/u/Malcolm)\
**Post date:** [November 28, 2020, 5:08am UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/11 "2020-11-28T05:08:46Z")

</div>

When I think about performance I will address efficiency, simplicity, and stability. If I can obtain a speed bump by addressing those three criteria then I'm happy. If I have a solution that is simple, stable and reasonably efficient, then I'm not going to look for tweaks unless it has become a bottleneck that is causing problems.

However, a custom function is the sort of thing that could be optimised ( because you don't have to interact with the internals) and your variation may use less overhead than mine. In this case, a single sentence comment would be enough to explain the workings of your variation of the function to any future developer.

---

<div class="post-metadata">

**Author:** ![Bobino](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/bobino/32/194_2.png) [@Bobino](https://the.fmsoup.org/u/Bobino)\
**Post date:** [November 28, 2020, 4:41pm UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/12 "2020-11-28T16:41:13Z")

</div>

For anyone who is not familiar with the concept of context and its representations beyond the table occurrence, there is a post here about it: [Context](https://the.fmsoup.org/t/context/761)

---

<div class="post-metadata">

**Author:** ![tonywhitelive](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/tonywhitelive/32/208_2.png) [@tonywhitelive](https://the.fmsoup.org/u/tonywhitelive)\
**Post date:** [December 3, 2020, 4:00pm UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/13 "2020-12-03T16:00:09Z")

</div>

Lines 16 & 19 are more portable

Line 22 is more readable.

Which of the 4 do you like?

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/fmsoup/original/2X/8/8bdb5d8e4fb177ec2332ca51c1aad5442d453c51.png)

---

<div class="post-metadata">

**Author:** ![Bobino](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/bobino/32/194_2.png) [@Bobino](https://the.fmsoup.org/u/Bobino)\
**Post date:** [December 3, 2020, 4:10pm UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/14 "2020-12-03T16:10:21Z")

</div>

I'm sure my answer is predictable, but I like line 22 the best.

---

<div class="post-metadata">

**Author:** ![bdbd](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/bdbd/32/620_2.png) [@bdbd](https://the.fmsoup.org/u/bdbd)\
**Post date:** [December 3, 2020, 4:57pm UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/15 "2020-12-03T16:57:13Z")

</div>

Line 16 is perfectly readable to me. Then again, I use that construct all the time. I suggest comments are missing in all cases. They can reduce the shortcomings of readability and add intent to what the code is trying to do. They allow you to understand what a line of code is suppose to do when you see something like "Missing XYZ" (custom function, in this case). Also useful to troubleshoot code portability issues.

---

<div class="post-metadata">

**Author:** ![Malcolm](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/malcolm/32/196_2.png) [@Malcolm](https://the.fmsoup.org/u/Malcolm)\
**Post date:** [December 3, 2020, 10:04pm UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/16 "2020-12-03T22:04:11Z")

</div>

line 22 wins for clarity. It's an unambiguous description.

If portability must win, I often use a short comment on the first line of the calc to provide a description of purpose.

 ![comments In Scripts.png](https://canada1.discourse-cdn.com/flex030/uploads/fmsoup/original/2X/2/284ac18f10c4168b8f619ed7acd0f466822ddcee.png)

---

<div class="post-metadata">

**Author:** ![tonywhitelive](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/tonywhitelive/32/208_2.png) [@tonywhitelive](https://the.fmsoup.org/u/tonywhitelive)\
**Post date:** [December 7, 2020, 2:39am UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/17 "2020-12-07T02:39:04Z")

</div>

A riff on the script step level comment that you are using is that you might make the comment the same as a custom function name. Then you could easily refactor to a CF after adding the custom function (or renaming an existing custom function that does the same thing).

On the topic of script comments…in recent times, I have been exploring how many comments I can avoid and still maintain readability.

In deciding what to comment and where, it is worth considering that for any given scripting pattern, you could comment at one of the following levels

- In the code itself // sufficiently well named code objects
- Script step // as you have done
- Block // in this example there is a repeating Else If…perhaps a comment only on the first, or at the top of the block?
- Script // sometime I will even add function signatures at the top of the script…not for myself of course…for my friends 😉
- File // for completeness
- System // for completeness
- Separate coding standard document for an organization // very DRY.
- Community // get all FileMaker developers worldwide to agree on a standard…just kidding

Lots of different coding styles to chose from!

---

<div class="post-metadata">

**Author:** ![AndyHibbs](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/andyhibbs/32/3089_2.png) [@AndyHibbs](https://the.fmsoup.org/u/AndyHibbs)\
**Post date:** [December 8, 2020, 8:19pm UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/18 "2020-12-08T20:19:30Z")

</div>

Always add ‘sort by’ or ‘find by’ comment if a stored find, above the script step to allow easy reading through the script.

Are we going off topic? Maybe a good subject for a new post?

---

<div class="post-metadata">

**Author:** ![tonywhitelive](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/tonywhitelive/32/208_2.png) [@tonywhitelive](https://the.fmsoup.org/u/tonywhitelive)\
**Post date:** [July 16, 2024, 6:26pm UTC](https://the.fmsoup.org/t/best-way-to-check-filemaker-context/1500/19 "2024-07-16T18:26:35Z")

</div>

One more way...  
@GFN.TO ( CustomFunctions::\_\_ID ) = Get ( LayoutTableName ) // returns 1 or 0  
... where @customFunction @GFN.TO is here...

> **[FileMaker Custom Function: @GFN.TO ( field\_reference )](https://www.briandunning.com/cf/2754)**
>
> returns the table\_occurrence from a field\_reference

... and here ↴  
// @GFN.TO ( field\_reference )  
// [twdesigns.com](http://twdesigns.com)  
// 2024-07-12\_v2  
// returns the table\_occurrence from a field\_reference  
// [GetFieldName](https://help.claris.com/en/pro-help/content/getfieldname.html)

Let ( [  
~fully\_qualified\_field\_name = GetFieldName ( field\_reference ) ;  
~fully\_qualified\_field\_name.as\_list = Substitute ( ~fully\_qualified\_field\_name ; "::" ; ¶ ) ;  
~table\_occurrence = GetValue ( ~fully\_qualified\_field\_name.as\_list ; 1 )  
] ;  
~table\_occurrence // return  
)
