Tap Forms app icon half
Tap Forms Forum text image
Blue gradient background

Exchange tips and ideas with the Tap Forms community

Search Results for 'script'

Viewing 15 results - 1,141 through 1,155 (of 3,011 total)
  • Author
    Search Results
  • #45860
    Sam Moffatt
    Participant

    No 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-REPLACEME with the ID of the field ID of the text field you want to update.

    #45858
    Sam Moffatt
    Participant

    I 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 parameters object 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.

    #45857
    Sam Moffatt
    Participant

    Yes! 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

    #45856
    Brendan
    Keymaster

    I 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 calling record.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.

    #45855
    Brendan
    Keymaster

    I 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.

    #45851
    Steven Daniel
    Participant

    Thanks 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.
    #45850
    Sam Moffatt
    Participant

    Yeah, 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 ID button 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

    #45847
    Steven Daniel
    Participant

    Wow – way more complicated and involved than I expected. So I presumably made an error somewhere – please see attached scripts – both show same error message “can’t find variable entries” on line 23. The only difference in the two scripts is I added the last bit of code you suggested to be able to add other synonyms down the road in one, without it in the other.

    I kind of, at a very basic level, get what you’re doing here, but really need to get my head around this scripting more. And really appreciate the help. I thought it would be something relatively simple like if Plant_ID (Atlas) = Plant_ID (Synonyms) enter Scientific_Name (from Synonyms) into the new Atlas Synonym field. Oh well – one step at a time. Thanks again!

    Can you see what went wrong and how to fix it?

    Attachments:
    You must be logged in to view attached files.
    #45846
    Sam Moffatt
    Participant

    Not quite copy paste but leveraging TF to do a lot of heavy lifting here.

    If everything is keyed off plant_ID, create a new “Link to Form” field in the atlas and link it to your synonym form and plant ID fields. That should automatically create links to the synonym records as either new synonyms are added or new plants are added.

    If you’re going to do it all programmatically, I’d swap the type of your synonym field from a “text” field to be a “script” field. Double click on the field name to open the script editor, delete the boilerplate code, select the “Scientific_Name” field on the left on the field list (it should show up once you create the “Link to Form” JOIN field) and then on the bottom left in the “Snippets” it should have “child records loop”. It’ll insert a new function called recordsLoop for you and it’ll have a line that says record.getFieldValue and if statement in the middle of it. After the line that was record.getFieldValue, put in a line like the following:

    var entries = [];
    

    My guess it should look something like this:

    	var atlas_synonyms = record.getFieldValue(atlas_synonyms_id);
    	var entries = [];
    

    We’re going to update the if statement to look something like this:

    		if (scientific_name) {
    			entries.push(scientific_name);
    		}
    

    Lastly there will be a line that says return;, update it to say return entries.join(' '); to get a space separated list of synonyms or return entries.join(', '); to have a comma separated list.

    If you want to keep the synonym field as a separate text field so you can add other values, then you’ll need to do this instead of the return:

    record.setFieldValue('fld-synonymfieldid', entries.join(' '));
    document.saveAllChanges();

    Usually I guard this with a check for if that field has a value already so that it doesn’t overwrite existing values automatically, putting this at the top of the function:

    function recordsLoop() {
    	if (record.getFieldValue('fld-synonymfieldid')) {
    		return;
    	}
    

    This prevents the script from overwriting the text field if there is a value in it already. Double clicking on the field name in the left on Tap Forms should populate the record.getFieldValue code for you (remove any var blah = bit).

    Hope this helps! Not quite copy/paste but hopefully useful enough instructions to get it sorted.

    #45844
    Sam Moffatt
    Participant

    Q1: Yes, this is a three field setup. One text field to import the date as is, a second “date” field to store the canonical representation and a third script field that watches the text field and mutates it to the date field.

    Q2: Swap this line around:

    	var parsedDate = new Date(pieces[2], pieces[0] - 1, pieces[1]);
    

    becomes:

    	var parsedDate = new Date(pieces[2], pieces[1] - 1, pieces[0]);
    

    The new Date() accepts a year, monthIndex (0 to 11, hence the -1) and then day, so we just need to flip the order to match.

    Q3: You can add a new field to land just the imported data and keep your existing date field. That shouldn’t mess up any layout, you’re just adding a text field to land data in during import. The field itself can be hidden most of the time, or if using custom layouts just not shown. Same deal with the script field or you could achieve something similar with a form script. I perhaps err on the side of caution that it’s usually more annoying to detangle data after you’ve imported and mangled it. You could do it inline with the import parse using the same code to convert a passed date string into a JS Date object:

    function parseDate(dateString) {
    	let pieces = dateString.split("/");
    	return new Date(pieces[2], pieces[1] - 1, pieces[0]);
    }
    

    The above assuming UK DD/MM/YYYY format already.

    #45840
    Steven Daniel
    Participant

    Hi – new to Tap Forms and no programming experience. But it seems like it should be simple to accomplish the following:

    I have a form that I added a new text field (synonym). I want to populate all the records in this field with the same value in the related field (species) when the field plant_ID (a number field) value in matches the same plant_ID field in a different form.

    I’m not sure I’m using the correct language. In the first screenshot here I have the form (Synonyms) with just two fields. In the next screenshot I have the new blank Synonym field, that I want to fill with the proper name when the plant_ID matches. So when the plant_ID = 66 it should fill Rhus toxicodendron in the new synonym field in the Atlas form.

    A simple script that I can cut and paste would be appreciated.

    One possible snag – sometimes there are multiple synonyms for the same species. Would it be possible to insert each synonym into the same field? Or how best to handle that.

    Thanks!

    Attachments:
    You must be logged in to view attached files.
    #45839
    Victor Warner
    Participant

    Sam,

    Thank you for the reply.

    **Query 1**.
    Does you code require the date data first to be imported as a text field, and then the script, in another field, with the script attached to the field to format the date?

    **Query 2**.
    How would the script you provided need to be adapted for a non-USA (UK) format?

    **Query 3**.
    More fundamentally, the database I have in Tap Forms contras a lot more Forms – and has many layouts based on and use existing dates – so I am reluctant to engage in a lot of restructuring to add more fields.

    Either to do some processing of the dates in the script you originally provided or outside of Tap Forms – e.g. using a Keyboard Maestro macro to do so.

    The backstory is that I am exporting several hundred records from FileMaker, each record (in Tap Form forms speak) consisting of several forms. I have to do this record by record – as the forms need to be linked together in Tap Forms – and have to be incorporate some manual data which is still recorded on paper.

    Using a script in importing will speed that part of the process – the way I have been doing this until know is to use the File / Import / Records in Tap Forms and automating the process through Keyboard Maestro – but for 6 or 7 forms for each record it take several minutes – and sometimes does not work (if the computer slows down the appearance of a window or keystrokes etc are delayed in appear, etc…).

    #45836
    Sam Moffatt
    Participant

    There are a few posts on Keyboard Maestro and AppleScript on the forum that you might be able to check into for inspiration. Maybe even Automator might help depending on precisely what your use case is.

    If you’re looking to do proper automation, I’d suggest setting up CouchDB and using the sync to that to automate from. I’ve got a set of PHP scripts that provide various mechanisms for grabbing data from CouchDB and putting it elsewhere.

    #45835
    Sam Moffatt
    Participant

    I’ve got a video on using Shortcuts to create a record from a scanned barcode which could be adapted to do something similar with an additional Utils.openUrl(newRecord.getUrl()) to open up the record after it’s been created (and obviously without the barcode scanning portion). If you set this as a shortcut on your home screen somewhere using the Shortcuts widget then you can launch into it and land more or less directly in the form. One caveat is that some times from cold load Tap Forms might load the wrong document or as it loads the right document sometimes doesn’t seem to execute the script. For my own use case I usually just swipe back and re-run the shortcut to get it all to work.

    #45833
    Prashant M
    Participant

    Is there a AppleScript or otherwise(since I couldn’t find in TapForms) to automate exports of records for a given form / database?

    I would be attempting Keyboard Maestro to achieve the same but will love if someone has already solved this problem.

    • This topic was modified 4 years, 3 months ago by Prashant M.
Viewing 15 results - 1,141 through 1,155 (of 3,011 total)