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,021 through 1,035 (of 3,011 total)
  • Author
    Search Results
  • #46623

    In reply to: Help with a script

    Stig Widell
    Participant

    I am really grateful to you Sam. I dont have the time tonight (Swedish time) to work with it but I have an initial question:
    “I’m going to use one of my scripts to get the ID, easiest way to do this is to import the Script Manager and in the “Repository” form run the “Update Scripts” form script”.
    This is not for me to do? You did it when you created the form script, right?

    #46620
    Rob Naaijkens
    Participant

    Thnx for your help, Sam!

    I removed the links and the scripts because my database was about 400MB with 142 records. Still my form/database is about 180MB witch is big I think.

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

    In reply to: Help with a script

    Sam Moffatt
    Participant

    Doing this backwards, don’t mind me…

    the Father-ID in the calf record should remain the same even when the mother, the year after, will be linked to another pedigree bull

    Right, the calf record should remain unchanged though presumably at some point it either is sold or grows up into one of the other forms. Which actually makes me wonder if ideally everything would be one form with relevant sections but I don’t think TF does self joins well (data model quirk).

    How do you mean the calf record should be created? I dont understand that? I dont have all information already before the time of birth of the calf, eg date of birth, sex, stillborn etc.

    Well you know what you know about the calf to be, you’re just entering it in immediately once you know some of the details just not all of the details. For me personally I find pre-entering partial data slightly easier because then it’s just a matter of inputting the final data once that comes in. Presumably you know which of your cows are calving so that can be a filter there to limit the possible candidates and you could also sort/group by the pasture they’re in.

    “Avelstjur” shouldnt be a text field but a number, my mistake.
    Yes, if I change that field to a join field I get the pedigree bulls info over to the cows record as a list with one row with all the info of that bull. But I cant get the ID of the bull over to the father-ID field in the record of the newborn calf when I link that calf record to the mother record. How do I manage that? What do you mean with “creative” join field?

    Type is mostly inconsequential for the simpler data fields (text/number) and more necessary for formatting than anything. That said it might mess up some comparisons so always useful to keep it consistent.

    Workflow wise I think I’d create the calf record first and then just link in the mother and father records from the calf directly. Or create the calf from the mother record and then just link it to the father at that point in time. That’d obviate the need to script because you could just use the UI to select.

    I’m going to use one of my scripts to get the ID, easiest way to do this is to import the Script Manager and in the “Repository” form run the “Update Scripts” form script.

    Inside “Rekryteringsdjur” I created a new form script called “Kalva” and then this is it’s implementation:

    let PARENT_SCRIPT = "Kalva";
    document.getFormNamed("Script Manager").runScriptNamed("getRecordFromFormWithKey");
    
    function Kalva() {
    	let rekryteringsdjur_kalvningar_id = 'fld-cdb4431a905f4b1dbaa750b07a63b6f1'; // ID of link to form field to Kalvningar form in the Rekryteringsdjur form.
    	let avelstjur_kalvningar_id = 'fld-35f714dc0468447c9b5b9875c8a86545';  // ID of link to form field to Kalvningar form in the Avelstjur form.
    	
    	let foldse_id = 'fld-8e0cd05ffe8d4603a8abb8ca4694b4ee'; // foldse field ID from Kalvningar form.
    	
    	let keyFieldId = "fld-0e776009803a4c1f8415e7cbb7919858"; // ID of the Djur-ID field in the Avelstjurar form.
    	let keyValue = record.getFieldValue('fld-db52d7055ddd40cf95f50bcf8e2fff3d'); // ID of the Avelstjur field in Rekryteringsdjur form.
    
    	// check if the bull is even set or error.
    	if (!keyValue) {
    		Utils.alertWithMessage("Fel!", "Avelstjur värde ej inställt!");
    		return;
    	}
    
    	// look for matching bull ID record; note this assumes the bull ID is unique.
    	let avelstjurRecord = getRecordFromFormWithKey("Avelstjurar", keyFieldId, keyValue)
    	if (!avelstjurRecord) {
    		Utils.alertWithMessage("Varning", "Det gick inte att hitta posten!");
    		return;
    	}
    
    	// Create a new calf record from the rekryteringsdjur form.
    	let kalvningarRecord = record.addNewRecordToField(rekryteringsdjur_kalvningar_id);
    	
    	// set foaling date to current date.
    	kalvningarRecord.setFieldValue(foldse_id, new Date());
    
    	document.saveAllChanges(); // flush out model changes for new record creation.
    	
    	// add the calf to the bull's record ID.
    	avelstjurRecord.addRecordToField(kalvningarRecord, avelstjur_kalvningar_id);
    	
    	document.saveAllChanges(); // save adding calf to bull
    
    	Utils.alertWithMessage("Gjord!", "");	
    }
    
    Kalva();
    

    The first two lines setup a variable some of my scripts use to identify themselves and import the getRecordFromFormWithKey function. If PARENT_SCRIPT isn’t set, some of the child scripts run self tests on import.

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

    Once we’ve got the bull, we’ve started already with the mother, so we create a new calf from the mother (record.addNewRecordToField). This will automatically set the link from form field to this record. I also set Födelse to the current date, you could also default this in your form settings but this is an example of how you can set some fields in that child record. There is a document.saveAllChanges() because when you create new records, sometimes the scripting system can get out of sync so I find it easier to force a save here.

    The last step is to add the calf to the bull and then save that change.

    That should do the trick for what you’re after.

    Did you use the tff file or the tfarc file? what is the difference?

    TFF is a form template, it just has the definition of the form and linked forms. TFARC is an archive that can also include records. I did the TFARC first then noticed I was missing a form and used the TFF.

    • This reply was modified 4 years, 1 month ago by Sam Moffatt. Reason: fixing blockquote tags
    #46611
    Sam Moffatt
    Participant

    The document.saveAllChanges() is after the return statement which means it never gets executed as the return statement returns control to the caller. It also looks like you’re missing a brace from the example code so I’m guessing that should have been before the function call. You can wrap the code in a back tick at the start and end to put it into code mode (or use the HTML tag):

    function Focus_Script() {
    
        /* variables for Practice Plan */
    
        var practice_date = record.getFieldValue('fld-id'); //practice date in parent form
        var OT = document.getFormNamed('Offensive Transition'); // child form;
        var offensive_transition = record.getFieldValue('fld-id'); // watches checkbox in parent form;
        var date_text = JSON.stringify(practice_date);
        let foci = [];
    
        /* variables from Offensive Transition */
    
        var OT_date_id = 'fld-id'; // date field from child record;
    
        if (offensive_transition == true) {
    
            /* Search Offensive Transition for existing date and add record if not there */
    
            var records = OT.getRecordsForSearchTerm(date_text);
    
            if (records.length !== 0) {
                console.log("DATE EXISTS ALREADY"); // true condition;
    
            } else {
    
                foci.push("Offensive Transition"); // false condition;
    
                var new_OT_record = record.addNewRecordToField('fld-id');
                new_OT_record.setFieldValue(OT_date_id, practice_date);
                console.log("DATE DOES NOT EXIST");
                Utils.alertWithMessage('Done.', practice_date + " added."); // false condition;
    
            }
        } else {
    
            /* Delete existing record for a given date in Offensive Transition */
    
            var OT_records = OT.getRecords();
            var field_id = 'fld-id';
            /date field converted to text;
            for (var index = 0, count = OT_records.length; index < count; index++) {
                var aRecord = OT_records[index];
                var field_value = aRecord.getFieldValue(field_id);
                if (field_value == date_text) {
                    OT.deleteRecord(aRecord);
                }
            }
        } // false condition;
    
        document.saveAllChanges()
    
        console.log("EOJ");
        return foci;
    }
    
    Focus_Script();
    #46610
    Mark Kearney
    Participant

    Hello. Basic programmer here (literally, my understanding comes from my days with BASIC).

    I am trying to create a basketball season practice planner where clicking a checkbox for a practice topic automatically adds a record to a “Link To” field (joined by date field). The details for the parent topic can be stored in the child record. The practice topic from the parent form is then stored in array (foci) along with the other topics for that practice. If the checkbox is unchecked, the records in the child form are deleted for a given date and that topic is removed from the array.

    This portion of the script (located in a field in the parent form) appears to work but the new child record does not appear in the linked field until records are refreshed. NB: There is a script in the child form that converts the date to text (as with var date_text below) for the search piece.

    I would like to get the child record to show in the “Link to” field without needing to refresh. Any help would be greatly appreciated.

    function Focus_Script() {

    /* variables for Practice Plan */

    var practice_date = record.getFieldValue('fld-id'); //practice date in parent form
    var OT = document.getFormNamed('Offensive Transition'); // child form;
    var offensive_transition = record.getFieldValue('fld-id'); // watches checkbox in parent form;
    var date_text = JSON.stringify(practice_date);
    let foci=[];

    /* variables from Offensive Transition */

    var OT_date_id = 'fld-id'; // date field from child record;

    if (offensive_transition == true) {

    /* Search Offensive Transition for existing date and add record if not there */

    var records = OT.getRecordsForSearchTerm(date_text);

    if (records.length !== 0) {
    console.log("DATE EXISTS ALREADY"); // true condition;

    } else {

    foci.push("Offensive Transition"); // false condition;

    var new_OT_record = record.addNewRecordToField('fld-id');
    new_OT_record.setFieldValue(OT_date_id,practice_date);
    console.log("DATE DOES NOT EXIST");
    Utils.alertWithMessage('Done.', practice_date + " added."); // false condition;

    }
    } else {

    /* Delete existing record for a given date in Offensive Transition */

    var OT_records = OT.getRecords();
    var field_id = 'fld-id'; /date field converted to text;
    for (var index = 0, count = OT_records.length; index < count; index++){
    var aRecord = OT_records[index];
    var field_value = aRecord.getFieldValue(field_id);
    if (field_value == date_text) {
    OT.deleteRecord(aRecord);
    }
    }
    } // false condition;

    console.log("EOJ");
    return foci;
    document.saveAllChanges()

    Focus_Script();

    #46606
    Rob Naaijkens
    Participant

    Hi Sam,

    First, thank you very much for all this effort, appreciate that!

    I had a link between the second and the first. But not between the first and second.

    I used your script but got some errors. Now I still got an error on the script for the second form. Is there a possibility to copy my database and send it to you by e-mail (WeTransfer) so you can take a look?

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

    At the moment it doesn’t look like you’ve got any links between the two?

    The function doesn’t work because first nothing calls it (you need a getTotalofLinkedFieldForField(); at the end of the script) and second linkedRecord is not defined.

    Since there isn’t a link between the two forms, we’ll automate populating the other form. We’re going to use form.getRecords to get a copy of all of the records in the current form (there are a bunch of other examples on the forum as well), get the total and populate that into the other form. We’ll create this script inside the first form since it’s the source. You’ll need to swap in the field IDs from the place holders:

    var firstForm_NaamFieldId = 'fld-naamfield';
    var firstForm_tableFieldId = 'fld-4dde0c4712954541a69b18d602bfcb27';
    var firstForm_tableField_subFieldId = 'fld-60216b72e69b4a9bab98a23c8a019ea9';
    
    var secondForm = document.getFormNamed("Nieuw formulier");
    var secondForm_NaamFieldId = 'fld-namefield';
    var secondForm_NummerFieldId = 'fld-nummerfield';
    
    function Create_Summaries() {
    	// get all of the records from the current form (should be first form)
    	for (let currentRecord of form.getRecords()) {
    		// create a new record for it in the second form
    		let linkedRecord = secondForm.addNewRecord();
    
    		// copy the name across
    		linkedRecord.setFieldValue(secondForm_NaamFieldId, currentRecord.getFieldValue(firstForm_NaamFieldId));
    
    		// create the total value field
    		linkedRecord.setFieldValue(secondForm_NummerFieldId, currentRecord.getTotalofLinkedFieldForField(firstForm_tableFieldId, firstForm_tableField_subFieldId));
    	}
    
    	// save all changes
    	document.saveAllChanges();
    }
    
    Create_Summaries();
    

    Though ideally you’d set up a link to form field to keep them together. If you create a link to form 1:M from your first form to your second form and then tick “show inverse relationship”, you can create a script field in your second form that looks like this:

    var firstForm_tableFieldId = 'fld-4dde0c4712954541a69b18d602bfcb27';
    var firstForm_tableField_subFieldId = 'fld-60216b72e69b4a9bab98a23c8a019ea9';
    var secondForm_linkFromFormFieldId = 'fld-linkfromformfieldid';
    
    function TotalValue() {
    	let linkedRecord = record.getFieldValue(secondForm_linkFromFormFieldId);
    	return linkedRecord.getTotalofLinkedFieldForField(firstForm_tableFieldId, firstForm_tableField_subFieldId));
    }
    
    TotalValue();
    

    Though for that to work you will need that link to form field setup. Give that a spin and see how it goes.

    #46596

    In reply to: Inventory Script

    Sam Moffatt
    Participant

    Random thought: you could go crazy with a few more forms but it feels like at least “purchase point” should be it’s own form (you could do a form for brewery, styles and sizes as well then use links but that might be overkill?). Purchase point is definitely a candidate for it’s own form and I think sizes might help as well because it seems like you’d have standard sizes.

    I did a tutorial on managing balances with Tap Forms using yarn balls as an example and products like hats. There are two videos, the first video sets the ground work forms and has a simple script for setting the balance whilst the second video walks through building the scripting to add and remove entries maintaining the balance. These should help you handle reducing the inventory quantity when applying new parts but it does rely upon form scripts to work properly.

    In your case you still need to have something that figures out the quantity you want to reduce by for the purchase points to make it work properly. That might just be a simple prompter entry that then creates the entries for you automatically. I personally use prompters a lot for quick entry of information which speeds up data entry because I can hit the three fields I need rather than load up an entire form.

    #46594

    Topic: Inventory Script

    in forum Script Talk
    Neil Bontoft
    Participant

    I have what I think is a very simple question but I sadly haven’t got a clue how to answer it.

    I use Tap Forms v5 as a tool to record inventory in my cellar. Columns include:

    • Brewery
    • Beer
    • Size
    • Style
    • Purchase Point

    I would like to monitor the quantity of each bottle. It’s not as simple as using an integer and increasing or decreasing as fields like Purchase Point can vary depending on where the bottle was bought.

    I have created a concat field that combines Brewery, Beer and Size. This field effectively creates a string for the unique beer which I think could be used.

    I guess I have two options:

    • A field script that for each record (circa 900) could check if the concat field equals other concat fields and count as appropriate.
    • A new form which uses the concat field and does a count against each record for the concat field. To do this I would also need the script to copy the concat field, and remove any with zero quantity.

    Advice is massively appreciated. Cheers

    • This topic was modified 4 years, 1 month ago by Neil Bontoft.
    #46588
    Kirk Williams
    Participant

    Thank you so much Sam! I will definitely need to spend some time with this (given my limited scripting experience), but this is precisely the type of logistic advice I was hoping for.

    Your time and guidance are very much appreciated!

    #46584
    Rob Naaijkens
    Participant

    Maybe some more information on what I am trying to do.

    First Form: (Assortiment wijnen) all unique records with a Table field “Verkocht” in that table there are 3 records at the moment. The total of the record with “Aantal” I would like to show-up in the second form.

    I would like to use the table because these are wines and I would like to see the price per year of period.

    Second Form: This is where I would like the total of all the “Aantal” records from the first form.

    On your explanation I made the following script. First ID is the field “script” itself and the second is the ID of the field “Aantal” form the first form.

    The script works, no errors but the result on both numbers and text is nothing or <empty>.

    Hope this makes any sense ;)

    Attachments:
    You must be logged in to view attached files.
    #46583
    Rob Naaijkens
    Participant

    Hello Sam,

    Thanx for the quick reaction! Is it possible to tell me the complete code of the script? I’m a beginner ;)

    I like to try your solutions and will post the results later.

    #46581
    Sam Moffatt
    Participant

    Do you mind expanding a little on your structure?

    If you’ve got a link from the second form to the first, you can create a script field that traverses the link to form field (actually might be easier to do it as a link from form field) and then you can call linkedRecord.getTotalOfLinkedFieldForField(tableFieldId, fieldInTableId) to get a total of the field in the table (first ID is the ID of the table field itself, second one is the ID of the field you want to get the total of in the table). This actually becomes a one liner if you only have a 1:1 style mapping with the second form (link to form 1:M from the first to the second will give you a singular relationship in the second form). If there is a many relationship from the second form to the first, then you’ll need to get all of the linked records, loop over them, get the total and then sum it together.

    If you’ve going to programatically populate the second form, then that’s a loop to create the record and the same getTotalOfLinkedFieldForField on the source record to calculate the total of the field in the table field.

    Share a little about how the two forms are linked (or not) and how you interact with the two forms. The CRM video I did recently has a bunch of fun with linking forms together as well.

    #46580

    In reply to: Help with a script

    Sam Moffatt
    Participant

    If you post your scripts to date and maybe the form template folk on the forum can probably help. One of the cool things about the forum is that everyone can benefit from your questions and any solutions :)

    #46579
    Sam Moffatt
    Participant

    When you use Siri Shortcuts, you need to use the x-callback-url feature to load Tap Forms to trigger the document interaction. I did a video on using the x-callback-url to trigger a script from Siri Shortcuts. Otherwise I believe it invokes Tap Forms in the background (via a library I think?) and doesn’t permit any interaction with scripting. The Siri interactions API is kind of limited in what it allows so if you want any sort of interactive experience, I’ve only really seen it possible via the Shortcuts app.

Viewing 15 results - 1,021 through 1,035 (of 3,011 total)