Blog  |  Support  |  Forums

Search Results for 'form.getRecords'

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Search Search Results for 'form.getRecords'

Viewing 15 results - 16 through 30 (of 141 total)
  • Author
    Search Results
  • #50008

    In reply to: Open record as result

    David Gold
    Participant

    Hi Daniel (or OP)

    I would like to be able to do this also. I have some code below (not sure exactly how to insert a code block). I’m trying to do what you’ve said in line 11 with function show_name. Basically I want to be able to search the name field and when I get a result (whether multiple or single by selection in prompter) it then opens the record in Tap Forms. Can you tell me how to make it work?

    Really appreciate any help as my javascript knowledge is limited.

    var myForm = document.getFormNamed(‘Test’);
    var records = myForm.getRecords();
    var search_term = Utils.copyTextFromClipboard();
    var result_count = 0;
    var results = [];
    var results_name = [];
    var selected;
    function show_name( name ) {
        form.selectRecord(name[0]);
    }
    function copy_result_multiple( name ) {
        if ( name == true ) {
            console.log( ‘Index:’ + results.indexOf( selected ) );
            console.log( results_name[ results.indexOf( selected ) ] );
            show_name( results_name[ results.indexOf( selected ) ] );
        } else {
            console.log( ‘Cancelled’ );
        }
    }
    function multiple_results( all_results ) {
        let prompter = Prompter.new();
        prompter.cancelButtonTitle = ‘cancel’;
        prompter.continueButtonTitle = ‘Show Name’;
        prompter.addParameter(‘Select Result ‘, ‘selected’, ‘popup’, all_results)
        .show(‘Multiple Results Found’, copy_result_multiple );
    }
    function search_records( haystack , needle ) {
    var name_id = ‘fld-1acb38c0b51b44698849501407b51722’;
        var rec;
        for (rec of haystack) {
        var name = rec.getFieldValue( name_id );
        if (name && name.includes( needle ) ) {
                results.push( rec.getFieldValue( name_id ) );
                results_name.push( rec.getFieldValue( name_id ) );
                result_count++;
            } else {
            if (! name) {
            console.log(“Empty field: ” + rec.getUrl());
            }
            }
        }
        if( result_count == 0 ){
            Utils.alertWithMessage(“No Results”, “No results were found for the search term: ” + needle);
        }else if( result_count > 1 ){
            //multiple results
            multiple_results( results );
        }else{
            //single result
            show_name( results_name[0] );
        }
    }
    search_records( records , search_term );
    #49998
    David Gold
    Participant

    I’ve had a go at it cutting and pasting from some other scripts but can’t work out why it’s not working. Am getting an error in the console saying:

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

    function copy_name( name ) {
    Utils.copyTextToClipboard( name );
    }

    function multiple_results() {

    var joined = ‘–multiple_matches–‘;
    var res;
    for (res of results) {
    joined = joined +
    res.name;
    }
    copy_comments( joined );
    }

    function search_records( haystack , needle ) {

    var name_id = ‘fld-1acb38c0b51b44698849501507b51722’;
    var rec;

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

    }

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

    }

    search_records( records , search_term );

     

    • This reply was modified 6 months, 3 weeks ago by David Gold.
    #49866
    Brendan
    Keymaster

    Hi Brian,

    I think this will take a bit of work. There is an API called record.addRecordToField(someRecord, field_id); in Tap Forms. You’ll need to call that to add the record to your Link to Form field.

    Now the tricky part is to get the record that you need from the linked form. That would be the someRecord in the above API.

    You will also probably need to use the var records = form.getRecordsForSearchTerm("search term"); API call. Now, the form you use will need to be the Link to Form field’s form. So to get that, you’ll need to call the document.getFormNamed('My Movie Library'); API and assign it to a variable (not named form).

    For example:

    var link_to_form_field_id = 'fld-......';
    var linkedForm = document.getFormNamed('Linked Form');
    var records = linkedForm.getRecordsForSearchTerm("some unique course code");
    var linkedRecord = records[0]; // assuming your course code is unique you'll get just one record back
    record.addRecordToField(linkedRecord, link_to_form_field_id);
    form.saveAllChanges();
    

    Hopefully that’ll get you what you need. I haven’t tested this code (just off the top of my head), but it should generally work. You may need to do some testing to check to see if you actually get records back.

    • This reply was modified 7 months, 3 weeks ago by Brendan.
    #49839

    In reply to: Convert 2 fields

    Brendan
    Keymaster

    There’s no function getSelectedRecords(). There is a form.getRecords(); function though.

    The current record can be obtained by simply referencing record.

     

    So get rid of the var record = form.getSelectedRecords()[0]; statement.

    For "sourceField" you’ll need to put in the field ID of the field you want to get the value from. Tap Forms will display the field ID when you select it. Or just select the field and click the ID button to have Tap Forms generate the line of code for you.

    Also there’s no record.save() function. You want to use form.saveAllChanges() instead.

    #49794
    Daniel Leu
    Participant

    In one of my forms, I encountered an issue of  missing parents. The contact field should contain the infos from the parent. This form script loops over all records of a form and checks if the contact field contains records. If no record is found (eg, length == 0), I set the select field. Afterwards, I can use a smart filter to find all these records where the select field is set to 1. This way, I can verify that these records are indeed the once I want to delete. Maybe this helps you!

    function Select_No_Contact_Parent() {
    
       const contact_id = 'fld-xxx';
       const select_id = 'fld-xxx';
       for (rec of form.getRecords()){
         let contacts = rec.getFieldValue(contact_id);
          if (contacts.length == 0) {
           rec.setFieldValue(select_id, true);
          }
       }
       document.saveAllChanges();
    }
    Select_No_Contact_Parent();
    • This reply was modified 8 months, 2 weeks ago by Daniel Leu.
    • This reply was modified 8 months, 2 weeks ago by Daniel Leu.
    • This reply was modified 8 months, 2 weeks ago by Daniel Leu.
    #49127
    Daniel Leu
    Participant

    Re: Form script:

    You just need to loop over all your records and then assign the values on each one:

    
    function wc_attributes_update() {
    
      // definitions
      var year_id = 'fld-bcf37649c9ee4332b7100a9589110f96';
      ....
    
      for (rec of form.getRecords()){
        rec.setFieldValue(attribute_3_value_s_id, model);
        rec.setFieldValue(attribute_4_value_s_id, gauge);
        rec.setFieldValue(attribute_5_value_s_id, container);
        rec.setFieldValue(attribute_6_value_s_id, era);
        rec.setFieldValue(attribute_7_value_s_id, railroad);
        rec.setFieldValue(attribute_8_value_s_id, brand);
        rec.setFieldValue(attribute_9_value_s_id, collection);
        rec.setFieldValue(attribute_10_value_s_id, tca_grade);
      }
      form.saveAllChanges()
    }
    
    wc_attributes_update();    
    

    This example works on all the records. If you only want to update the ones from a saved search, you might use form.getSearchNamed('Genre: Action & Adventure').

    #49097

    In reply to: Simple previous

    Daniel Leu
    Participant

    I don’t really understand what you would like to calculate. But here are my 2 cents:

    You get the error:
    > “Tot$/dwr: ReferenceError: Can’t find variable: previousRecord_total, line:(null)”
    because the variable previousRecord_total is not defined. It should be previousTotal and the statement should be a line lower.

    > var previous_total = previousRecord.getFieldValue($_grp);`
    getFieldValue() needs a field id and not a value.

    For the previous calculation, the field id is the id of the script. You can get it from the form inspector panel > field > select field and look for the ID under the description section.

    I would change as well how the first initial value for currentRecordIndex == 0 is handled:

    var previous_total = 0;
    if (currentRecordIndex > 0) {
       var previousRecord = records[currentRecordIndex-1];    
       previous_total = previousRecord.getFieldValue(totalId);
    }
    

    Now previous_total is initialized to 0. If there is an earlier record, it is set accordingly.

    With these changes, my field script now looks like this:

    function Add_Previous() {
        var $_dwr = record.getFieldValue('fld-c1faad7124f8400bbf9e55c477b101ef');
        var $_grp = record.getFieldValue('fld-72156bcc2515453d80a4649d30cb938f');
        var totalId = 'fld-9bc44cd8a52547fcab897dbfa04afcc2';
    	
        var records = form.getRecords();   // calls function getRecords()
        var currentRecordIndex = records.indexOf(record);
    
        var previous_total = 0;
        if (currentRecordIndex > 0) {
           	var previousRecord = records[currentRecordIndex-1];    
           	previous_total = previousRecord.getFieldValue(totalId);
        }
        
        console.log("dwr: " + $_dwr);
        console.log("grp: " + $_grp);
        console.log("previous total: " + previous_total);
       	
        var total = $_dwr + previous_total;	
        console.log("Total: " + total);
    	
        return total;
    }
    
    Add_Previous();

    Hope this helps!

    #48974

    In reply to: days between dates

    Daniel Leu
    Participant

    I got several errors when I tried to run your code. This touched-up version works for me:

    var date_id = 'fld-xxx';
    
    function Days_Between_Dates(){
    	console.log("Days_Between_Dates() called ...");
    
    	var days = -1;
    	var records = form.getRecords(); // calls function getRecords()
    	var currentRecordIndex = records.indexOf(record);
    
    	if (currentRecordIndex > 0) {
    		var previousRecord = records[currentRecordIndex-1];
    	} else {
    		return -1;
    	}
    
    	// get the two dates
    	const today = record.getFieldValue(date_id);
    	const previous_day = previousRecord.getFieldValue(date_id);
    	
    	// calculate the difference between the two date objects
    	days = Math.round((today - previous_day) / (1000 * 60 * 60 * 24));
    
    	console.log("today: " + today)
    	console.log("previous day: " + previous_day)
    	console.log("difference: " + days)
    	
    	return days;
    }
    
    Days_Between_Dates()

    In Javascript, the Date object represents the time in milliseconds since January 1, 1970, UTC. So the difference needs to be divided by milliseconds-per-day.

    I changed the return value to -1 for the first record.

    When reporting a syntax error, it’s helpful to have the entire message.

    When posting code, please encapsulate it in back-ticks. This way it is easier to copy&paste it and it looks prettier. Thanks!

    Cheers & happy hacking!

    • This reply was modified 1 year, 2 months ago by Daniel Leu.
    #48971

    In reply to: days between dates

    Glen Forister
    Participant

    Ok, Tried that.
    End result = Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    Days_Between_Dates()
    2/20/23, 6:24:40 PM / Psoriasis / Days since last Trt.
    Days_Between_Dates()
    Days since last Trt.: ReferenceError: Can’t find variable: previousRecord, line:(null)

    I’ve done lots of Basic, some Pascal, and some R in the past, but this is very different and I’m lost. Went through the basic training, but still new. It will take lots of experimenting and work to get to where I need to be to fix this. In time I will I hope, but still in the process of making TF usable to me before I branch off into another programming language.

    PS, Have no idea what the “called…” after in the Console.log statement does. I just get a syntax error.

    function Days_Between_Dates(){

    console.log(“Days_Between_Dates()”);
    var date_id = (‘fld-119ad5683104451ca6855e777a23ad21’);

    var records = form.getRecords(); // calls function getRecords()
    var currentRecordIndex = records.indexOf(record);

    if (currentRecordIndex > 0) {
    var previousday = records[currentRecordIndex-1];
    } else {
    return 0;
    }

    var today = record.getFieldValue(date_id);
    var previous_day = previousRecord.getFieldValue(date_id);
    var total = today – previousday;

    console.log(“today: “)
    console.log(“previousday: “)
    console.log(“total: “)

    return total;
    }
    Days_Between_Dates()

    Thanks for looking, but…

    #48962

    In reply to: days between dates

    Glen Forister
    Participant

    I added that. It just prints to the screen after every recored looked at I guess. I don’t get it.
    There aree 26 records.

    Here is the result.
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    Days_Between_Dates() called …
    2/20/23, 10:38:59 AM / Psoriasis / Days since last Trt.
    Days_Between_Dates() called …
    ————-

    function Days_Between_Dates(){
    	var date_id = ('fld-119ad5683104451ca6855e777a23ad21');
    	var records = form.getRecords();   // calls function getRecords()
        	var currentRecordIndex = records.indexOf(record);
    
        	if (currentRecordIndex > 0) {
            	var previousday = records[currentRecordIndex-1];
        	} else {
         		return 0;
        	}
        	
        var today = record.getFieldValue(date_id);
        var previous_day = previousRecord.getFieldValue(date_id);
        var total = today - previousday;
        
        console.log("today: ")
    	console.log("previousday: ")
    	console.log("total: ")
        
        return total;
    }
    function Days_Between_Dates(){
       console.log("Days_Between_Dates() called ...");
      
    }
    
    Days_Between_Dates();
    • This reply was modified 1 year, 2 months ago by Glen Forister.
    • This reply was modified 1 year, 2 months ago by Brendan.
    #48954

    In reply to: days between dates

    Glen Forister
    Participant

    Ok, missed that.
    But I got all the syntax cleared and this script does nothing, not even printing to console the variable values. So I’ve proved I haven’t learned anything yet.
    Any ideas?
    Thanks.

    
    function Days_Between_Dates(){
    	var date_id = ('fld-119ad5683104451ca6855e777a23ad21');
    
    	
    	var records = form.getRecords();   // calls function getRecords()
        	var currentRecordIndex = records.indexOf(record);
    
        	if (currentRecordIndex > 0) {
            	var previousday = records[currentRecordIndex-1];
        	} else {
         		return 0;
        	}
        	
        var today = record.getFieldValue(date_id);
        var previous_day = previousRecord.getFieldValue(date_id);
        var total = today - previousday;
        
        
        console.log("today: ")
    	console.log("previousday: ")
    	console.log("total: ")
        
        return total;
    }
    
    
    #48948
    Glen Forister
    Participant

    I did some study and tried to determine the #days between the date of one record and the previous record. I can’t get past line one syntax. Help please?

    
    Function Days_Between_Dates(){
    
    	var date_id = 'fld-119ad5683104451ca6855e777a23ad21');
    
    	
    	var records = form.getRecords();   // calls function getRecords()
        	var currentRecordIndex = records.indexOf(record);
    
        	if (currentRecordIndex > 0) {
            	var previousday = records[currentRecordIndex-1];
        	} else {
         		return 0;
        	}
        	
        var today = record.getFieldValue(date_id);
        var previous_day = previousRecord.getFieldValue(date_id);
        var total = today - previousday;
        return total;
        
    }
    
    #48934
    Daniel Leu
    Participant

    You need to select ‘script’ and not ‘calculation’ for the field type, return type is ‘number’.

    The script had some errors too like incorrect field ids.

    
    function Season_to_date() {
    
    	var date_id = 'fld-e2d20da877cb4a6c8fd72153b86f1ab1';
    	var donation_id = 'fld-05c429d6a4484061b554aa584a45f8fc';
    	var total_id = 'fld-7c6b9bb288ab47e0b295af6eacc9cd26';
    	
    	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(date_id);
    	if (date.getMonth() == 0 && date.getDate() == 1){
    		return 0;
    	}
    	
    	var today = record.getFieldValue(donation_id);
    	var previous_total = previousRecord.getFieldValue(total_id);
    	var total = today + previous_total;
    	
    	console.log("Today: " + today)
    	console.log("Previous: " + previous_total)
    	console.log("Total: " + total)
    	
    	return total;
    }
    
    Season_to_date();
    #48932
    Glen Forister
    Participant

    I did have a record that had no data, date or values. Deleted that. Also made sure my added records for Jan 1 had the value = 0 for donations (that field was empty).
    Now the script runs without errors with the original lines, or those lines replaced with the above correction for undefined.

    But, all my records have nothing in the Totals value. All blank.

    I checked the script window and the value = number. Also the field Total is calculated and that value = decimal with zero decimal places.

    Stopped the program, loaded it again and brought up the Form and refreshed it many times and no totals show up.
    Is there a way to do this?
    Thanks. Code below just so you know what I’m working with.

    
    
    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 != undefined && date.getMonth() == 0 && date.getDate() == 1){
    	}
    //	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();
    

    I just ran it again to verify and I get using one statement:
    2/16/23, 9:26:49 AM / Donations / Yearly
    Yearly: TypeError: undefined is not an object (evaluating ‘date.getMonth’), line:(null)

    The other statement gives me:
    2/16/23, 9:29:25 AM / Donations / Yearly
    Today: undefined
    Previous: undefined
    Total: NaN

    I’m really confused. Don’t know what is going on.
    Here is my file.
    I would like to see my donations for any single year that I can see with a search view.
    I have a script in the Total Field and a similar one (maybe different now) in the Script designer field.

    Attachments:
    You must be logged in to view attached files.
    #48925
    Daniel Leu
    Participant

    You still need to reset the year-to-date field at the beginning of the year.

    	if (date.getMonth() == 9 && date.getDate() == 1){
    		return 0;
    	}

    Needs to be

    	if (date.getMonth() == 0 && date.getDate() == 1){
    		return 0;
    	}

    The function should look like this:

    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();

    And again, this only works if there is a record for January 1st.

Viewing 15 results - 16 through 30 (of 141 total)
 
Apple, the Apple logo, iPad, iPhone, and iPod touch are trademarks of Apple Inc., registered in the U.S. and other countries. App Store is a service mark of Apple Inc.