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 'form.getRecords'

Tap Forms Database Pro for Mac, iPhone, iPad and Apple Watch Forums Search Search Results for 'form.getRecords'

Viewing 15 results - 46 through 60 (of 154 total)
  • Author
    Search Results
  • #48845
    Daniel Leu
    Participant

    One cool feature in TF is that picklist entries can come from a form. So I have a form called ‘Brands’ that contains name and URL fields. I select this form to be the source of my picklist. Then in my main form, I use that picklist to select the brand. Further, I use a field script ‘URL Script’ to set the value of the URL field. The field script is triggered whenever a different brand is selected and then the URL field is updated accordingly.

    When the script is triggered, it fetches all entries of the ‘Brands’ form and loops through them. Once a match is found, the URL field is set according to that matching record entry and the script aborted:

    function Url_Script() {
    
     	var brand_id = 'fld-feb500eed99747169d8d90db3a620243';
    	var url_id = 'fld-0a342472ca0b44b1a52c3758c5be92e4';
    
    	const brands_name_id = 'fld-8d373c0912154c69a7f579e696cd598c';
    	const brands_url_id = 'fld-0a342472ca0b44b1a52c3758c5be92e4';
    
    	let brand = record.getFieldValue(brand_id);
    	
    	// get the brands form. Note that the name of the form must match!
    	let brandsForm = document.getFormNamed("Brands");
    	
    	for (let rec of brandsForm.getRecords()){
    		if (rec.getFieldValue(brands_name_id) == brand){
    			let url = rec.getFieldValue(brands_url_id);
    			console.log("found match: " + brand) + " with url " + url);
    			record.setFieldValue(url_id, url);
    			document.saveAllChanges();
    			return url;
    			break;
    		}
    	}
    
    }
    
    Url_Script();
    

    Hope this is what you were looking for. I have attached my example document.

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

    Cheers, Daniel

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

    #48715

    In reply to: List with Check box

    Brendan
    Keymaster

    The Advanced Find & Replace function is under the Records menu on the Mac version.

    There’s a script snippet called Basic Loop which just sets up a loop. But you would have to fetch the records first with var records = form.getRecords(); and then get each record within the loop and then set the value of the field using the record.setFieldValue(some_checkmark_field_id, 0); function. The 0 would set the checkmark field value back to off.

    Don’t forget to do a form.saveAllChanges(); outside of the loop at the end before the program ends so Tap Forms can save the values.

    This Form Script will reset all the checkmark fields for your Shield field back to OFF:

    function Reset_Shields() {
    
    	var shield_id = 'fld-d13c2bafe6e8482288c551bd8c931373';
    	var records = form.getRecords();
    	
    	for (var index = 0, count = records.length; index < count; index++){
    		var theRec = records[index];
    		theRec.setFieldValue(shield_id, 0);
    	}
    	form.saveAllChanges();
    }
    
    Reset_Shields();
    #48679

    In reply to: Learning JavaScript

    Daniel Leu
    Participant
    function Season_to_date() {
    	
    	var records = form.getRecords();
        	var currentRecordIndex = records.indexOf(record);
    
        	if (currentRecordIndex > 0) {
            	var previousRecord = records[currentRecordIndex-1];
        	} else {
         		return 0;
        	}
    
    	// Is this the beginning of the year?
    	var date = record.getFieldValue('fld-0720a5baa6fc4980aee22798bd089714');
    	if (date.getMonth() == 9 && date.getDate() == 1){
    		return 0;
    	}
    	
    	var today = record.getFieldValue('fld-61c03b767f2c497491d10a28db8abdb3');
    	var previous_total = previousRecord.getFieldValue('fld-f6bc7e82da874579940fb8afc167dac4');
    	var total = today + previous_total;
    	
    	console.log("Today: " + today)
    	console.log("Previous: " + previous_total)
    	console.log("Total: " + total)
    	
    	return total;
    }
    
    Season_to_date();
    

    Cheers, Daniel

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

    #48675

    In reply to: Learning JavaScript

    Glen Forister
    Participant

    That is great. I put it in the script right after the other “var”s near the top. Hope that is the right place.

    function Get_Previous_Record() {

    var records = form.getRecords();
    var currentRecordIndex = records.indexOf(record);
    // added var for Oct 1st.
    var previousRecord = nil;
    var date = record.getFieldValue(‘fld-0720a5baa6fc4980aee22798bd089714’);
    if (date.getMonth() == 9 && date.getDate() == 1){ // check for October 1st
    return 0;
    }
    // end added var for Oct 1

    if (currentRecordIndex > 0) {
    previousRecord = records[currentRecordIndex – 1];
    }

    if (previousRecords != nil) {
    // fetch some value and do some calculation with it and the current record
    // the currently selected record is always accessed with the built-in variable
    // record
    }
    }

    Get_Previous_Record();

    #48670

    In reply to: Learning JavaScript

    Daniel Leu
    Participant

    This should to the trick to reset the season total at the beginning of the year:

    function Season_to_date() {
    	
    	var records = form.getRecords();
        	var currentRecordIndex = records.indexOf(record);
    
        	if (currentRecordIndex > 0) {
            	var previousRecord = records[currentRecordIndex-1];
        	} else {
         		return 0;
        	}
    
    	// Is this the beginning of the year?
    	var date = record.getFieldValue('fld-0720a5baa6fc4980aee22798bd089714');
    	if (date.getMonth() == 0 && date.getDate() == 1){
    		return 0;
    	}
    	
    	var today = record.getFieldValue('fld-61c03b767f2c497491d10a28db8abdb3');
    	var previous_total = previousRecord.getFieldValue('fld-f6bc7e82da874579940fb8afc167dac4');
    	var total = today + previous_total;
    	
    	console.log("Today: " + today)
    	console.log("Previous: " + previous_total)
    	console.log("Total: " + total)
    	
    	return total;
    }
    
    Season_to_date();

    Cheers, Daniel

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

    #48648

    In reply to: Learning JavaScript

    Daniel Leu
    Participant

    Hi Glen,

    Thank you for sharing your form! The script Brendan posted is for a field of the type Script and not Calculation.

    Here is the updated script that works for me:

    function Season_to_date() {
    	
    	var records = form.getRecords();
        	var currentRecordIndex = records.indexOf(record);
    
        	if (currentRecordIndex > 0) {
            	var previousRecord = records[currentRecordIndex-1];
        	} else {
         		return 0;
        	}
    
    	var today = record.getFieldValue('fld-61c03b767f2c497491d10a28db8abdb3');
    	var previous_total = previousRecord.getFieldValue('fld-f6bc7e82da874579940fb8afc167dac4');
    	var total = today + previous_total;
    	
    	console.log("Today: " + today)
    	console.log("Previous: " + previous_total)
    	console.log("Total: " + total)
    	
    	return total;
    }
    
    Season_to_date();

    Cheers, Daniel

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

    #48641

    In reply to: Learning JavaScript

    Brendan
    Keymaster

    You can get the previous record with this code:

    function Get_Previous_Record() {
    
    	var records = form.getRecords();
    	var currentRecordIndex = records.indexOf(record);
    	var previousRecord;
    	
    	if (currentRecordIndex > 0) {
    		previousRecord = records[currentRecordIndex - 1];
    	}
    	
    	if (previousRecord != undefined) {
    		// fetch some value and do some calculation with it and the current record
    		// the currently selected record is always accessed with the built-in variable
    		// record
    	}
    }
    
    Get_Previous_Record();
    • This reply was modified 2 years, 9 months ago by Brendan.
    • This reply was modified 2 years, 9 months ago by Brendan.
    • This reply was modified 2 years, 9 months ago by Brendan. Reason: Fixed a syntax error
    #48321

    In reply to: Date comparison

    Daniel Leu
    Participant

    To get the comparison to work, you need to get the numerical representation of the date. Additionally, the date object needs to set to the beginning of the day to properly detect a prior date.

    function Delete_Expired_Docs() {
    
    	var records = form.getRecords();
    	var field_id = 'fld-xxx';
    	
    	// Get time in ms at beginning of today
    	let now = new Date();
    	now.setSeconds(0);
    	now.setMinutes(0);
    	now.setHours(0);
    	now.setMilliseconds(0);
    	const today = now.getTime();
    		
    for (var index = 0, count = records.length; index < count; index++){
        var aRecord = records[index];
        var field_value = aRecord.getFieldValue(field_id);
    
        if (field_value.getTime() < today) {
            //form.deleteRecord(aRecord);
            console.log("found record");
        } else {
        	console.log("record doesn't match");
        }
    }
    
    form.saveAllChanges();
    
    }
    
    Delete_Expired_Docs();

    BTW, I don’t like deleting records… I would just mark a record as ‘expired’. But that depends really on your data model.

    Cheers, Daniel

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

    #48313

    Topic: Date comparison

    in forum Script Talk
    Ian Wheeler
    Participant

    Hi Folks,

    I am trying to check via a script whether a Date field contains a date that is earlier than today. If it is then I want to delete the record. Ideally this would be a manually run script. I am very new to TF (and loving it, and am in the process of migrating over from Filemaker) and Javascript. Below is the script that I have which just doesn’t work. I suspect it is my Date comparison element that is the problem as the Console.Log isn’t detecting any errors, but I don’t know how to fix it. Any help would be greatly appreciated

    function Delete_Expired_Docs() {
    
    	var records = form.getRecords();
    var field_id = 'fld-46f16c00d13344e79478a8b629022ee7';
    	
    for (var index = 0, count = records.length; index < count; index++){
        var aRecord = records[index];
        var field_value = aRecord.getFieldValue(field_id);
        if (field_value < Date()) {
            form.deleteRecord(aRecord);
        }
    }
    
    form.saveAllChanges();
    
    console.log(field_value);
    console.log(count);
    console.log(Date());
    
    }
    
    Delete_Expired_Docs();
    • This topic was modified 2 years, 10 months ago by Brendan.
    • This topic was modified 2 years, 10 months ago by Brendan. Reason: formatted code
    #48041
    Jake Gregory
    Participant

    Thank you for your response Brendan, I decided to have a bash on my own – not much luck though lol

    However I have managed to rather clumsily get it working from the code you provided.


    function Update_Prices() {

    var group_1_id = 'fld-0305473a4c7e41f69ee491266c99ec03';
    var group_2_id = 'fld-93197576ce50421599626d9ccacff2f5';
    var group_3_id = 'fld-ec93413e3880425da973d35d93b99595';
    var group_4_id = 'fld-cd36a8501faf45408cd88e607e0bc4e1';

    var records = form.getRecords();
    var price_increase_percent = 0.12;

    for (var index = 0, count = records.length; index < count; index++){
    var rec = records[index];

    var g1_current_price = rec.getFieldValue(group_1_id);
    var g2_current_price = rec.getFieldValue(group_2_id);
    var g3_current_price = rec.getFieldValue(group_3_id);
    var g4_current_price = rec.getFieldValue(group_4_id);

    var g1_new_price = g1_current_price + g1_current_price * price_increase_percent;
    var g2_new_price = g2_current_price + g2_current_price * price_increase_percent;
    var g3_new_price = g3_current_price + g3_current_price * price_increase_percent;
    var g4_new_price = g4_current_price + g3_current_price * price_increase_percent;

    rec.setFieldValue(group_1_id, g1_new_price);
    rec.setFieldValue(group_2_id, g2_new_price);
    rec.setFieldValue(group_3_id, g3_new_price);
    rec.setFieldValue(group_4_id, g4_new_price);

    }
    form.saveAllChanges();

    }

    Update_Prices();

    I’m sure there’s a much better way to do this, but I’ll get there.

    I want to add a prompt box that asks for a user input for a percentage and then have a confirmation box ask them to confirm.

    I’m currently exploring the prompt options and I’m sure I’ll have more questions later lol
    Thanks again for your replies.

    #48040
    Brendan
    Keymaster

    Hi Jake,

    I replied to your email about this topic, but I’ll reply here too with the code I wrote for you in case anyone else needs this type of code:

    function Update_Prices() {
    
    	var price_field_id = 'fld-.....';
    	var records = form.getRecords();
    	var price_increase_percent = 0.05;
    	
    	for (var index = 0, count = records.length; index < count; index++){
    		var rec = records[index];
    		var current_price = rec.getFieldValue(price_field_id);
    		var new_price = current_price + current_price * price_increase_percent;
    		rec.setFieldValue(price_field_id, new_price);
    	}
    	form.saveAllChanges();
    
    }
    
    Update_Prices();
    #47893
    John Cranney
    Participant

    It does make sense, thanks.

    I was trying to get it to work using a script and I think I found some odd behaviour.

    Say I have a linked form (stuff_link_id) and a table that I want to display a view of records in that table (stuff_table_id). If I click the checkmark button as you mention above, I get a copy of the selected stuff_link record in my table, with values magically populated.

    However if I run the following (which I was hoping would populate the table with a view of whatever is linked in link) it deletes the record from link and moves it to table. Is that intentional? I expected it would do the same magic population. I’ve got a workaround which is to use duplicate() but I need to manually set all the columns.

    var records=form.getRecords();

    for (var rec of records){
    for (var stuff_rec of rec.getFieldValue(stuff_link_id)) {
    rec.addRecordToField(stuff_rec,stuff_table_id);
    }
    }

    document.saveAllChanges();

    #47426
    David Gold
    Participant

    I’ve had a go at some Javascript but can’t get it to work. Basically I want it to take a search term off the clipboard, do a search and get the record ID for the found item and then copy that to the clipboard. It isn’t working so could someone tell me what I have done wrong?

    var myForm = document.getFormNamed(‘Scopus RE’);
    var records = myForm.getRecords();
    var search_term = Utils.copyTextFromClipboard();
    var result_count = 0;
    var results = [];
    var selected;

    function search_records( haystack , needle ) {

    var name_id = ‘fld-90984efc3585456ab56763adff0a5228’;
    var rec;

    for (const rec of haystack) {
    if ( rec.getFieldValue( name_id ).toLowerCase().includes( needle.toLowerCase() ) ) {
    results.push( { name: rec.getFieldValue( name )
    } );
    result_count++;
    }

    }

    if( result_count == 0 ){
    console.log( ‘No results found!’ );
    }else if( result_count > 1 ){
    multiple_results();
    }else{
    copy_record.getId( results[0] );
    }

    }

    #47414
    Daniel Leu
    Participant

    I would use something like this:

    if (typeof search !== 'undefined'){
    	var records = search.getRecords();
    } else {
    	var records = form.getRecords();	
    }

    This doesn’t address the corner case where search exists but doesn’t contain any records, eg records.length == 0.

    I wouldn’t use record in var record = ... since that is a predefined value.

    Cheers, Daniel

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

    David Schwane
    Participant

    I have this script that returns a random record regardless if I’m on a form or a saved search. Works great but I know using try catch this way is not correct. What is a better way to resolve if I’m trying to get a form recordset or search recordset?

    Random_Record();
    try {
    var records = search.getRecords();
    }
    catch (err) {
    var records = form.getRecords();
    }
    var record = records[Math.floor(Math.random() * records.length)];
    form.selectRecord(record);

Viewing 15 results - 46 through 60 (of 154 total)