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 - 916 through 930 (of 2,951 total)
  • Author
    Search Results
  • Bruce Meikle
    Participant

    Hi Sam,

    Excellent! It does make sense. That achieves exactly what I want to do – in a simpler database. I am still having issues in the more complex example that prompted the question (an additional blank account record is created now), but the simpler model works. I need to spend some debug time on my main database, but now that I now what is involved, hopefully I can solve it.

    The saveAllChanges after each record change doesn’t appear to make much difference but I get what it is trying to achieve. It sounds like a good practice to use.

    BTW – I have learnt a lot about the JavaScript/TapForms model from your YouTube videos. Very helpful. And thanks for your very quick response to this question!

    • This reply was modified 3 years, 8 months ago by Bruce Meikle.
    Bruce Meikle
    Participant

    I’, new to Javascript and relatively new to TapForms. I need to replicate the “Select and link existing records” function in a script. In certain circumstances I am creating a new “transaction” (new record) inside a script (using AddNewRecordToField) and once created I need to set the “Link From Field” to an existing “account” record in the script so that the user doesn’t have to take the extra records to go to the subform and select (probably the wrong) “account”, or even worse, not select one at all.

    In summary:
    1. A new transaction item is created in a script using AddNewRecordToField();
    2. I need to update the “Link from field” to select an existing account record inside the script;

    Is this possible, and if so how would I do it?

    Attachments:
    You must be logged in to view attached files.
    #46878

    In reply to: Help with a script

    Sam Moffatt
    Participant

    One has the latest changes in it, one I think will be out of date. Just nuke the script that doesn’t have the changes I noted above.

    #46875

    In reply to: Help with a script

    Stig Widell
    Participant

    Japp, I did as Brendan wrote and it worked. Everything looks just fine. Marvelous job you have done Sam. I am so grateful.
    The two “Kalva” scripts should both be there? I ticked the first one.
    Enclosed please find two print screens with questions.

    Attachments:
    You must be logged in to view attached files.
    #46874
    Brendan
    Keymaster

    Hi Alan,

    There’s no direct way to sort on fields that are in related forms.

    But you could pull out the data from your Join field to display it in the parent form. You’d need a script to do that though. And because a Join field is a many to many relationship, you could possibly have multiple values in that field. So you would need to build up a string that concatenates all of the values and then sort by that on the parent form.

    Or if you’re only interested in the first value of the relationship, you could just pick out the first value from that field.

    Here’s information on scripting:

    https://www.tapforms.com/help-mac/5.3/en/topic/scripts

    On the Script Edit screen there are snippets. One of them is called Child Records Loop. If you select a field from your Link to Form field, then double-click on the Child Records Loop snippet, Tap Forms will write the code for you that you would need to loop through all the records of the Link to Form field. You would then just need to augment that with code to concatenate the values from the field into a single string. Then you can sort your parent form by that Script Field.

    Hope that makes sense.

    Thanks,

    Brendan

    #46824
    Brendan
    Keymaster

    Hi Lorenz,

    You would probably want to write a Form Script to add the reminders. There’s a function for adding to reminders available in the Script engine.

    https://www.tapforms.com/help-mac/5.3/en/topic/javascript-api

    You can create a Saved Search that just searches on the months. You just need to be able to extract the month name from the birthdate field.

    Add a Calculation field with the following formula:

    DATE(Birth Date; "MMM")

    Insert the field into the formula properly. Don’t just type the name. On macOS you would double-click on the field name on the formula edit screen. On iOS, surround the field name with square brackets (e.g. DATE([Birth Date]; "MMM") ).

    Now you can create Saved Searches that just looks for the month name and it wouldn’t matter what the year and day was.

    • This reply was modified 3 years, 8 months ago by Brendan.
    Brendan
    Keymaster

    Sorry for the confusion with the sample script. The update function was there only to show that you can update an event, not that you should add and then update all the time.

    I’ve updated the documentation to show that the updating is optional and should probably be done sometime later and that you’d have to store the identifier in another field.

    Sorry this caused you this trouble. But thanks for pointing out this issue.

    #46804

    In reply to: Help with a script

    Sam Moffatt
    Participant

    Ok, so minor change to Kalva to change how keyValue is determined once you have field links:

    	//let keyValue = record.getFieldValue('fld-db52d7055ddd40cf95f50bcf8e2fff3d'); // ID of the Avelstjur field in Rekryteringsdjur form.
    	
    	var grupp_id = 'fld-f195db7abf3145f08eeb26dbb610bce1'; // ID of the grupp link from form fieldType
    	var avelstjurar_djur_id_id = 'fld-5529d9d293934026a0b746890c2baf32'; // ID of the field from Grupp
    	
    	let gruppRecord = record.getFieldValue(grupp_id);
    	
    	if (!gruppRecord) {
    		Utils.alertWithMessage("Fel!", "Sätta grupprekordet");
    		return;
    	}
    
    	keyValue = gruppRecord.getFieldValue(avelstjurar_djur_id_id);
    

    The ID’s should match everything from the attached template. It adds a Grupp form, you’ll need to create a record for each of the grupp’s you’ve got (I did a quick copy paste from the pick list, I’m sure you can just type it directly) and then in the grupp link the correct avelstjurar for them. That should then create a single value chain from rekryteringsdjur up to grupp to avelstjurar. There is an extra calc field to add in the djur ID and prefix when you select the grupp.

    When you import, there should be two scripts, I modified the one that was favourited. The script should be updated for the above and should work properly.

    Attachments:
    You must be logged in to view attached files.
    Chris Ju
    Participant

    Hello,

    i want to share my experience with the ‘Utils.addToCalendar’ function…

    For me, the server error 415 (in calendar app) almost always occurred (some weeks ago also saw 515) when I created a calendar entry with the example script from the TF online help website (https://www.tapforms.com/help-mac/5.3/en/topic/javascript-api). Now I noticed that the error occurs only if you use the ‘Utils.updateCalendarEvent’ function immediately after the ‘Utils.addToCalendar’ function.

    I suppose the script on the help page written like this in order to save the ‘identifier’ in a field. But it is not necessary to use the update function, because ‘var identifier = utils.addtocalendar (…);’ already creates the event. The identifier variable could then be stored… However, should it be necessary to use the update function, a delay is necessary. Then the error does not occur, too…

    Maybe that helps someone… I needed hours to get to this finding.

    Thanks
    Chris

    #46744
    Sam Moffatt
    Participant

    There is an equivalent for the scripting video on using an iPad to script in TF5 which is a very similar interface to iOS featuring the same options though with a wider display and split views. The balances videos (part 1 and part 2) also are done on the iPad as well to try to demonstrate some of those capabilities. In terms of scripting 99% is the same on the two platforms, there are a couple of things around UI navigation that I think is better on the Mac but most of the core data manipulation stuff is identical.

    I’ve wondered about having a dedicated documentation page I could link to that covered getting the field ID on both the Mac and iOS/iPadOS because once you know it things are easy but it isn’t immediately obvious and I write down to get the field ID an number of times. I might have to email the keymaster about it.

    #46743
    Sam Moffatt
    Participant

    There is this sample that relies upon an external web service to populate image based on pick list values. You’d need to host the images external to TF or use an on disk location within the script folder access (though that’d only work on a Mac device and you’d have to sync the images to all devices that use it).

    #46738
    Brendan
    Keymaster

    You could also write a Field Script that could do that for you. If you have your images located in a folder somewhere, you could have the code fetch the image and add it to the field in the record whenever you change the value.

    Something like this:

    record.addPhotoFromUrlToField(image_url, case_cover_id)

    You would have to provide the URL and the field ID.

    #46735
    Nom Deguerre
    Participant

    Sam,

    Thank you so much for this perfect response.

    I had seen the earlier post and I felt that the solution lay in that direction but, not having used scripts in Tap Forms before, I wasn’t sure how to apply it. You’ve added just enough information to get me over the line and I can assure you that it works perfectly.

    My biggest hurdle (when reading the original post and having received this response) was simply determining the fieldIDs. I didn’t know how to discover them – it’s perhaps not as obvious on a phone as it is on the desktop version.

    So, for the sake of anyone at my level who needs to follow this path, here’s how I finally got there:

    1. In the parent form, create a “Link To Form” field that links to the child form as a JOIN on the ID field – this will populate and link the forms and be functional but will display inline.

    2. In the parent form, create a “Link To Form” field that links to the child form as a 1-to-MANY link – no functional links will be created.

    3. At the form level, choose “Run a Script” which opens the script editor.

    4. Add a new script and paste Sam’s code, in its entirety, into the editor replacing the default starter script.

    5. Watching the first 5 minutes of Sam’s own “Simple Intro To Scripting” video allowed me to see that the field ids are self-populated from the script editor. Knowing that, I was able to find that feature on the iOS version, above the software keyboard. For me, this was the key breakthrough. I could now obtain the field ids for both the functional join field and the empty 1:M field.

    6. Insert the obtained field ids into the placeholders in the script.

    7. Save and then run the script. Problem solved!

    8. To tidy up, delete the script and delete the “Link to Form” join version.

    Once again Sam, thank you. It won’t be the only time that I’ll be making use of this method of linking forms.

    #46730
    Sam Moffatt
    Participant

    I think this is a case where the JOIN mode on iOS will render the table inline but the 1:M mode has a selector to go to a subview that has the table.

    I feel the tangible feature request is to be able to control if the JOIN displays inline or as a subview (conversely one could consider the inverse for the 1:M/M:M modes).

    In terms of fixes right now, you could use scripting to create the links for you. You’d start with a JOIN field so that Tap Forms does the heavy lifting for finding the child records and then you just need a script that iterates through each of the records and attaches the child records from the JOIN field to a 1:M field.

    The script is pretty simple, this is from an earlier post:

    record.getFieldValue('fld-joinfieldid').forEach(rec => record.addRecordToField(rec, 'fld-1tomanyfieldid'));
    

    This will do just the currently selected record and whilst I (still) haven’t tested it but it should work though you need to replace the field ID’s to match. You could probably put this in as a field script as a test run but I’m not sure if the JOIN field triggers a script field update automatically (since the field isn’t edited directly) so I’d probably stick with a form script. Something like this as a form script might do it:

    form.getRecords().forEach(parentRecord => parentRecord.getFieldValue('fld-joinfieldid').forEach(childRecord => parentRecord.addRecordToField(childRecord, 'fld-1tomanyfieldid')));
    document.saveAllChanges();
    

    Basically get all the records from the current form as parentRecord, then for each parentRecord get all of the records from the JOIN field as childRecord, then for each childRecord add it to the 1:M field in the parentRecord. Then save all the changes :D

    Again, I’ve not tested it and you’ll have to splice your own ID’s in, running this more than one should be safe (TF generally dedupes multiple record links) and should allow you to relink everything after you do your own import. The document.saveAllChanges() makes sure that everything is saved properly so it’s important it’s there as well. You can delete the boilerplate you get when creating a form script and just put this in with the ID’s replace to match your field IDs.

    #46728
    Sam Moffatt
    Participant

    The snippet goes at the end of your script once you’ve finished everything. If you’re hooking into the prompter stuff, something like this is perhaps a slightly more concise example (this could be the callback from a prompter for example):

    var callbackFunction = function(continued) {
    	if (continued)
    	{
    		let newRecord = form.addNewRecord();
    		newRecord.setFieldValue(tracking_number_id, clipboard);
    		newRecord.setFieldValue(received_date_id, new Date());
    		newRecord.setFieldValue(confirmed_id, true);
    		document.saveAllChanges();
    		if (parameters && parameters["x-success"]) {
    			Utils.openUrl(parameters["x-success"] + "?message=Created%20record&tfrec=" + newRecord.getUrl());
    		} else {
    			Utils.openUrl(newRecord.getUrl());
    		}
    	} else {
    		if (parameters && parameters["x-cancel"]) {
    			Utils.openUrl(parameters["x-cancel"] + "?message=Cancelled");
    		}
    	}
    };
    

    The message in Shortcuts is the ?message=Blah on the Utils.openUrl line. You could call it anything really so long as it matches and the text is URL encoded. I hand craft those messages though there is encodeURIComponent that would take an arbitrary string and URL encode it for you.

    Hope that helps!

Viewing 15 results - 916 through 930 (of 2,951 total)