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,036 through 1,050 (of 2,989 total)
  • Author
    Search Results
  • #46456

    In reply to: Table default values

    John McArthur
    Participant

    Hi Daniel,

    I attempted your script with the appropriate FLD’s inserted for the toggle switch & link but couldn’t get it to work.

    The links work fine when using the Globe but when the Toggle box is ticked nothing happens.

    Tested the script via the console and the variables were returning; ‘open’ = ‘false’, even when ticked & ‘link’ = ‘undefined’, even though link field was populated with a valid link.

    Just to make sure I have created the Table correctly; it should have a toggle field, a link field and a script field with your script inserted?

    I’m probably doing something very wrong as usual! (Pesky newbies!) :-)

    #46452

    In reply to: Table default values

    Daniel Leu
    Participant

    Hi John, yes, this script assumes that the link address is already in the proper format. If clicking on the globe works, then the script will be able to open the file as well.

    Cheers, Daniel

    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

    #46441
    Daniel Leu
    Participant

    From the documentation: Sets the value on the specified field and optionally executes scripts that would run by default when the field value changes.

    So it should say: …optionally _disables_ scripts that would run by default…?

    Thanks!

    Cheers, Daniel

    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

    #46438
    Brendan
    Keymaster

    Ugh… oops. Sorry about this misunderstanding. The default for that flag is already true, so subscripts already get run when you set a value, even without that parameter. Sorry for causing the confusion.

    #46425

    In reply to: Table default values

    Daniel Leu
    Participant

    Hi John, I run into the same issue…. I created a toggle field in the first column. Then I added a field script that is triggered by this checkbox field. When the checkbox is set, then the file is opened. This way, I no longer have to look for the globe at the end of the URL.

    I did add an additional field that only displays the filename since in don’t care about the file path in my case.

    Set open_id and link_id values according to your form. open_id is the id of the checkbox flag that triggers the script and link_id is the id of the website field that contains the URL of your file.

    function Attachment_Open() {
    
    	const open_id = 'fld-xxx'; // checkbox Id
    	const link_id = 'fld-xxx'; // URL Id
    
    	let open = record.getFieldValue(open_id);
    	let link = record.getFieldValue(link_id);
    	
    	if (link && open){
    		link = link.replace(/[ ]/g, '\ ')
    
    		console.log("Opening URL " + link);
    		Utils.openUrl(link);
    
    		// clear opening flag
    		record.setFieldValue(open_id, false)
    		form.saveAllChanges()
    	}
    }
    
    Attachment_Open();

    At the end, I clear the checkbox flag so it doesn’t stay checked.

    Cheers, Daniel

    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

    #46424
    JB Be
    Participant

    Thank you so much! What a kind help. What incredible responsiveness!

    The proposed solution is obviously already able to address the more complicated case of multiple email addresses in the same field. I must admit, however, that even despite the detailed explanation, I am still challenged. Because the ‘Duplicate’ function is missing, Tap Forms Organizer remained rather worthless for me and I have put it aside for months (but thanks to your help, that will probably change now!). Now I have to work my way back into the overall functionality of Tap Forms Organizer from scratch.

    What would it look like if there was only one sole email address in the field to assess? Would the script then be much simpler and even more transparent for the novice willing to learn?

    #46420
    Sam Moffatt
    Participant

    The “OR” use case is a little different because you need to split out the individual values first. Thinking a little about this what might work is a form of email addresses and then a link to form M:M relationship. Each email address is split up and added to the other form and linked back. Since it’s M:M you can link multiple parent records and then count that way to unique email addresses.

    I created a simple form called “Source Form” and added a name and email address field to it. I then added a second form called “Emails” and added a field called “Email” to it. In the “Source Form” I went back and added a Link to Form to the “Emails” form, set it to M:M and ticked “Show inverse relationship” (I do that last because there used to be a bug where if you changed the type from 1:M to M:M, the inverse field wouldn’t update properly, I think it’s fixed). Back in our “Emails” form, I added a script field that just returns the length of the Link from Field that was created (it’s as simple as this: record.getFieldValue('fld-2b0ad00a96bd4cf4b20dca95899a7a5a').length; where the field ID is the ID of the link from form field). Last piece is to grab the md5sum.js file and add it to the “Source Form” as a new form script and then create another form script that I called “Extract Emails” with this in the contents:

    form.runScriptNamed("md5sum");
    
    var source__email_id = 'fld-4e8071f8f3ce4a75a66954f6a3c636ef';
    var emails__email_id = 'fld-7e880e79867345549fb04f377412fefd';
    var emails_link_id = 'fld-f5d00c535c3c437a87a262f6d0f434e4';
    
    var emailsForm = document.getFormNamed("Emails");
    
    function Extract_Emails() {
    
    	for (let currentRecord of form.getRecords()) {
    		let emails;
    		try {
    			emails = currentRecord.getFieldValue(source__email_id).split(' ');
    		} catch(e) {
    			console.log("Error processing record: " + e);
    			console.log(currentRecord.getUrl());
    			continue;
    		}
    		
    		for (let email of emails) {
    			console.log(email);
    			let recordHash = "rec-" + md5(email);
    			let candidateRecord = emailsForm.getRecordWithId(recordHash);
    			if (!candidateRecord) {
    				candidateRecord = emailsForm.addNewRecordWithId(recordHash);
    				candidateRecord.setFieldValue(emails__email_id, email);
    			}
    			currentRecord.addRecordToField(candidateRecord, emails_link_id);
    			document.saveAllChanges();
    		}
    	}
    }
    
    Extract_Emails();
    

    The first line imports the md5sum script we created. The next three var statement lines are the field ID’s for the “Email” field in the “Source” form, the field ID of the “Email” field in the “Emails” field and then the field ID of the link to form field in the “Source” form that links back to the “Emails” form. The emailsForm is getting the Tap Forms object representing the “Emails” form.

    The function then iterates over all of the records in the current form (e.g. “Source” form), it then splits up the email field based on a space. You can change this to be a comma or any other character you want. If you need more than one separator you will need to use a regular expression but we’ll skip that. The try/catch is just in case there is something weird to trap the error there and keep processing since we’re dealing with a field value.

    The second for loop is then iterating over the emails in the record and creating a new record ID using the MD5 of the email. This pattern is somewhat risky because we intentionally will create colliding document ID’s that generally Tap Forms will avoid. We do that here to be able to do the key lookup without having to build the index ourselves (TF will do that for us). We check to see if a record already exists using getRecordWithId and if it doesn’t we create it using that same ID using addNewRecordWithId and set he email address. We then have Tap Forms link the “Source” record we’re working with to the new email record and save all the changes.

    The saveAllChanges is inside the loop because I’ve had issues with TF where the links behave weirdly if you manipulate too many of them via scripting without saving. There’s probably a bug somewhere but reproducing it is a challenge so this is my work around.

    This should fill up the “Emails” form with the unique email addresses all linked to their relevant parent forms. I had to do a manual refresh of the form because the script field didn’t populate the links properly. Once I’d verified my duplicates were being detected, I created a saved search on the count of linked records being greater than one.

    Attaching a form template with that in it as an example to play with. The earlier one I did a quick change based on an existing form so not so easy to share because it’s got a bunch of other fields in it but this is a relatively concise use case and should work to handle multiple email addresses as well.

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

    Wouldn’t the false disable executing the script? Isn’t the problem the chaining essentially means that if it’s executed out of order that the value isn’t available from the “earlier” calculations which means that it’ll not have the value required later to work. I think depending on correlated calc fields is probably an antipattern because you’re relying on TF ordering and if there is common code between fields to isolate that into a form script (which you can then include from form.runScriptNamed) so that there is one copy of it you can use elsewhere or setting the different values into empty script fields as noted above.

    #46412
    JB Be
    Participant

    Fantastic. I filed my ‘duplicate’ question more than a year ago (see more upwards) and I am still in need of a solution, too. Thank you for taking this up, Sam. It all looks a little cryptic to me, though, being an informed user but not fluent in scripting…

    In my case, I am using the email-Adress as a unique identifier for 2000+ records. So I am looking for a (scripting) way to find records that have exactly the same email address in the specific field named ’email1′.

    In rare cases, there might be (erroneously) several email addresses in this field. In this case, I would look for records that have the same email address at least once in the specific field named ’email1′.

    Would you think the script above might help?

    #46410
    Brendan
    Keymaster

    Hi Chris,

    Yes, I could probably add a refresh function. form.refreshRecord(aRecord); would probably be a better API.

    I’m not sure if this would help you but there is an alternative setFieldValue() function:

    record.setFieldValue(movie_title_id, 'The Terminator', false);

    From the manual:

    Sets the value on the specified field and optionally executes scripts that would run by default when the field value changes.

    It also evaluates formulas too, not just scripts.

    #46408
    Chris Ju
    Participant

    Ok, wouldn’t have thought it is possible to write into a script field via script. Thanks for the great hint. Will try to implement it in the near future…

    #46402
    Sam Moffatt
    Participant

    Most of the time you can have an empty script field and it seems to work. There was one situation where for what ever reason it didn’t work for me and my work around was to just have the script do record.getFieldValue(scriptFieldId); which returns the value for itself. Since the value for the field is set by your other script field (e.g. record.setFieldValue(scriptFieldId, desiredFieldValue)) this should work properly. This is at the limits of what a script field should do but it does seem to work properly and gives you a read only field.

    #46399
    Chris Ju
    Participant

    Thanks, Sam, but what do mean exactly with

    “you can also work around that by using self referencing script fields that maintain their value set by an external script”?

    Do you have a short example?

    #46394
    Sam Moffatt
    Participant

    Chained fields is going to be complicated because how many times does the refresh need to be called to resolve everything becomes a recursion problem. How do you know when to stop evaluating if someone adds say a time based script that mutates each execution.

    A model I’ve used is having a single script that calculates everything and then stores the data in normal fields. There isn’t a read only option so they can be mutated by the end user but you can also work around that by using self referencing script fields that maintain their value set by an external script.

    #46393
    Sam Moffatt
    Participant

    Are you looking for exact duplicates (all fields identical) or duplicates on a primary or subset of key fields? If you want all fields identical, does this extend to link to form fields, table fields or photo fields and their contents?

    I was working a while back on tooling to do merging of records through the scripting interface to handle when I end up with duplicate shipping records due to key mismatch (sometimes the tracking numbers are changed in transit) but I never got it finished because the UI experience wasn’t something I figured out.

    If you’re after a subset of key fields, I did a quick POC where you create a new field in your source form with the key. I already had a composite key field built using a calculation field I use which looks like this (square brackets for field placeholders):

    IFEMPTY([Marketplace];"No marketplace";
    CONCAT([Marketplace], "/",[Store Name], "/",[Order ID]
    ))
    

    This creates what should be a unique key for the record based on my own metadata (designed to handle direct sales and hosted marketplaces). I then created a new form called “Orders Dedupe” and put in it three fields: a *string* type field called “key”, *link to form* field called “Order Dedupe” and a script field which counts the entries in the order. The link to form field is configured as a JOIN type on the “key” field of the dedupe form and the calculation field in the original form. The script field looks like this (change your ID’s to match):

    function Key_Match_Count() {
    	var order_dedupe = record.getFieldValue('fld-fde68e7d2b384cb2a4452d3ae66bbab1');
    	return order_dedupe.length;
    }
    
    Key_Match_Count();
    

    In this form also create a new saved search that uses the script field and is set to look for values greater than one as those will be the duplicates.

    Last step is to populate this form, go back to your base form and create a new form script. I wrote the script below to scan each record, use an md5sum implementation to create a hash of the key field and then look to see if that record exists in the dedupe form:

    document.getFormNamed("Script Manager").runScriptNamed("md5sum");
    
    var purchase_key_id = 'fld-3e49aaa5bc32429c8f0f0f234878356d';
    var dedupfield__key_id = 'fld-c52906ee940142a0a54fac1a98346afd';
    var dedupForm = document.getFormNamed("Order Dedupe");
    
    function Extract_Purchase_Keys() {
    	for (let sourceRecord of form.getRecords()) {
    		let purchaseKey = sourceRecord.getFieldValue(purchase_key_id);
    		if (!purchaseKey) {
    			console.log("Missing purchase key for record: " + sourceRecord.getUrl());
    			continue;
    		}
    		let purchaseKeyHash = "rec-" + md5(purchaseKey);
    		let dedupRecord = dedupForm.getRecordWithId(purchaseKeyHash);
    		if (!dedupRecord) {
    			dedupRecord = dedupForm.addNewRecordWithId(purchaseKeyHash);
    			dedupRecord.setFieldValue(dedupfield__key_id, purchaseKey);
    		}
    	}
    	document.saveAllChanges();
    }
    
    Extract_Purchase_Keys();
    

    This actually found me a dupe record that I hadn’t found in my orders form when I went back to look at the saved search. It’s a bit of a journey, might turn it into a video at some point when I get some more time.

Viewing 15 results - 1,036 through 1,050 (of 2,989 total)