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 - 121 through 135 (of 2,866 total)
  • Author
    Search Results
  • #51589
    Josef Tingbratt
    Participant

    Thanks i did solve it oth help from ChatGPT, I just needed to point out the correct table to search in :)

    But is there a way to set the scripts to run automatically for each post?

    #51584
    Daniel Leu
    Participant

    It seems that the script is a bit more complicated than it needs to be. You only have one customer per note, don’t you? The script assumes several. I would remove the save field functionality and just use the return value. Set the return type to text.

    Just out of curiosity, I used ChatGPT to simplify your script and the result seems reasonable:

    function calc() {
        // Fält-ID för det fält i relaterade tabellen som innehåller namn
        var namn_id = 'fld-e59a27d956a442f7b1995c71bffdde1e';
    
        // Fält-ID för fältet som länkar till relaterade poster
        var relaterade_falt_id = 'fld-1234567890abcdef1234567890abcdef';
    
        // Hämta relaterade poster
        var relateradePoster = record.getFieldValue(relaterade_falt_id);
    
        // Kontrollera om det finns exakt en relaterad post
        if (relateradePoster && relateradePoster.length > 0) {
            // Hämta namn från den relaterade posten
            var namn = relateradePoster[0].getFieldValue(namn_id);
    
            // Returnera namnet
            return namn;
        } else {
            // Om inga relaterade poster finns, returnera en tom sträng
            return '';
        }
    }
    

    I think you could achieve the same result by using a calculation field. Select the field of your related form and set the return value to text.

    Cheers, Daniel
    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

    #51582
    Josef Tingbratt
    Participant

    I have a basic CRM-setup. For my meeting notes I connect all of them to a customer (a related table). I would like to fetch the name from the related table (customer name) and put in to a text-field as a string. Been trying with CHatGPT with the following script but it doesn’t fill the text filed with the value:

    function calc() {
    // Fält-ID för det fält i relaterade tabellen som innehåller namn
    var namn_id = ‘fld-e59a27d956a442f7b1995c71bffdde1e’;

    // Fält-ID för fältet som länkar till relaterade poster
    var relaterade_falt_id = ‘fld-1234567890abcdef1234567890abcdef’;

    // Fält-ID för textfältet “Kundnamn”
    var kundnamn_id = ‘fld-9b111db729f343629bdc9823c7af9810’;

    // Hämta relaterade poster
    var relateradePoster = record.getFieldValue(relaterade_falt_id);

    // Kontrollera om det finns relaterade poster
    if (relateradePoster && relateradePoster.length > 0) {
    // Skapa en lista med namn från relaterade poster
    var namnLista = relateradePoster.map(function(poster) {
    return poster.getFieldValue(namn_id); // Hämta namn från varje relaterad post
    });

    // Kombinera namnen till en kommaseparerad lista
    var sammanstalltNamn = namnLista.join(‘, ‘);

    // Skriv värdet till fältet “Kundnamn”
    record.setFieldValue(kundnamn_id, sammanstalltNamn);

    } else {
    // Om inga relaterade poster finns, töm fältet “Kundnamn”
    record.setFieldValue(kundnamn_id, ”);
    }

    // Returnera ingenting (fokus är på att skriva till textfältet)
    return ”;
    }

    #51581
    JScottA
    Participant

    The following response to your post is from Tap Forms 5 Assistant (an unofficial custom GPT). Use with caution. Maybe it can assist until Brendan is able to get to you. Here is the response:

    The issue you’re experiencing with addFileFromUrlToField crashing Tap Forms might be related to one of these factors:
    1. Field Configuration: Ensure the file attachment field (file_attachment_id) is correctly set up as a “File Attachment” field and that the “Multi-File Enabled” option is properly configured. The field must support attaching files. You can verify this by checking the properties of the field in the Tap Forms application under Field settings.
    2. URL Accessibility: Ensure the PDF URL is accessible and not blocked by any permissions or firewalls. Test the URL in a browser to confirm that it downloads correctly.
    3. Scripting API Limitation: According to the documentation, there could be platform-specific limitations or bugs in the JavaScript API for Tap Forms, especially on iPadOS. If addFileFromUrlToField works on other platforms but not on iPadOS, this indicates a bug that should be reported to Tap Forms support.
    4. Error Handling in Script: Add error handling to your script to catch any issues during execution. For example:

    function Test_Pdf() {
        try {
            // Field ID
            var file_attachment_id = 'fld-858558a7165649e6ac3bd117e17483ec';
            // File URL
            var url = "https://cdn.manula.com/user/1444/1328_16794_en_1538683502.pdf?v=20240123105634";
            // Add new record
            var newRecord = form.addNewRecord();
            // Add file to field
            newRecord.addFileFromUrlToField(url, file_attachment_id);
            document.saveAllChanges();
        } catch (e) {
            console.log("Error: " + e.message);
        }
    }
    
    Test_Pdf();

    5. File Size or Compatibility: Large files or unsupported file formats could also cause crashes. Test with smaller PDFs or other supported formats to identify if file size or type is contributing to the issue.
    6. Bug Report: If none of the above resolves the issue, it might be a bug in the Tap Forms app. You should report this to Tap Forms support, providing details about the script, iPadOS version, and steps to reproduce the issue.

    For additional scripting resources, refer to the Tap Forms Scripting 101 guide, which might offer more insight into debugging and advanced script usage in Tap Forms.

    #51576
    Daniel Leu
    Participant

    When you run the script in the editor, what is the console output? Do you get anything?

    Attached is my test form showing that my script works.

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

    Cheers, Daniel
    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

    #51574
    Calion
    Participant

    How do I get the field ID?

    // Okay, I found the answer to that, but nothing is showing up in the script field.

    Here’s the script:

    function Table_Text() {
    
    	// set field value according to your form
    	const table_id = 'fld-26a15768a8f54cc5b8cc3cec0cd7d295';
    
    	// get table records
    	let tableRecs = record.getFieldValue(table_id);
    	
    	// extract all table values
    	let tableValues = [];
    	tableRecs.forEach( (a) => {tableValues.push(a.values)});
    	
    	//console.log("Raw data:\n"+JSON.stringify(tableValues,"",4));
    	
    	// extract all table values
    	let tableText = [];
    	for (tableValue of tableValues){
    		let row = [];
    		for (const [key, value] of Object.entries(tableValue)) {
    			row.push(value);
    		}
    		tableText.push(row.reverse().join(", "));
    	}
    	
    	//console.log("Extracted data:\n"+tableText.join("\n"));
    	return tableText.join("\n");	
    }
    
    Table_Text();

    I set the Result Type to text, but that didn’t change anything.

    • This reply was modified 5 months ago by Calion.
    #51573
    Tap forms Forum
    Participant

    I’m trying to write a script that adds a pdf to a file attachment field, but addFileFromUrlToField consistently crashes Tap Forms (at least on iPadOS 18.2.1).
    Here’s an example with the Tap Forms manual:

    function Test_Pdf() {
    	// Field ID
    		var file_attachment_id = 'fld-858558a7165649e6ac3bd117e17483ec';
    	// file url
    		var url = "https://cdn.manula.com/user/1444/1328_16794_en_1538683502.pdf?v=20240123105634"
    	// add new record
    		var newRecord = form.addNewRecord();
    	// add to field
    		newRecord.addFileFromUrlToField(url, file_attachment_id)
    }
    
    Test_Pdf();
    document.saveAllChanges();

    This script does create a new record, but then crashes when it tries to add the pdf. I’ve tried different files and even images that do work when using addPhotoFromUrlToField to a photo field, but Tap Forms always crashes.
    Is there something special I should do to the file object before adding, or is this a bug?

    #51571
    Daniel Leu
    Participant

    What you could do is using a field script that combines all table data into one field. Then when you export your form, such data would be included.

    Here’s an example of such a field script. You need to replace ‘fld-xxx’ with the field id of your table.

    function Table_Text() {
    
    	// set field value according to your form
    	const table_id = 'fld-xxx';
    
    	// get table records
    	let tableRecs = record.getFieldValue(table_id);
    	
    	// extract all table values
    	let tableValues = [];
    	tableRecs.forEach( (a) => {tableValues.push(a.values)});
    	
    	//console.log("Raw data:\n"+JSON.stringify(tableValues,"",4));
    	
    	// extract all table values
    	let tableText = [];
    	for (tableValue of tableValues){
    		let row = [];
    		for (const [key, value] of Object.entries(tableValue)) {
    			row.push(value);
    		}
    		tableText.push(row.reverse().join(", "));
    	}
    	
    	//console.log("Extracted data:\n"+tableText.join("\n"));
    	return tableText.join("\n");	
    }
    
    Table_Text();
    

    Cheers, Daniel
    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

    #51538
    Brendan
    Keymaster

    I’m glad you found the Scripting documentation. Form scripts are activated only when you manually run them. Field scripts are activated when a field you’re getting the value from is modified within the selected record. Creating a Form script automatically attaches it to the selected form.

    #51531

    In reply to: Button to open media

    JScottA
    Participant

    The following is from the Tap Forms Assistant, an unofficial custom GPT. Maybe it can help you:

    To implement a button in your Tap Forms “Pretty View” layout that opens the first attachment in the “Video” field, follow these steps:

    1. Prepare the Field
    • Ensure your “Video” field is a “File Attachment” field. If it supports multiple files, you should confirm this by checking if “Multi-File Enabled” is selected in the field’s properties.

    2. Create or Edit the Layout
    • Navigate to the Layout Designer by selecting the desired form and choosing the Layouts tab.
    • Either create a new layout or edit an existing one.

    3. Add a Button to the Layout
    • Drag a “Button” field from the Layout Designer’s tools panel onto your layout.
    • Position it wherever you’d like it to appear in the “Pretty View.”

    4. Configure the Button Action
    • Assign the button an action to execute a script. Scripts in Tap Forms use JavaScript, which allows you to access and manipulate your database.

    5. Script to Open the First Attachment

    Use the Tap Forms JavaScript API to implement the button’s functionality. Here’s an example of a script:

    // Assuming the field name is “Video”
    var videoFieldName = “Video”;
    var videoField = record.getFieldValue(videoFieldName);

    if (videoField && videoField.length > 0) {
    // Open the first attachment
    var firstAttachment = videoField[0];
    document.showFile(firstAttachment);
    } else {
    document.alert(“No video attached to this record.”);
    }

    This script:
    • Retrieves the attachments from the “Video” field.
    • Opens the first attachment using the document.showFile() method.

    6. Test the Layout
    • Save the layout and test the button to ensure it opens the video as expected.

    For more detailed guidance on layouts and scripting, you can refer to the Tap Forms documentation on Layouts (Page 80) and JavaScript API (Page 58) in the manual .

    If you encounter any issues or need further clarification, let me know!

    #51521
    Keith Tizzard
    Participant

    I have just created my first application in Tap Forms. Works well and was simple to create.

    I now want to explore Scripting but need a few pointers to where to start. I can see the panel on the right of the Form screen with 3 tabs Form, Fields, Scripts.

    Clearly I need to create a new Script in the Scripts tab.

    Questions:
    1 how do you attach a Script to a Form?
    2 How do you attach a Script to a Field?
    3 In each case when is the Script activated?

    Can your please direct me to the documentation that explains this.

    #51517
    Brendan
    Keymaster

    Hi Jeff,

    With a Join relationship, both sides of the relationship are “many” relationships. That means both sides, the parent and the child, can have many records.

    In the Invoices form, there can be only one parent for a child record, so the Link From Form field can be used to directly get a parent record’s value to display on the child. With a Join or Many to Many, you need to use a Script field, get the first parent record, then get the values for the fields you’re interested in.

    A Link From Form field is created automatically for you on the child form (the form you’re linking TO) when you enable the “Show Inverse Relationship” option.

    So basically you need a script that fetches the records from the parent, creates the value you want to display, and returns it.

    Thanks,

    Brendan

    #51496
    Brendan
    Keymaster

    Hi Jeff,

    You could do this with a Form Script. But it would take some time and thought into exactly how you want it to work.

    But certainly a script can copy values from one form, create a record in another form and then set those values into the record in the other form.

    I don’t have time to write the script for you though, but you could look at the scripting information here:

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

    And the Scripting 101 tutorial by TL Ford, which is linked from my support page:

    Support

    Thanks,

    Brendan

    #51492
    JScottA
    Participant

    If it helps, there is an unofficial custom GPT on the ChatGPT store called, Tap Forms 5 Assistant that is tuned specifically for scripts in TF5. Here is the link:

    Tap Forms 5 Assistant

    I hope that it helps you. However, Brendan is the final authority on all things TF5.

    #51491
    Sarah Young
    Participant

    That is very helpful, thank you Brendan! It’s a 1:M relationship, so will need a script.

    I’ve also now found the javascript documentation which I think/hope will get me the rest of the way there. (somehow I missed this yesterday when I was looking for info, so apologies for asking a question that is probably in those docs!)

Viewing 15 results - 121 through 135 (of 2,866 total)