Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Text field ‘OnChangeEvent’
- This topic has 6 replies, 3 voices, and was last updated 5 years, 1 month ago by Stephen Abshire.
-
AuthorPosts
-
November 18, 2019 at 5:37 AM #38079
Stephen AbshireParticipantGreetings,
Is there a way that I can detect when a text field that uses a pick list has had a value change? Say for example it used to be ‘ABC’ and now it is ‘DEF’. How can I catch when the value has changed so I can script it? I suppose a hidden field could be used as a form of global variable but that seems like a hack.
November 18, 2019 at 1:37 PM #38088
BrendanKeymasterIf you have a Script field and you use
record.getFieldValue(field_id)
within the script, Tap Forms will add that field_id to a watch list. So when the value of that field changes, the script will execute.November 18, 2019 at 2:41 PM #38091
Stephen AbshireParticipantThe fields involved are all text because they require user input, yet I want to programmatically modify them based upon certain criteria.
November 18, 2019 at 9:33 PM #38093
Sam MoffattParticipantI use a hidden field to save state of what fields used to be and then compare the new field value against the old one. There is no before change data so you have to grab it yourself.
I also have another technique where with the script field, you can return a value. So you can use the script field itself to store the previous state and by accessing the script field with
getFieldValue
, you can then get at the previous value. When you finish up your script field, you return the previous value. If you have a few different values you want to track, you can useJSON.stringify
andJSON.parse
to serialise and unserialise the field value into an object in JSON.Hopefully that helps.
November 19, 2019 at 8:40 PM #38108
Stephen AbshireParticipantI went with the hidden field method as I wasn’t completely understanding your other method but all good.
November 20, 2019 at 12:36 AM #38117
Sam MoffattParticipantReposting because my original reply got flagged as spam.
I’m uploading a sample form template that shows what I mean. It has two fields in it, a text field and a script field. I also included the form logger script with an update that prints out field ID’s in Javascript variable syntax. That’s used at the top top populate the field ID’s because by default the script editor doesn’t expose script fields. I also use this syntax because it includes the form name which makes it easy to keep track of variables in larger documents with lots of forms and lots of fields. There’s a commented line to ensure that Tap Forms registers this script to the field that works even if the line is commented out. I then grab the text field and the contents of the script field.
For the script field, I check to see if it is set and provide a default dictionary if it isn’t set already. If it is set to a value, I assume it’s JSON and parse it back into an object.
Then at that point it’s a quick comparison of the deserialised JSON payload and your new one. I added a message in so you can see a note, always useful after the fact for some of these scripts even if you hide the field by default, you can unhide it to see what it has in it.
Lastly the script field ends with
JSON.stringify
to return a JSON string as the fields contents. This object can obviously contain what ever you want in it so if you have multiple values instead of setting multiple fields, you just return a value at the end of your field script with the data you need to keep track of from each execution.Sample execution:
Field script:
// Script Field State var script_field_state_formID = "frm-86fd53d1f9ab47548636fc2e2d1d0d35"; var script_field_state__main_field_fldID = "fld-2177d4bf8500482d8fc6242faa80c5e6"; // text var script_field_state__watcher_fldID = "fld-7e997368aef740f68f126d2c8509d148"; // script // Make sure the field is watched to pick up changes. //var main_field = record.getFieldValue('fld-2177d4bf8500482d8fc6242faa80c5e6'); var main = record.getFieldValue(script_field_state__main_field_fldID); var state = record.getFieldValue(script_field_state__watcher_fldID); if (!state) { state = {'previous':undefined,'message':'Initial setting'}; } else { state = JSON.parse(state); } if (state['previous'] != main) { state['message'] = `Field value changed from '${state['previous']}' to '${main}'`; state['previous'] = main; } JSON.stringify(state);
Attachments:
You must be logged in to view attached files.November 20, 2019 at 10:57 AM #38130
Stephen AbshireParticipantThank you and appreciate the code sample. This is actually what I was working on myself but I was having issues that you answered in another thread regarding setting field values that trigger scripts and using the third parameter of ‘false’ when writing field values. Once again appreciate the help!
-
AuthorPosts
You must be logged in to reply to this topic.