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 - 751 through 765 (of 3,053 total)
  • Author
    Search Results
  • #48654

    In reply to: Learning JavaScript

    Glen Forister
    Participant

    That loop script doesn’t do anything either.

    #48652

    In reply to: Learning JavaScript

    Glen Forister
    Participant

    I’m recording daily rain fall (morning, noon and night)I want each record to show the total for that day (done) and add that to the previous season total to get the new season total.
    each record has the total for the day and the season total

    I added the Script to the list of Scripts, then noticed the change of field properties to Script instead of Calculated and put the script there, but nothing happened.

    Am I supposed to change the script to enter the field somewhere?

    Why a loop?

    • This reply was modified 3 years, 3 months ago by Glen Forister.
    #48651

    In reply to: Learning JavaScript

    Glen Forister
    Participant

    I’m recording daily rain fall (morning, noon and night)I want each record to show the total for that day (done) and add that to the previous season total to get the new season total.
    each record has the total for the day and the season total

    I added the Script to the list of Scripts, then noticed the change of field properties to Script instead of Calculated and put the script there, but nothing happened.

    Am I supposed to change the script to enter the field somewhere?

    #48650

    In reply to: Learning JavaScript

    Brendan
    Keymaster

    Thanks for posting your script Daniel. I had a couple syntax errors in mine which I updated to fix.

    I wonder if Glen wants a cumulative total and not just a total from the previous and today’s totals? If that’s the case, then maybe a Form level script to loop through all the records and update a cumulative total field would be required?

    #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

    #48646

    In reply to: Learning JavaScript

    Brendan
    Keymaster

    Hi Glen,

    You put that script code into a Calculation field.

    You need to put it into a Script field, not a Calculation field.

    #48644

    In reply to: Learning JavaScript

    Glen Forister
    Participant

    Thanks for the script, but it doesn’t do anything, I suppose be cause I have to define where to get the previous record and then add today’s total to it. I could probably spend the rest of the day working on this and not figure it out I’m sure.

    I’ve attached my file and maybe if you fix it I can apply the example to other similar forms I have to import data for.
    Thanks.

    Attachments:
    You must be logged in to view attached files.
    #48637
    Mike Guilbault
    Participant

    I’m trying to extract the phone number from a linked form.
    The current form is called Appointments and the field is simply called “Phone”
    The linked form is called “INSTALLS” and the field I want to draw from is called “Best Phone”

    This is the error I get when I run the script:
    2023-01-13, 8:27:29 PM / Appointments / Phone
    Phone: SyntaxError: Invalid character ‘\u2018’, line:3

    and this is the Script:

    function Phone_Script() {
    
    	var Phone_id = ‘’fld-1a146d1f63aa43c0881af16b1e65f1be;
    	var Best_Phone = record.getFieldValue('fld-eef8580ecdb744bf94a58eb1b7997a89');
    	if (Best_Phone != undefined) {
    		var Phone = Best_Phone.getFieldValue(Phone_id);
    	
    		return Phone;
    	} else {
    		return "";
    	}
    }
    
    Phone_Script();

    Any suggestions?

    • This topic was modified 3 years, 3 months ago by Brendan.
    #48636

    In reply to: Learning JavaScript

    Brendan
    Keymaster

    There’s no function in the Calculation field to reference the previous record. Calculations work only on the current record.

    You would probably have to use JavaScript to do that. But then, what really is the previous record? It really depends on how you sort your records.

    #48630

    In reply to: Learning JavaScript

    Brendan
    Keymaster

    Hi Glen,

    Instead of JavaScript, you could also just use a Calculation field.

    In your previous JavaScript question, the solution to get the difference between two dates in months is this formula:

    MONTHS(Start Date; End Date)

    and that’s it!

    It’s not required to learn to program in JavaScript just to do simple things like that. That’s what the Calculation field is for. There’s lots of functions that’ll do all sorts of things for you without programming. You just have to know simple mathematical expressions.

    Thanks,

    Brendan

    #48624

    In reply to: add & subtract

    Brendan
    Keymaster

    Hi Glen,

    You would not use the square brackets around [today] and [purchased].

    In JavaScript there’s a getMonth() function you can use on dates.

    So your formula would be:

    var duration = today.getMonth() - purchased.getMonth();

    FYI, NaN stands for Not a Number

    #48614

    In reply to: Learning JavaScript

    Glen Forister
    Participant

    Disappointed…
    I didn’t know I would have to spend a week or more to learn Javascript to add or subtract two fields. Spent the greater part of a summer recently learning “R”, but now I can barely get started again. Use it or lose it. I don’t want to spend a week to do a simple calc to get a form to do something and then a month or year late have to go through all that again. This isn’t simple…

    #48604

    In reply to: Number Formatting

    Matthew Carpenter
    Participant

    I’m learning!

    Okay so I took the old code and tweaked it to fit another line of data. The format does not need to change because it is simply a Student ID Number. I got it to spit out the same number but as a script. The scripted output is now my form and cannot be edited from the custom form.

    This is what I did;

    function formatStuID(val){
    
    	val = val.replace(/(\d)-(\d{4}).*/, '$1-$2');
    	return val;
    }  		
    
    function Formatted_Sin() {
    
    	var sin = 
    record.getFieldValue('fld-66039384e7ac46f0aecb546f5e8c4ac9');
    	console.log(sin);
    	
    	if (sin != undefined) {
    		return formatStuID(String(sin));//"1234567890"
    	} else {
    		return nil;
    	}
    }
    
    Formatted_Sin();

    BAM!!! Protected form value!

    #48592
    Brendan
    Keymaster

    Hi Matthew,

    There’s no function to lock a field on a custom form. You can lock the position of a field so you don’t accidentally move it. But that’s not used for preventing editing on a custom layout. But what you can do is just not include that field on your custom layout and it will still be available on the Default Layout. With custom layouts you don’t have to have every field available displayed there. Not even for script or calculation fields to work. They can be on your custom layout even without any of the fields they reference. And you can create as many custom layouts as you like.

    Thanks,

    Brendan

    #48589
    Matthew Carpenter
    Participant

    So I am new (as you may know).
    I went onto a coding adventure to output a value in a desired format.

    When I add a new record there are some values that are required for each record.
    Optimally I do not want those to ever be accidentally changed. So far being new to TapForms I have had to be very carful when using a copy/paste from MY form so I don’t change a value. When I enter a new record I use the default layout but when I dig in and use my data I use MY custom form.

    I do not know if there is a way to lock a value on a custom form but what I learned is that the output value of a script on MY custom form cannot be altered easily.

    So my silly question is, can we lock a value so it cannot be changed on a custom form but only from the default layout?

Viewing 15 results - 751 through 765 (of 3,053 total)