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,096 through 1,110 (of 3,052 total)
  • Author
    Search Results
  • #46468
    Kirk Williams
    Participant

    Good morning!

    I know populating fields conditionally (based on other fields’ values) is a popular topic, so I’m posing this question as more of a plea for strategic guidance. As I’m sure will become painfully obvious, my experience with coding is entry-level at best.

    The file I’m working with is a pretty basic asset inventory. The example I’ll use is: populating (multiple) spec fields based on the value of another (model) field.

    When adding a new device to my “Computers” form, I need to include specific specifications such as “RAM”, “HD”, “Format”, “OS”, etc. (obviously there are many more).

    Entering these values manually is not only cumbersome but also repetitive since I am often deploying multiple devices with the same characteristics. My goal is to be able to enter a value in the “model” field and have the spec fields automatically populate with that model’s corresponding values.

    To accommodate this, I’ve created a “Computer Models” form containing records for each model and their respective specs. On my “Computers” form, I have a link-to field (“Linked model”) that allows me to select a record from “Computer Models”.

    So here’s where I feel as though I need some strategic assistance. With guidance from other users here, I added script fields that will populate “spec” values based on the “Linked model” value. For example, the “Format” field contains the following code:

    function getFormat() {

    var Format = 'fld-20f1f5ca216a4d7f8108f32be30c22d3';
    var linked_model = record.getFieldValue('fld-a136f05b49a54749b15bedd371560ce1');

    if (linked_model.length > 0) {
    var first_contact = linked_model[0];
    Format = first_contact.getFieldValue('fld-20f1f5ca216a4d7f8108f32be30c22d3');
    }

    return Format;

    };

    getFormat();

    I was elated to see this successfully recognize/return the format for the chosen model, however, I’ve since observed some less-than-optimal behavior with this method:
    1. The “Linked model” value is not visible or editable in multi-column view. For visual representation, I’ve addressed this by simply adding an additional “Model” field and populating it with the text value using the same scripting method as the other spec fields. This solves the visibility issue, but still does not allow me to add/edit records in multi-column view (which I tend to do often).
    2. If a record’s “Linked model” field does not have a value entered, the scripted “spec” fields display the referenced field ID. Having fld-20f1f5ca216a4d7f8108f32be30c22d3 displayed results in a distracting/confusing visual representation. Ideally, I’d like the scripted fields to remain blank (or return a default vale like “unspecified” etc) in this scenario.
    3. Selecting a record in the “Linked model” field feels cumbersome. This is not a huge inconvenience, but ideally, I’d love to be able to enter this value using my keyboard, as I would if it were a text or pick list selection. Currently, I’ve only been able to select a linked record by clicking on “Select existing linked records” and scrolling through the “Computer Models” records.
    4. Exported data displays scripted fields in the same way multi-column view does. Exported records offer the same display as described in item #2 above.

    I’ve seen and tinkered with other methods, including IF/THEN and SWITCH statements, but haven’t been successful at all. Since I’m honestly not sure if my issues are with coding or the core logic behind my strategy, so I’m kneeling before the TapForms gurus here with a plea for guidance.

    Any suggestions or new perspectives on my situation are welcomed and sincerely appreciated!

    Thanks in advance!

    #46467

    In reply to: Table default values

    John McArthur
    Participant

    Hi Daniel,

    I have decided to go down the keep it simple route! (A bit of lateral thinking)
    Once all my data fields have been populated, I can simply reduce the length of the field display dramatically as this will still show the Globe but only a fraction of the link.
    As you mentioned earlier, I too don’t need to know the full link value.
    I’ll just put a Title/Item field in to show a brief description of the file/folder and that should work fine.

    Many thanks for your help but I didn’t want to waste any more of your time on such a small part of my ‘big picture’. :-)

    Cheers.

    #46459

    In reply to: Table default values

    John McArthur
    Participant

    Hi Daniel,

    Yes, to ensure I got the correct ones, I retrieved them specifically whilst in the JavaScript view, from the ‘Available Fields’ ID section.

    Noticed some semi-colons missing so added them. Is this correct?

    This is the code:

    function Attachment_Open() {

    const open_id = ‘fld-4b19c3623b674f13b6d07093f0889cf0’; // checkbox Id
    const link_id = ‘fld-2a2a101bee1e439cbbe098a8051a57fb’; // URL Id

    let open = record.getFieldValue(open_id);
    let link = record.getFieldValue(link_id);

    if (link && open){

    console.log(“Opening URL ” + link);
    Utils.openUrl(link);

    // clear opening flag
    record.setFieldValue(open_id, false);
    form.saveAllChanges();
    }
    }

    Attachment_Open();

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

Viewing 15 results - 1,096 through 1,110 (of 3,052 total)