Search Results for 'script'
-
Search Results
-
Hi Script Guys,
It’s more or less not a scripting question but how to use it in TapForms.
I do have a form with a script field, the script behind, just as simple example:
function Script() { var hello_world = "Hello World"; return hello_world; } Script();I assumed TapForms will set this field value to “Hello World” when a recordset is added or changed ?
But it’s empty, independant of which type of value (text, number, …)
The console log shows the value, but the field stays empty.I additional added document.saveAllChanges; without any change.
Or is such a script field to be used only to set another field value by using recordset.setFieldValue(ID,value) ?
Topic: Checkbox Flip Flop
A long time ago, I created one checkbox to mark a particular state transition. A while later I ended up realising I need another checkbox because I messed up the first one and it could be ambiguous. So I decided to create a sort of flip flop where if you check one check box, it unchecks the other one.
To solve this I created a new script field that watched the two fields and then used itself to store the historic state of these fields as a serialised JSON value. When it runs, it checks the values and then sees if the field changing is different to what it’s seen previously. It then decides if it wants to toggle the checkbox and returns a JSON string with the current field states to be stored for next time.
To work this uses the Logger module and it also includes a reference to the Form Logger that I used to find the field ID of the script fields. The script is listed below:
// Import the logger and output the header. document.getFormNamed('Script Manager').runScriptNamed('Logger'); logger.consoleHeader('Checkbox Flip Flop', 'Shipments'); // Get the current values of the check boxes. var confirmed = record.getFieldValue('fld-2adb9ba8cdd048bbbb614d46b415ada5'); var unverified = record.getFieldValue('fld-abdb319b7db74fc39812a94778c433cc'); // Get the current value for this script field. var oldState = record.getFieldValue('fld-2cd0296ea0884c8cba3640d8e33f010b'); // Create a copy of the new state for later. var newState = {'confirmed': confirmed, 'unverified': unverified}; // This logs all of the script fields in the form using the form logger. document.getFormNamed('Script Manager').runScriptNamed('Form Logger'); formLogger.dump({'type': ['script']}); // For debugging, log the various states. logger.logMessage(<code>Current State: ${oldState}</code>); logger.logMessage('New State: ' + JSON.stringify(newState)); // If we have an old state, we parse it out (otherwise it'd be null). if (oldState) { oldState = JSON.parse(oldState); } else { oldState = { 'unverified': false, 'confirmed': false }; } // If the old state was unverified and not confirmed and this is confirmed... if (oldState['unverified'] && !oldState['confirmed'] && confirmed) { // Update the unverified field to not be set. logger.logMessage('Unsetting unverified flag since confirmed flag toggled'); record.setFieldValue('fld-abdb319b7db74fc39812a94778c433cc', false, false); } // If the old state was confirmed and not verified and this is now unverified... if (oldState['confirmed'] && !oldState['unverified'] && unverified) { // Update the confirmed field to not be set. logger.logMessage('Unsetting confirmed flag since verified flag toggled'); record.setFieldValue('fld-2adb9ba8cdd048bbbb614d46b415ada5', false, false); } // Save the changes to the database. document.saveAllChanges(); // Turn the newState into a JSON string. var result = JSON.stringify(newState); logger.consoleFooter('Checkbox Flip Flop', 'Shipments'); // Return the JSON string for the next execution run. result;There’s one issue with this in that the UI doesn’t seem to update properly when the checkbox state is changed. At some point I’ll turn it into a combobox and fix my fields but until then, I have some automation to keep my state consistent.
Topic: Form Logger Script
I had a need to get the script field ID which isn’t readily available in the Script Editor and decided to do a quick form script so that I could pick it up later. If you’ve used my ‘Script Manager’ form convention, then just create a new form script in it called ‘Form Logger’.
You can call it without arguments to get all fields dumped, here’s some sample calling code:
document.getFormNamed('Script Manager').runScriptNamed('Form Logger'); formLogger.dump()If you want to filter by type, you can specify the field type:
document.getFormNamed('Script Manager').runScriptNamed('Form Logger'); formLogger.dump({'type': ['script', 'text']});And this is it’s output in the console:
Form Logger: Shipments Type Filter: ["script","text"] Layout: default fld-c487390743c947969cbe661cff596855: text Tracking Number fld-0950c430cb0c41f79c51d43a544b366b: text Carrier fld-7a29242731d9451092c92d8586dbc94a: script Tracking Details fld-dddcdc15e1c44aa4a99bba6314dc7a07: script Tracking URL Autocomplete fld-9bee4fd0919e4b439e7bed8d5a6c1053: script Tracking Number Reformat fld-2cd0296ea0884c8cba3640d8e33f010b: script Checkbox Flip Flop fld-a9a09a61a9e949199e92d47c02fd364c: text Shipment Order ID fld-f7aba3b5ddd6430cb8e9a211e0086c84: script Date Propagation fld-45c8f29ec3214fc5aacbba73e2de3142: script Order ID FK*Note:* This won’t look as formatted in the console because it uses a proportional font.
Here’s the full script, it’s pretty simple:
// ========== Form Logger Start ========== // // NAME: Form Logger // VERSION: 1.0.1 // CHANGELOG: // 1.0.1: Added support for custom form name and fix for "getId" rename. /** * Method to dump out the field ID's, type and names with the ability to filter by type. */ if (formLogger == undefined) var formLogger = (function() { return { dump: function({type = [], layout = 'default', formName = ''} = {}) { let targetForm = form; if (formName) { targetForm = document.getFormNamed(formName); } console.log('Form Logger: ' + targetForm.name); console.log('Type Filter: ' + JSON.stringify(type)); console.log('Layout: ' + layout); var fields = targetForm.getFields(); for (field in fields) { if (type.length == 0 || type.includes(fields[field].fieldType)) { console.log(fields[field].getId() + ": " + fields[field].fieldType + "\t" + fields[field].name); } } } } })(); // ========== Form Logger End ========== //As you might gather, I want to have a layout additional filter but haven’t gotten there yet. Maybe in a later version.
Hi Brendan,
I work on a CRM system using Tapfroms where I would like to guide the user (eg, me:) along the way. I used custom layouts to add a new client, quotations, orders, and sales. Each has its own form and custom layout.
On the client form, I would like to have buttons for ‘quotation’, ‘order’, ‘invoice’. etc. Clicking a button would trigger a script that creates a new record in the respective form. Next I would like to display directly the custom layout to enter the needed information, eg, form.showLayoutNamed(‘my fancy quotation layout’);.
I haven’t seen anything in the documentation supporting this. Is this something that could be added? This would make it much easier to implement flow-like applications and guide the user along.
Thanks,
DanielCheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricksTopic: Auto backup iOS
Hi, is there a way to create a backup automatically, for example when leave app, or sometime when working on the bdd?
Only on iOS please, a script can handle the job to click on the + on the Backup & Restore window ?!
Thank you for yours ideas!


