Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Checkbox Flip Flop
- This topic has 1 reply, 1 voice, and was last updated 3 years, 5 months ago by Sam Moffatt.
-
AuthorPosts
-
October 9, 2019 at 11:07 PM #37039
Sam MoffattParticipantA 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.
July 20, 2021 at 3:45 PM #44831
Sam MoffattParticipantUpdate to this because I realised posting in another thread the OP here had a bug where
newState
wasn’t reset when toggling the corresponding checkbox. This should fix this and remove the form logger because you can now get the script field ID in the field list.Replace the field ID’s (e.g.
fld-2adb9ba8cdd048bbbb614d46b415ada5
) with the field ID’s from your form. The field ID is located under the description box for that field.// 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}; // For debugging, log the various states. logger.logMessage(`Current State: ${oldState}`); 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); newState['unverified'] = 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); newState['confirmed'] = false; } // Save the changes to the database. form.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;
-
AuthorPosts
You must be logged in to reply to this topic.