Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Show related records in text field

Featured Replies

Hi everybody

I'm putting a "notes" field on my invoice screen that shows all notes entered that's related to the open invoice in one scrollable text field (the notes are created and modified in another window and in this one instance I would like to avoid a portal). 

It's 90% working, however it is displaying notes that are related to every Invoice in the system, not just the active invoice record:

Let ( [ 

ReturnSub = "\n" ;



header = "";
header = "";


aNotes = Quote ( GetValue ( Substitute ( GetFieldName ( Notes::id_Invoice ) ; "::" ; ¶ ) ; 1 ) ) & " a" ;
bInvoices = Quote ( GetValue ( Substitute ( GetFieldName ( Invoices::id ) ; "::" ; ¶ ) ; 1 ) ) & " b" ;


aKeyPrime = "a." & Quote ( GetValue ( Substitute ( GetFieldName ( Notes::id_Invoice ) ; "::" ; ¶ ) ; 2 ) ) ;
aDateTime = "a." & Quote ( GetValue ( Substitute ( GetFieldName ( Notes::StampCreated_DateTime ) ; "::" ; ¶ ) ; 2 ) ) ;

aUser = "a." & Quote ( GetValue ( Substitute ( GetFieldName ( Notes::StampCreated_User ) ; "::" ; ¶ ) ; 2 ) ) ;
aNote = "a." & Quote ( GetValue ( Substitute ( GetFieldName ( Notes::Note ) ; "::" ; ¶ ) ; 2 ) ) ;

bKeyPrime = "b." & Quote ( GetValue ( Substitute ( GetFieldName ( Invoices::id ) ; "::" ; ¶ ) ; 2 ) ) ;


q = 
"SELECT " & aDateTime & " , " & aUser & " , " & aNote & "
FROM " & aNotes & " 
INNER JOIN " & bInvoices & " ON " & aKeyPrime & " = " & bKeyPrime & " 
ORDER BY " & aDateTime & " ASC " ;


result = ExecuteSQL ( q ; Char ( 9 ) ; "|*|" ; "" ) ] ;  

 

List ( header ; Substitute ( result ; [ ¶ ; ReturnSub ] ; [ "|*|" ; ¶ ]  ) )  )

Is someone with a better eye able to pinpoint where I made the mistake that's telling the calculation to display all related records? I was more than sure this would have eliminated this problem:

INNER JOIN " & bInvoices & " ON " & aKeyPrime & " = " & bKeyPrime & "

 

Funnily enough, it was just stated in the other thread of yours that for a single-table query you can establish a relationship with a simple

WHERE TableA.primaryKey = TableB.foreignKey 

In your case, delete the INNER JOIN line and add e.g.


FROM " & aNotes & " 
WHERE " & aKeyPrime & " = ?" ;
result = ExecuteSQL ( q ; Char ( 9 ) ; "|*|" ; Invoices::id ) ] ; 

and see if this works reliably.

btw, is aKeyPrime a good name for a foreign key field?

Edited by eos

And again I say... a lot of old-style FM devs overthink the JOIN thing in SQL queries where it is not needed.  A simple WHERE clause like eos has will do just fine

  • Author

Thank you, guys.

I am learning. I appreciate your critiques. 

  • 2 weeks later...

Something that may help: if you build your "joins" like you build your relationships "on the graph", then they are far less complex. The advantage of eSQL is that you don't need to actually build on the graph. But the query would be the same:

TableA::field_pk = TableB::field_fk (as FM would show this in the Database Design Report)

becomes (in eSQL):

TableA.field_pk = TableB.field_fk.

The OUTER JOIN is helpful when you need all parents returned whether they have children or not. Otherwise just use JOIN or just the more simple equality in the WHERE.

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.