Jump to content
Server Maintenance This Week. ×

Show related records in text field


This topic is 3205 days old. Please don't post here. Open a new topic instead.

Recommended Posts

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 & "

 

Link to comment
Share on other sites

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
  • Like 1
Link to comment
Share on other sites

  • 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.

Link to comment
Share on other sites

This topic is 3205 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

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