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 - 2,266 through 2,280 (of 2,950 total)
  • Author
    Search Results
  • #37539
    Brendan
    Keymaster

    Hi Federico,

    If you have a one-to-many relationship from Customer to Appliance then yes you can do this easily.

    Make sure you set the Show Inverse Relationship option on your Link to Form field, then Tap Forms will show the parent relationship on the Appliance form.

    However, once you’ve done that, you can pick out values from specific parent fields just by adding a Calculation field that references a field from the parent. Just make sure you set the Return Type to be the correct type. If it’s the name of the contact, then it’s probably Text.

    You can also use a Script field for the same purpose.

    Thanks,

    Brendan

    #37537
    Federico Santello
    Participant

    Hello everyone.
    I have a question.
    I have a form for customer, each customer can have multiple appliance and i Have create a child form where i put the appliance.
    In the child form i want that some field as name of the customer and address will be filled automatically from the customer form.
    How can i do?
    I need Java script?
    Someone can help me with the script?

    Thank’s
    Federico

    #37535

    In reply to: record or records ?

    Marcus
    Participant

    Let did not solve it.

    How to explain ?
    I do have a form with a lot of repeating fields.
    Name1
    Date1
    Task1

    Name2
    Date2
    Task2

    Name3
    Date3
    Task3

    A script will look for the next empty date,
    will set a date, name and task.

    This script is executed when a recordset is open.

    Another script will be executed as „clean up“,
    remove expired recordsets by date.

    My goal is to prevent the execution of a script in the wrong view.

    There are differences between a loop over all recordsets
    or just the current visible record.

    #37530
    Daniel Leu
    Participant

    You can use standard Javascript date and time functions for this: https://www.w3schools.com/jsref/jsref_obj_date.asp:

    var date_id = 'fld-xxxx';
    
    var d = record.getFieldValue(date_id);
    console.log(d.toLocaleTimeString());

    Cheers, Daniel

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

    #37529
    Martin Inchley
    Participant

    I’m writing a script to concatenate info from lots of fields, including a Time field, and insert it into a Notes field. The line with data from the Time field includes the date 1st Jan, 1970, before the time. I need it to include the time of day, and no date.

    #37484
    Sam Moffatt
    Participant

    The Script Editor has a field list on the left that will let you insert snippets with the field ID’s populated for you, either assigning the ID to a variable or creating a line that pulls the value from the field in one step. If you have linked forms, it’ll also include the fields from those as well. It’s not quite as visually smooth as the calculation editor but I think it’s a pretty reasonable compromise to support scripting in Tap Forms.

    #37481
    Daniel Leu
    Participant

    Changing the name of a field doesn’t require changes to the script as long as you use the field id instead of the field name.

    Cheers, Daniel

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

    #37477
    Eddy
    Participant

    Hi Sam!

    Thanks for this advice.

    Actually I already thought about that, but…
    if I use the calculation field, the names of the field I am using in a formula seem to be updated/actualized, whenever I change the name of a field. Yes, of course you can say, it is not the clean way to change field names, but this is what I have to do sometimes, as long as I develop.

    What I am wondering about is that: If you need a line break in the calculation field, you have to insert “\r”. So, obviously there is something like an escape code. So, how can I box special characters with an escape code? I think, I just have to learn, which coding for the characters is nessesary. Obviosly, it’s not like in java/javascript, because this I tried already…

    Regards, Eddy

    #37475
    Sam Moffatt
    Participant

    Try using a script field instead of a calculation field. Script fields use Javascript and are a little more lenient. It’s slightly more code but a lot more powerful. Any time I go beyond a basic calculation I jump over to a script field, easier to debug and update.

    #37439

    Topic: Get record by key

    in forum Script Talk
    Sam Moffatt
    Participant

    I had a situation where I wanted to automatically download a set of files from the web and store them as Tap Forms document. In my case I have two forms: one parent form that stores the URL I want to grab data from as a base and second form that I want put new records with more details keyed for me by source URL.

    It takes a few parameters to operate (the last three are optional):

    • formName: The name of the source form for the records; can also be a form ID (required).
    • keyFieldId: The field ID of the key field in the record (required).
    • keyValue: The value to match as the key (required).
    • createIfMissing: Control if a record is created if none is found (default: true).
    • earlyTermination: Control if function terminates as soon as a match is found (default: true).
    • alwaysScanOnMiss: Control if we should assume concurrent access (default: false)

    I use this with my Script Manager idiom, so I have something like this:

    document.getFormNamed('Script Manager').runScriptNamed('getLinkedRecord');
    let currentRecord = getRecordFromFormWithKey('Location Tracker', source_id, entry.href, true, false);
    

    The early termination option is interesting and how I’d advise using it is dependent upon your use case. If you set early termination, when the script is iterating through all of the records it will return the first match it finds. If you only expect to call this function once, maybe twice, then this will work fine for you because depending on your ordering you will avoid a full traversal of your recordset.

    If you are running this in a loop or some other construct where you expect to call the function multiple times then disabling early termination ensures that everything is indexed on the first pass. If you expect to have many cache hits, then this will improve performance significantly as subsequent calls will immediately return based on a direct lookup. Record creation is added as a method here so that newly created records can be immediately indexed in the system avoiding future cache misses.

    The worst case for this method is a cache miss. After the first scan of the records, it assumes no other concurrent access. If you don’t want this behaviour, there is another flag to always scan if a miss isn’t found and the form has already been scanned.

    Here’s the script:

    // ========== getRecordFromFormWithKey Start ========== //
    // NAME: Get Record From From With Key
    // VERSION: 1.0
    
    /**
     * Get a record from a form with a key field, optionally creating it if missing.
     * Note: assumes your key field is unique.
     *
     * formName           The name of the source form for the records (required).
     * keyFieldId         The field ID of the key field in the record (required).
     * keyValue           The value to match as the key (required).
     * createIfMissing    Control if a record is created if none is found (default: true).
     * earlyTermination   Control if function terminates as soon as a match is found (default: true).
     * alwaysScanOnMiss   Control if we should assume concurrent access (default: false)
     */
    function getRecordFromFormWithKey(formName, keyFieldId, keyValue, createIfMissing = true, earlyTermination = true, alwaysScanOnMiss = false)
    {
    	// Check if our basic parameters are set.
    	if (!formName || !keyFieldId || !keyValue)
    	{
    		throw new Error("Missing required parameters");
    	}
    	
    	// Determine the target form (check if we were given an ID or else assume a name)
    	let targetForm = undefined;
    	if (formName.match(/frm-/))
    	{
    		targetForm = document.getFormWithId(formName);
    	}
    	else
    	{
    		targetForm = document.getFormNamed(formName);
    	}
    
    	// Check if our global variable has been setup.
    	if (!indexedRecordIndex)
    	{
    		var indexedRecordIndex = {};	
    	}
    	
    	// Flag for if we've indexed this form already.
    	if (!indexedRecordState)
    	{
    		var indexedRecordState = {};
    	}
    
    	// Create a key for this form-field combination.
    	// Form+Field is the key.
    	let indexedRecordKey = targetForm.getId() + "_" + keyFieldId;
    
    	// Check to see if this particular link field has been setup.
    	if (!indexedRecordIndex[indexedRecordKey])
    	{
    		indexedRecordIndex[indexedRecordKey] = {};
    	}
    
    	
    	// Short circuit if we have an exact match.
    	if (indexedRecordIndex[indexedRecordKey][keyValue])
    	{
    		return indexedRecordIndex[indexedRecordKey][keyValue];
    	}
    	
    	// No immediate match, check to see if we should scan everything.
    	// alwaysScanOnMiss forces this code path each execution.
    	// The check to indexedRecordState is if this has been indexed.
    	if (alwaysScanOnMiss || !indexedRecordState[indexedRecordKey])
    	{	
    		// Brute force search :(
    		let records = targetForm.getRecords();
    
    		// Iterate through all of the records and look for a match.
    		for (currentRecord of records)
    		{
    			// Set up a reverse link for this value.
    			let recordKeyValue = currentRecord.getFieldValue(keyFieldId);
    			indexedRecordIndex[indexedRecordKey][recordKeyValue] = currentRecord;
    
    			// If this is a match and early termination is setup, return immediately.
    			if (earlyTermination && recordKeyValue == keyValue)
    			{
    				return currentRecord;
    			}
    		}
    		
    		// Flag this record-field as being indexed.
    		indexedRecordState[indexedRecordKey] = true;
    	}
    
    	// Check to see if we got a match here and return if the key exists.
    	if (indexedRecordIndex[indexedRecordKey][keyValue])
    	{
    		return indexedRecordIndex[indexedRecordKey][keyValue];
    	}
    	else if (createIfMissing)
    	{
    		// If createIfMissing is set, create a new record.
    		// Note: it's expected the caller will call document.saveAllChanges();
    		let newRecord = targetForm.addNewRecord();
    		// Set the key value to our search value.
    		newRecord.setFieldValue(keyFieldId, keyValue);
    		// Link this up to save us another lookup in future.
    		indexedRecordIndex[indexedRecordKey][keyValue] = newRecord;
    		// And now we return the new record. 
    		return newRecord;
    	}
    	
    	// If we didn't find anything, return undefined.
    	return undefined;
    }
    // ========== getRecordFromFormWithKey End ========== //
    
    #37397
    Sam Moffatt
    Participant

    I created a form called ‘Script Manager’ which I use to put form scripts and then you can load them like this:

    document.getFormNamed('Script Manager').runScriptNamed('Form Logger');
    

    I use this idiom for a bunch of sample scripts you can find on the forum.

    #37395
    Rocky Machado
    Participant

    Hi – Not sure if I am using the right terminology, but How do I create global functions?
    for example, I my have a formatting function that I want to use in different Scripts that I create within a form.

    Thanks,
    rocky

    #37390
    Brendan
    Keymaster

    Hi Mackay,

    Yes, you can do this in a Tap Forms formula. But it’s much easier to deal with in a Script field rather than a Calculation field.

    Your example though doesn’t quite make sense because there’s no condition in the first parameter.

    You need to compare Field with something.

    For example:

    IF(Field = 10; "Spring"; IF(Field = 20; "Summer"; IF(Field = 30; "Fall"; "")))

    So if field is 10, return Spring, otherwise if field is 20, return Summer, otherwise if field is 30, return Fall, otherwise return the empty string.

    But in a Script, it would look like this:

    if (field == 10) {
       return "Spring";
    } else if (field == 20) {
       return "Summer";
    } else if (field == 30) {
       return "Fall";
    } else {
       return ""
    }

    To me that’s just easier to read than the nested IF() function.

    You can also use a switch statement, which might look nicer.

    Thanks,

    Brendan

    #37388

    In reply to: External Apps

    Brendan
    Keymaster

    Hi Denny,

    The Default Value option is your only way of launching another app from within Tap Forms. You can put parameters in the URL that get their values from other fields if you might need that kind of functionality. Just put the field name within the URL like this: https://www.tapforms.com/%5Bsome other field] and Tap Forms will substitute in the value for you.

    You can use a script to fetch an image from a URL and populate a Photo field with it in Tap Forms.

    See the Scripts section of the user manual for details:

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

    There’s even a Fetch Movie Data from Web Service example that shows how to fetch data from a web service, including getting a photo from a URL:

    record.addPhotoFromUrlToField(imageUrl, case_cover);

    Hope that helps!

    Brendan

    #37371
    Rocky Machado
    Participant

    Hi – I actually have two questions about Recalculating. I currently have an import form that I used to review transactions (data scrubbing) before I put them into the final form.

    My First question, I have a Purge All Transactions function that cycles through all the records and deletes them so I can begin a new import (usually 10-20 transactions a day). I notice when I do this it appears that the records are not deleted, but when I click the refresh record list button then the go away. Is there a method that would allow me to refresh the list? or will I always need to click on the refresh record list button to refresh the form?

    My Second Question, In my import form I have several scripts that I created to parse certain fields in the import process. I noticed when I import my records I have to click on the refresh record list form for my scripts take affect. Is there a method or process to force a recall or do I also need to click on the refresh record list button to refresh my records?

    thanks,
    rocky

Viewing 15 results - 2,266 through 2,280 (of 2,950 total)