Back to all How-tos

Passing Record Data to a front-end web page

If you hadn’t yet realized, generating Web contents using Connect can be a little tricky depending on what you want to achieve. One of the restrictions that becomes apparent is that the fields and tables from your Record (the Data Model pane) is not available to the client that requests a web page. This is generally fine – you don’t want personal or restricted data to end up visible to the client.

In some cases however, assuming a web page has been properly secured and you don’t care about that data appearing in the source code (it’s an employee, for example, accessing client data), then this trick can be very useful if you are using a framework such as jQuery to build dynamic, interactive pages.

Passing on the full record to the page

The most basic (and less secure) way to pass on the information, is to simple insert the complete record straight into the source code. A Script where the selector is simply head (and it must be exactly that), with the following code, does the trick in a pinch:

results.prepend("<script>var data_record = " + JSON.stringify(record) + "</script>");

For the developers out there, yes the record is an object. Using JSON.stringify() converts all properties into a string version of the structure, including all fields and tables.

What if I only want part of the record?

You can add just the information from a detail table, by refering to it in the code instead of just the record object:

results.prepend("<script>var my_table = " + JSON.stringify(record.tables.my_table) + "</script>");

And of course having a single field is perfectly acceptable:

results.prepend("<script>var customer_id = '" + record.fields.customer_id + "'</script>");

Or more than one field (here we build the string first, for readability):

var my_string = "<script>";
my_string += "var customer_id = '" + record.fields.customer_id + "'\n";
my_string += "var invoice_uuid = '" + record.fields.invoice_uuid + "'\n";
my_string += "</script>";

A variable defined in one script can be accessed by any script defined after it. By using the prepend() function, the script will be the first executed meaning it will be accessible by any other script in the template.

Leave a Reply

Your email address will not be published. Required fields are marked *