Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Running sql queries in Javascript

Avatar

Former Community Member

Have built a form with numerous rows based on Stefan Cameron's methodology (http://forms.stefcameron.com/2006/10/12/displaying-all-records-from-an-odbc-data-connection/) and have gotten that part to work.

Need to use a sql query to get a subset opf the data, but Stefan's and other examples use Formcalc.  I figure I just need to do a delayed query once I get the query set up and loaded.

Drawing a blank on how and where to load the sql query string.

Thanks in advance,

Patrick

5 Replies

Avatar

Former Community Member

Stefan's blog has an entry on how to do that as well. Just go his site and search on database ...a number of entries will come up.

Paul

Avatar

Former Community Member

Dear Paul,

Did some more digging on Stefan's site, and found the functions to set the database connection up for querying in Javascript.  Boy, there is a lot of code there relative to the FormCalc version.

My next question is Stefan has the functions under the Variables section.  Can I use his other code at initialization to display all the entries of the database (filtered by the select statement) along with this?  Is it smart / prudent to run the PrepareForSQL once at the beginning, or each time like it is currently set up?

Thanks,

Patrick

Avatar

Former Community Member

Stefan's code is very useful when you want to display a single record. Typically I woudl enter a value on a field and use that entered value as part of teh record search in my SQL statement (usually with a delayed open of the DB). If you want to get a subset of records and simply navigate through them that code is not neccessary. You can change the settings when you set up the dataconnection to enter a SQL command and enter it in the 1st panel. Then when the connection is made that SQL statement will run each time.

Capture.GIF

Avatar

Former Community Member

I don't think I can run FormCalc as a function.  Want to run a group of queries when I press a button that already is doing stuff in Javascript.

Is it easier / best to swicth that scripting over to FormCalc?

Avatar

Former Community Member

Got the Formcalc version to work - will have another post on that.

As a warning / cautionary note; do not assume data type in fields is the way you want it when sending it to the SQL statement.  Make sure to do trims and field length trancations, so blanks and other non-printable characters sneek in.