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 - 541 through 555 (of 2,951 total)
  • Author
    Search Results
  • #49351

    In reply to: Name Lookup

    Brendan
    Keymaster

    hi Chris,

    The Child Records Loop snippet is designed to fetch the child records, not the parent record. So in your case, the People form is the parent and the Donations form is the child. So if you wanted to put a field on the People form that say, concatenated all of the donations for that People record together, that’s when you would use the Child Records Loop.

    Yes, Documents are like Databases. Logically and technically as each .tapforms document is actually a folder that contains within it its own SQLite database.

    And yes, Forms are like tables precisely. Records are of course like SQL records and fields are like Table Columns in SQL.

    If you want to automatically connect your forms together based upon some key field, then you would need to use the Join Link Type on your Link to Form field and set the common fields up. Then Tap Forms will auto-join for you.

    But that does in fact change the JavaScript code you would need to get the name values. Because with a Join Link Type, that is in fact the same as an automatic Many to Many relationship. That is, for each Donation record, there technically could be many People records and for each People record there could be many Donation records.

    So now when you get the value of the People field from the record, you’ll get an array instead of just the one object like in my code example. In this case, the Child Records Loop would work as expected.

    Hope that makes sense.

    Thanks,

    Brendan

    #49350

    In reply to: Name Lookup

    Chris Jordan
    Participant

    Hello again Brendan,

    I guess what I was originally trying to do, was write a script that:

    1. Automatically connected each donation to its parent record based on Envelope number.
    2. Automatically fill in the name field based on the Envelope number found in the parent record.

    Number two is what you just helped me with, however I had to manually select the parent record in the Inverse Relationship field, by looking up and connecting the correct Envelope number for the person. I know you have removed the SQL language, however now I have to understand the inner modeling of the application in order to write Javascript to pull the data.

    Still learning,

    Chris

    #49348

    In reply to: Name Lookup

    Brendan
    Keymaster

    By the way, to write that code, all I had to do was double-click on the People field on the left side of the Script Edit window. Then double click on the First Name field. Then all I typed was the return first_name and I wrapped it into a function structure.

    #49347

    In reply to: Name Lookup

    Brendan
    Keymaster

    Hi Chris,

    Ok, so your form is setup with People as the parent and Donations as the child.

    Your script is in your Donations form. Your script is attempting to loop through the parent People record. However, it’s not an array because it’s just one object. A Donation record has only a single People parent record. So you can just get the People record, then ask it for one of the fields.

    Since your script is called Firstname, I’m assuming you’re trying to get the People record’s First Name field.

    So all you need to do is first get the parent People record, then ask that record for the value from the First Name field.

    Here’s code to do that:

    function firstName() {
    	var people = record.getFieldValue('fld-042db77a64be4be0a8f041e927d5ff24');
    	var first_name = people.getFieldValue('fld-8c3880a29ec949e0a8ff1d502b527ae7');
    	return first_name;
    }
    
    firstName();

    That’ll do it. Hopefully that’s what you’re looking to do.

    #49338

    Topic: Name Lookup

    in forum Script Talk
    Chris Jordan
    Participant

    Hi,
    I have two Forms. One called Donations and the other called People. In the Donations form I have the script below where I’m attempting to look up the name of a person by Envelope number. I double-clicked the “Child records loop” Snippet with the People – Envelope field highlighted.

    For some reason I’m not getting any records (people.length = 0) even though there are 26 Records in the People Form. I am running the script in the Editor and length prints out 0. I must be doing something wrong. Can someone please give me some guidance.

    Thanks,
    Chris

    function Script() {
    
    	function recordsLoop() {
    	
    		var people_id = 'fld-16486faf17c94f779b495e39db0d2a51';
    		var envelope_id = 'fld-86ff175986c74617a1c71822bf7e9f2c';
    		var people = record.getFieldValue(people_id);
    	
    		console.log(people.length);
    		
    		for (var index = 0, count = people.length; index < count; index++){
    	     	var envelope = people[index].getFieldValue(envelope_id);
    	     	console.log(envelope);
    	     	
    	     	if (envelope) {
    				// do something
    				console.log(envelope);
    			}
    		}
    		return;
    	}
    	
    	recordsLoop();
    
    }
    
    Script();
    • This topic was modified 2 years, 6 months ago by Brendan. Reason: added back ticks around script code
    #49333
    Brendan
    Keymaster

    Hi Rudy,

    Make sure when you add your fields to your formula that you double-click on the field on the left side of the formula editor to insert the field into your formula. Don’t just type the field name. The square brackets around the field name are for the iOS version only.

    For your script, make sure you have the Result Type set to Number. The same for the calculation field.

    If you want me to look into your form, please email me your .tff file (export Tap Forms Form Template in the File menu).

    Thanks,

    Brendan

    #49330
    Rudy Zwartjens
    Participant

    Thank you Brendan for your – as usual – fast reply!
    The solution you describe is known for me (but still very useful as a reminder), so I now understand that I my explanation was not complete.
    I hoped to be able to “fill” a field – at least – with the number of different countries, so I could print that number together with the set list.
    BTW, the set list is printed using a label layout, the only way to get it printed like I want.

    In the meantime I was puzzling with a DIY solution.
    I used a checkbox field [c.change] with a check on every first tune in a country:

    Garoon Armenia check
    Hey Par Armenia
    Hora Romenia check
    Musette1 France check
    Musette2 France

    With your described method I found: 3 countries, right!
    To get this count value in a field I used a calculation field with formula:
    IFNOTEMPTY([c.change];sum([c.change]);0) . In the formula I used no brackets.
    The result was empty, so this appears not to be right (too simple?).

    Then I used a script field using a sample for summation:

    function getFieldTotal() {
    var field_id = record.getFieldValue(‘fld-5e8d5d2744d4e2cbdbc9f3d7ac1cc35’);
    // whatever the field ID is for the field you want the total of.
    var total = form.getTotalOfField(field_id);

    return total;
    }

    getFieldTotal();

    Also this field stays empty. So it is clear: I still have to learn a lot!

    Will you please give me a hint in the right direction?
    I am not afraid of programming, but I have no experience with Java.

    Thanks in advance.

    Rudy

    #49325
    Paul Wirth
    Participant

    Hello again, I’m having an issue getting record.setNoteFieldValue() to work.

    (I mentioned this here but it wasn’t really on-topic, so I’m starting a new topic)

    Where targetId is the field ID of a Note field, using record.setFieldValue(targetId,'foo') updates the text of the Note field, but using record.setNoteFieldValue(targetId,'foo') fails to update.

    I’m attaching a Tap Forms document that demonstrates. One form uses record.setFieldValue() and the other uses record.setNoteFieldValue(). In each, the field script attempts to update a “Target” Note field based on text entered in the field “Trigger”.

    Attachments:
    You must be logged in to view attached files.
    #49315
    Paul Wirth
    Participant

    Too late to edit my previous post. I believe I found the issue:

    I’ve been editing my scripts in VSCode, and when a javascript line is beyond a certain length, either it (or the Prettier plugin) makes line breaks like this:

    const triggerFieldValue = record.getFieldValue(
      'fld-07c030d45e8642158be624403154874e'
    );

    When I edit the line in Tap Forms so that it reads all in one line, everything works.

    I’m guessing that Tap Forms is looking for a contiguous string like record.getFieldValue('fld-blablabla to set up the script trigger, and gets thrown off by line breaks.

    Not a huge deal, but perhaps that would be helpful to account for, for those of us who like to edit outside of Tap Forms?

    Edit: in the meantime, the Prettier plugin .prettierrc.js can be made to stop breaking long lines with:

    
    module.exports = {
      printWidth: 999, // Tap Forms script trigger expects <code>record.getFieldValue('fld-...)</code> to be all on a single line
    };
    

    Additional request: could you possibly account for variables in place of the actual field ID string, and still trigger a script? e.g.,:

    
    const triggerFieldId = 'fld-07c030d45e8642158be624403154874e';
    const triggerFieldValue = record.getFieldValue(triggerFieldId);
    
    • This reply was modified 2 years, 7 months ago by Paul Wirth.
    • This reply was modified 2 years, 7 months ago by Paul Wirth.
    • This reply was modified 2 years, 7 months ago by Paul Wirth.
    • This reply was modified 2 years, 7 months ago by Paul Wirth.
    • This reply was modified 2 years, 7 months ago by Paul Wirth.
    #49311
    Paul Wirth
    Participant

    Hi Brendan,

    Sorry, I didn’t explain well.

    Plan A — When I:
    – added a text field to the form
    – referenced that field in the script (record.getFieldValue...)
    – changed the value of that field

    The script didn’t trigger, including after restarting Tap Forms. I repeated this multiple times with new text fields

    Plan B — When I:
    – referenced a text field that had been there before introducing this script
    – changed the value of that field

    The script did trigger, consistently.

    The plot thickens, though: Since I’ve returned to Tap Forms, Plan B also doesn’t work. The script no longer triggers when the text field is altered.

    Here’s a basic script that repeats my issues:

    function changeTargetOnTrigger() {
    Utils.alertWithMessage('success!','the script was triggered');
    const triggerFieldValue = record.getFieldValue('fld-d5bbf5db0052462e8e138d1636718d64');
    const targetId = 'fld-a6c94d8bb94e4ee0a1287a046280a6d1';
    record.setFieldValue(targetId, triggerFieldValue);
    form.saveAllChanges();
    }
    changeTargetOnTrigger();

    I’m attaching a minimal document that includes the script and relevant fields (Basically just a form with three fields: “Target”, “Trigger”, and “Script”.

    When running the script manually, the value of the field “Trigger” is applied to the field “Target”. But when changing the value of the field “Trigger”, the script doesn’t run. Maybe I’m missing something obvious!

    • This reply was modified 2 years, 7 months ago by Paul Wirth.
    • This reply was modified 2 years, 7 months ago by Paul Wirth.
    #49310
    Victor Warner
    Participant

    Brendan,

    Thank you. Issue resolved.

    I would like to add one addition to number the combined values, e.g. “1. certificate of good standing, 2. Power of attorney, 3. statutory declaration, 4. travel consent document” with the following field script:

    function recordsLoop() {
    
    var all_documents_id = 'fld-b130f329a7ae46f3908d190699822044';
    var script_id = 'fld-bb67d87c0f044b2cb2745ba9e3fde81f';
    
    	var forms_id = 'fld-8c147256f1534336aa960e509a8b826b';
    	var name_of_document_id = 'fld-03b7ec42c07542ea8cfc23d4809ef367';
    	var forms = record.getFieldValue(forms_id);
    
    	var allDocs = [];
    	
    	for (var index = 0, count = forms.length; index < count; index++){
         	var name_of_document = forms[index].getFieldValue(name_of_document_id);
    		if (name_of_document) {
    			allDocs.push((index+1) + ". " + name_of_document);
    			
    		}
    	}
    	
    return joinedDocs = allDocs.join(", ");
    
    console.log(joinedDocs);	
    
    }
    
    recordsLoop();
    

    However, where there is only one value I would like that single value not to have a value, e.g. rather than “1. power of attorney” but rather just “power of attorney”.

    How can the code be amended to permit this?

    #49307
    Brendan
    Keymaster

    Hi Paul,

    Tap Forms will only run the script if the field already exists in the form. That’s normal behaviour.

    Try quitting Tap Forms and launching it again to see if script triggering works properly when you change a value in the referenced field.

    #49302
    Paul Wirth
    Participant

    I think this is probably a bug.

    I was attempting to get changes in a field to trigger a field script, using a reference to record.getFieldValue('fld-blablablabla') in the script. The script would only trigger when the field referenced was one that had already existed in the form (in this case, a text field that I wasn’t using). If I created a new text field, referencing the new field’s ID would never cause the script to be triggered when that new field’s value was changed.

    In the end, I went through the trouble of creating a script to copy all data from the field that worked correctly as a trigger, to a new field, just so I could use old field properly as a script trigger.

    #49299
    Brendan
    Keymaster

    Hi Victor,

    On the Script Editor, if you select a field from your Link to Form field and then double-click on the “Child Records Loop” code Snippet, Tap Forms will write the code for you to loop through all your child records, extracting out the value from the selected field.

    Now you can use the JavaScript push() function to combine all the values from that field into an Array. Then after the loop, use the JavaScript join() function to join them together with a comma separator.

    Then use the Tap Forms Utils.copyTextToClipboard() function to copy the value to the clipboard.

    Then set the value on the field in your Form 1 using record.setFieldValue(field_id, joinedDocs);

    Here’s a start for you:

    function recordsLoop() {
    
    	var forms_id = 'fld-8c147256f1534336aa960e509a8b826b';
    	var name_of_document_id = 'fld-03b7ec42c07542ea8cfc23d4809ef367';
    	var forms = record.getFieldValue(forms_id);
    
    	var allDocs = [];
    	
    	for (var index = 0, count = forms.length; index < count; index++){
         	var name_of_document = forms[index].getFieldValue(name_of_document_id);
    		if (name_of_document) {
    			allDocs.push(name_of_document);
    			
    		}
    	}
    	
    	var joinedDocs = allDocs.join(",\r");
    
    	
    	return joinedDocs;
    }
    
    recordsLoop();

    It literally took 30 seconds because Tap Forms did most of the work for me writing all that code just by double-clicking the Child Records Loop snippet. All I did was defined the allDocs array, then pushed the name_of_document value to the array inside the loop. Then joined and returned the joined value.

    Victor Warner
    Participant

    I would like to make a script that takes the content of a field from linked records of another form and combine them together.

    For example, see attached Tap Form database

    In Form 1 there is a link to Form 2

    So I would like to create a script which:

    1. combines the fields for the 4 linked records
    2. combines them together with commas after each but adds numbers before each field (eg “1. certificate of good standing, 2. Power of attorney, 3. statutory declaration, 4. travel consent document’)
    3. copies the result to clipboard and writes the combined fields into the All documents field in Form 1

    Help on how to do this would be gratefully received.

    Attachments:
    You must be logged in to view attached files.
Viewing 15 results - 541 through 555 (of 2,951 total)