Search Results for 'script'
Tap Forms Database Pro for Mac, iPhone, iPad and Apple Watch › Forums › Search › Search Results for 'script'
-
AuthorSearch Results
-
December 1, 2021 at 1:08 PM #45888
Sam Moffatt
ParticipantYou should put the files you want to import in the “Script folder access” folder (see preferences for the document) to make sure Tap Forms can get to it otherwise MacOS may block read access. If you imported that CSV via the normal TF import process previously (File > Import) then MacOS will have remembered that you selected the CSV file and give TF access to it. This is part of the security sandboxing that prevents apps from accessing files that you haven’t explicitly granted the application access. There are also some special folders that MacOS also adds extra security to which might require you to give it access via System Preferences > Security & Privacy > Privacy.
December 1, 2021 at 1:03 PM #45887In reply to: Create new field with referenced values
Sam Moffatt
ParticipantThere are two types of scripts: field scripts and form scripts.
When you create a new field via the “Fields” tab and set the type to “script”, then that’s a field script. Field scripts are evaluated when ever a field they reference (via
record.getFieldValue) changes and should automatically be re-evaluated. Field scripts are good for this sort of use case where you have related data that you need to store. These results of the scripts are cached with the records so that once they are evaluated, they don’t get re-evaluated unless something they depend on changes, you refresh the record (including all records) or you hit the “update records when saving” in the script editor for the field script. Ideally field scripts refer to information primarily in the record in question.Form scripts are in the “Scripts” tab and are designed to be executed on demand. These can work on a single record to do an on demand action or can be used to work on multiple records. I use form scripts to do one of imports of data either through web services or parsing fields in a record, to do bulk imports and creation of records from external systems or to do one off data clean up work. The key is that form scripts have to be explicitly executed whilst field scripts will be implicitly updated by Tap Forms for new records or when referenced fields update.
Good to hear it’s finally working for you though and hopefully this explanation makes sense.
November 30, 2021 at 7:25 PM #45875In reply to: Create new field with referenced values
Steven Daniel
ParticipantI just found the “update records” checkbox. It is in the Field Script Editor – I was working in the script editor under scripts, if you follow me. That checkbox is not in that script editor where I was working, as you can see in the last screenshot.
So now I’m still getting no results, but I wonder which script editor I should be using? Does it matter? I’ve tried it in each one and still getting zero results.
November 30, 2021 at 6:54 PM #45873In reply to: Create new field with referenced values
Steven Daniel
ParticipantI don’t understand this. Now the same script is giving no results. At first it had the same result (4 names for one record) – now zero (attached). I have tried quitting out, and starting from scratch and still nada.
Is this funkiness of TF? Or maybe I did something but I don’t think so.Also – I did try refreshing from the bottom of the list when I did have the working script. It said it was going through all the records but nothing changed.
And for the life of me I cannot find the “update records when saving” checkbox in the script editor. Aargh!
Attachments:
You must be logged in to view attached files.November 30, 2021 at 6:18 PM #45871Sam Moffatt
ParticipantThere is probably a record that doesn’t have a string value in it which is causing the
spliterror. Updated the full script to shuffle theparseDatefunction, add a quick guard to returnundefinedwhen thedateStringisn’t truthy and fix the expiry/issue mismatch.// this imports the Papa Parse script form.runScriptNamed('PapaParse'); // replace with your field ID's // Passport var passport_number_id = 'fld-9fe227057cdf44bfa62ad8a97cc6a62a'; var nationality_id = 'fld-0039b290b2054881ac8f004c01903c6f'; var country_id = 'fld-6e861fab76b9457bb625953cece54c96'; var date_of_issue_id = 'fld-0154d8f9ce384e708502fdd775d7bfb1'; var date_of_expiry_id = 'fld-df90736c929549cf8b863666077937fe'; var issuing_authority_id = 'fld-d03c8c1e5fe64e4dada673cb4a6ed322'; var place_passport_seen_id = 'fld-a23576f1e56b48539434de96f5afda23'; function parseDate(dateString) { if (!dateString) { return undefined; } let pieces = dateString.split("/"); return new Date(pieces[2], pieces[1] - 1, pieces[0]); } function Import_Passport() { let filename = "file:///Users/victor/Desktop/Passport.csv"; let csvFile = Utils.getTextFromUrl(filename); if (!csvFile) { console.log("No CSV file?"); return } var output = Papa.parse(csvFile); // abort if there are any errors and log to console. if (output.errors.length > 0) { console.log(errors.join("\n")); return; } // read each line for (let line of output.data) { //var newRecord = document.getFormNamed("Passport").addNewRecord(); //document.getFormNamed("Other Form").addNewRecord() let date_of_issue_string = line[3]; let date_of_expiry_string = line[4]; let date_of_issue = parseDate(date_of_issue_string); // using Sam's date parse function let date_of_expiry = parseDate(date_of_expiry_string); // using Sam's date parse function var newRecord = document.getFormNamed("Passport").addNewRecord(); newRecord.setFieldValues({ [passport_number_id]: line[0], [nationality_id]: line[1], [country_id]: line[2], [date_of_issue_id]: date_of_issue, [date_of_expiry_id]: date_of_expiry, [issuing_authority_id]: line[5], [place_passport_seen_id]: line[6], }); document.saveAllChanges(); } } Import_Passport();-
This reply was modified 4 years, 1 month ago by
Sam Moffatt. Reason: forgot to close the backtick
November 30, 2021 at 6:14 PM #45870In reply to: Create new field with referenced values
Sam Moffatt
ParticipantYup! If you don’t want to override the values then the first script if it’s working for you will do the trick and you don’t need to worry about the other one.
There is a check box in the script editor for “Update records when saving” which will update all records in your form. The reason to keep it deselected is so it doesn’t update all records because you might save during development, it might not be the right result so you don’t want to have to wait for it to update every record before finishing. The other way is to use the refresh button at the bottom of either the record to refresh just the record or at the bottom of the list to refresh all of the records in the form. It’ll recalculate the calculation and script fields for you automatically. Once you’ve done this once, Tap Forms should keep things up to date each time you make a change but if it doesn’t for what ever reason you can just click the record refresh to force an update the to record in question (also Command+R).
November 30, 2021 at 12:31 PM #45865Victor Warner
ParticipantBrendan, Sam,
Thank you both for the suggestions. I have incorporated what you have both suggested but I am obviously still doing something wrong. The csv file is imported and:
1. the first date (date_of_issue) is imported and formatted correctly, but
2. not the second (date_of_epxiry), which is ignored; and
3. the first date is used (date_of_issue) in the field for (date_of_epxiry).The error message is “Date import: TypeError: undefined is not an object (evaluating ‘dateString.split’), line:(null)”
The complete script is:
// this imports the Papa Parse script form.runScriptNamed('PapaParse'); // replace with your field ID's // Passport var passport_number_id = 'fld-9fe227057cdf44bfa62ad8a97cc6a62a'; var nationality_id = 'fld-0039b290b2054881ac8f004c01903c6f'; var country_id = 'fld-6e861fab76b9457bb625953cece54c96'; var date_of_issue_id = 'fld-0154d8f9ce384e708502fdd775d7bfb1'; var date_of_expiry_id = 'fld-df90736c929549cf8b863666077937fe'; var issuing_authority_id = 'fld-d03c8c1e5fe64e4dada673cb4a6ed322'; var place_passport_seen_id = 'fld-a23576f1e56b48539434de96f5afda23'; function Import_Passport() { let filename = "file:///Users/victor/Desktop/Passport.csv"; let csvFile = Utils.getTextFromUrl(filename); if (!csvFile) { console.log("No CSV file?"); return } var output = Papa.parse(csvFile); // abort if there are any errors and log to console. if (output.errors.length > 0) { console.log(errors.join("\n")); return; } // read each line for (let line of output.data) { //var newRecord = document.getFormNamed("Passport").addNewRecord(); //document.getFormNamed("Other Form").addNewRecord() let date_of_issue_string = line[3]; let date_of_expiry_string = line[4]; let date_of_issue = parseDate(date_of_issue_string); // using Sam's date parse function function parseDate(dateString) { let pieces = dateString.split("/"); return new Date(pieces[2], pieces[1] - 1, pieces[0]); } let date_of_expiry = parseDate(date_of_issue_string); // using Sam's date parse function function parseDate(dateString) { let pieces = dateString.split("/"); return new Date(pieces[2], pieces[1] - 1, pieces[0]); } var newRecord = document.getFormNamed("Passport").addNewRecord(); newRecord.setFieldValues({ [passport_number_id]: line[0], [nationality_id]: line[1], [country_id]: line[2], [date_of_issue_id]: date_of_issue, [date_of_expiry_id]: date_of_expiry, [issuing_authority_id]: line[5], [place_passport_seen_id]: line[6], }); document.saveAllChanges(); } } Import_Passport()I would very grateful for any further help.
Victor
November 30, 2021 at 5:58 AM #45864In reply to: Create new field with referenced values
Steven Daniel
ParticipantSorry – I’m a little dense. Yes – since I don’t think I’ll be overriding the values – just using the synonyms in the Atlas Synonyms file. So in that case I’ll just use the first script (keeping Atlas synonyms as a script field) you suggested that did return 4 values in one record? Is that Correct? Ignore the script for text field that you wrote above?
So, since the first script one is close and does return 4 results in one record – what to change next to update all the records in the entire file? I don’t understand why it only updated the single record.
November 29, 2021 at 11:36 PM #45860In reply to: Create new field with referenced values
Sam Moffatt
ParticipantNo this is good! So the box in the left is the result of the field script, if those are what you expect then we’re pretty close. Is you save out, you should see in your record where that field is the values you’re expecting. Again if you don’t need to override the values ever (e.g. it always comes from that other form), then instead of the text field you can just use the script field and it’ll auto-update for you.
For the text field, make sure the one you’re testing the value is empty and if that still doesn’t do the trick, you can disable the guard (
/*starts a multi-line comment and*/ends a multi-line comment):function recordsLoop() { var atlas_synonyms_id = 'fld-REPLACEME'; /* if (record.getFieldValue(atlas_synonyms_id)) { return; } */ var link_to_form_id = 'fld-f835affdf6f34d9890a55d38f985d8cd'; var scientific_name_id = 'fld-7c6e6899c8c2400b8127a82857cd619d'; var link_to_form = record.getFieldValue(link_to_form_id); var entries = []; for (var index = 0, count = link_to_form.length; index < count; index++) { var scientific name = link_to_form[index].getFieldValue(scientific_name_id); if (scientific_name) { entries.push(scientific_name); } } var joined = entries.join(', '); record.setFieldValue(atlas_synonyms_id, joined); document.saveAllChanges(); return joined; } recordsLoop();By the way, don’t forget to replace
fld-REPLACEMEwith the ID of the field ID of the text field you want to update.November 29, 2021 at 10:59 PM #45858In reply to: iOS open Entry form directly
Sam Moffatt
ParticipantI haven’t seen any issues with what looks like happening, I did a scan of four items the other day into my purchases document without any issues where each record was updated. It’s the same template/script I mailed you (at some point I’ll fix the double encoding for the spaces too). I can shoot you a copy of the template if that helps.
That said in thinking about this a bit, I think you cache the
parametersobject within the JS interpreter and there was a discussion at some point about removing it but it was pointed out that this makes it easy to re-run and debug scripts without needing the full round trip.November 29, 2021 at 10:56 PM #45857In reply to: Scheduled /Automated Export of Records:
Sam Moffatt
ParticipantYes! You’ve done a great job of a bunch of features that I heavily use, scripting being the biggest and CouchDB sync being the other one I asked for back in the day with the innocuous statement of “Cloudant seems like CouchDB” I think :D I think answering a bunch of question is the least I can do :D
November 29, 2021 at 8:27 PM #45856Brendan
KeymasterI think maybe just modifying the code as follows might work?
let date_of_issue_string = line[3]; let date_of_expiry_string = line[4]; let date_of_issue = parseDate(date_of_issue_string); // using Sam's date parse function let date_of_expiry = parseDate(date_of_expiry_string); // using Sam's date parse function newRecord.setFieldValues({ [passport_number_id]: line[0], [nationality_id]: line[1], [country_id]: line[2], [date_of_issue_id]: date_of_issue, [date_of_expiry_id]: date_of_expiry, [issuing_authority_id]: line[5], });When you call
record.setFieldValues({});Tap Forms just loops through those values callingrecord.setFieldValue(field_id, value);, which would do the right thing if given a Date value.Maybe I’m missing something, but I’m not sure what the need for the extra field would be or even a Field Script. Doesn’t the code read the file, parse, and then set the values on the fields in the record? If so, then it could just transform the date values properly in JavaScript before writing them to the actual Date fields in the record.
November 29, 2021 at 8:14 PM #45855In reply to: Scheduled /Automated Export of Records:
Brendan
KeymasterI really appreciate you answering questions here Sam. You’ve been a great help over the past many years. And hopefully I’ve done an ok job adding some of the more power features you’ve requested.
In fact Prashant, you can actually thank Sam for me adding the scripting engine into Tap Forms as I think he was the one who originally requested it.
November 29, 2021 at 7:25 PM #45851In reply to: Create new field with referenced values
Steven Daniel
ParticipantThanks again. Not there yet. I tried both and they run BUT only return 4 names in the box at bottom left of the script page (attached) and there are no entries in the Synonym field in the Atlas Field…
Attachments:
You must be logged in to view attached files.November 29, 2021 at 7:00 PM #45850In reply to: Create new field with referenced values
Sam Moffatt
ParticipantYeah, I think maybe the calculation field could be improved to provide a way to do string concatenation of linked fields as an option but right now that’s not an option and scripting is kind of the only way. It’s a little verbose but does at least get the job done.
Ok, let’s try to get the basic script field returning a value. This should get things a little further along (I copied it out of the image so the OCR needed some tuning and I think I got everything):
function recordsLoop() { var link_to_form_id = 'fld-f835affdf6f34d9890a55d38f985d8cd'; var scientific_name_id = 'fld-7c6e6899c8c2400b8127a82857cd619d'; var link_to_form = record.getFieldValue(link_to_form_id); var entries = []; for (var index = 0, count = link_to_form.length; index < count; index++) { var scientific name = link_to_form[index].getFieldValue(scientific_name_id); if (scientific_name) { entries.push(scientific_name); } } return entries.join(', '); } recordsLoop();That should work as a field, you can run it in the script editor and it should show you something.
———-
For the second one where you update a text field I can see you had a crack at that too. This line seems like a weird replacement or something:
var atlas_synonyms = record.getFieldValue(atlas_synonyms_id);It should have had an ID in it, try clicking on the
IDbutton and it’ll insert something like:var atlas_synonyms_id = 'fld-1234';Your line should be different and longer number but that’ll be the format. I do wonder if the snippet system did something odd (I ran into that the other day on iOS). Minor tweak to my original post to use this variable:
function recordsLoop() { var atlas_synonyms_id = 'fld-REPLACEME'; if (record.getFieldValue(atlas_synonyms_id)) { return; } var link_to_form_id = 'fld-f835affdf6f34d9890a55d38f985d8cd'; var scientific_name_id = 'fld-7c6e6899c8c2400b8127a82857cd619d'; var link_to_form = record.getFieldValue(link_to_form_id); var entries = []; for (var index = 0, count = link_to_form.length; index < count; index++) { var scientific name = link_to_form[index].getFieldValue(scientific_name_id); if (scientific_name) { entries.push(scientific_name); } } var joined = entries.join(', '); record.setFieldValue(atlas_synonyms_id, joined); document.saveAllChanges(); return joined; } recordsLoop();Running that in the script editor should also result in the text field being updated as well plus returning the value for debugging. You do still need to insert the ID for the text field that you want to use at the top where it says
fld-REPLACEME.Let’s give those a spin and see what we get back :D
-
This reply was modified 4 years, 1 month ago by
-
AuthorSearch Results