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 - 961 through 975 (of 2,989 total)
  • Author
    Search Results
  • #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!

    #46722
    Prashant M
    Participant

    HI Sam ,
    Thanks for the reply ,as always they are really appreciated.

    1. The snippet you’ve showcased from your script , this is to be added “where” ? in Shortcuts to be defined as a variable “message” ?

    #46717

    In reply to: Help with a script

    Stig Widell
    Participant

    Sam, it is early Sunday morning here and I slept thinking of the Kalva script.
    You said the script is working because it was your intention to manually enter the eartag number in Avelstjur. I said it didnt work because I expected script to enter the number by matching grupp.

    After all cows have calved I sell (in November) the males and some of the females in form Kalvningar and I am no longer concerned about the father of these calves. The female calves I keep will be transferred over to the form Rekryteringsdjur (hopefully by a separate script further on).
    So I am not too worried about mutation?

    Of course I can restructure but I would ask you to help me with that I am not experienced enough.
    But wouldnt it be easier just to look for a match in grupp in Rekryteringsdjur and Avelstjurar?

    #46715

    In reply to: Help with a script

    Sam Moffatt
    Participant

    There is no search, it grabs the value of Avelstjur from the current record because that’s a single value and one assumed to be the primary key. The code I used is based on the assumption that there is only a single primary key match. The grupp fields are configured as pick lists for which that assumption doesn’t hold.

    Ideally you’d restructure to have grupp be it’s own form and then have that grupp contain a link to form field 1:M pointing on one side to the Rekryteringsdjur record and then from the Avelstjur a link to form field 1:M going to the Grupp. Then in Rekryteringsdjur you would have a single entry pathway from Rekryteringsdjur to Grupp to Avelstjur (though this model does mean a bull could be configured to be in two Grupp at the same time, Tap Forms doesn’t have a 1:1 link type which would potentially catch this).

    The downside of all of this is if you change the metadata in time then the links become out of sync again because there is no temporal coherency to the values, especially once you start automatically updating records. This is what makes your use case slightly more complex in that over time I would expect that some of the values will mutate in non-predictable and hard to script ways.

    #46714

    In reply to: Help with a script

    Stig Widell
    Participant

    You wrote this earlier:
    The Kalva function is there to create the calf, the first set of lines set up the various field ID’s we’re going to need later and then grabs the Avelstjur value for the current record. If that isn’t set we have an error message and return. It then looks for a match of that ID in the Avelstjurar form and if it doesn’t find it, also returns out. I did fix the type to match as number with the same formatting everywhere because I think initially it didn’t match properly due to a type mismatch (Javascript is a little more picky than TF).

    I cannot find that you are comparing the grupp field in form Aveltjurar with the grupp field in form Rekryteringsdjur? When these two match we have the correct Avelstjur and the father of the calf? That should be the crucial point.
    So my question is: is there a search for match between the grupp fields in the two forms in the script Kalva??

    #46708

    In reply to: Help with a script

    Stig Widell
    Participant

    You created the script Kalva to identify the field Avelstjur in form Rekryteringsdjur as I understood it??

    #46707

    In reply to: Help with a script

    Stig Widell
    Participant

    But the intention was not to use picklist at all but to let the script identify Avelstjur when grupp is the same in form Avelstjurar and form Rekryteringsdjur?

    #46705

    In reply to: Help with a script

    Stig Widell
    Participant

    Yes I know but why is the Avelstjur in form “Rekryteringsdjur” empty? The script should identify Avelstjur when field “grupp” is the same in records in form “Avelstjurar” and in form “Rekryteringsdjur”. When I insert a bull via picklist in form “Rekryteringsdjur” a calf record is created.

Viewing 15 results - 961 through 975 (of 2,989 total)