Hello. I would like some scripting help with a database I’m creating.
What I would like is to take the contents of a pick list, compare them with a different list (it can be within the script, or it can be referred from somewhere else, doesn’t matter), and then show the entry that matches the conditions. I’ve created a pick list in the preferences of my database.
To use an example, the pick list has Star Wars Episode 1 to 8. The other list has three entries: Prequels, Original Trilogy, New Trilogy. So if Episode 2 is selected from the pick list, the Script field would show “Prequels”.
Is this possible?
Thought I’d play around and see if I could come up with a different approach to the same mileage tracker. This would probably be closer to a fuel log rather than a simple mileage tracker.
The inputs would be:
– odometer value
– Price per gallon
– Gallons filled
A script would calculate your mileage since last fill up as well as your miles per gallon.
The script uses the default created date field to sort all records by their created date. This assumes that you’re entering in your mileage log around the time you filled up.
Here’s the script for those interested
var odometer_id = 'fld-9674e316bfed408ca6710ce81b72bf05';
var vehicle_id = 'fld-64b0b17a635f49b8b40867e45f8d24db';
var date_created_id = 'fld-2097149a2eeb4d4e892f13b62de6b5ea';
// sort records in ascending order by date value
var dateSorter = (a, b) =>
a.getFieldValue(date_created_id) - b.getFieldValue(date_created_id)
var run = () => {
var vehicle = record.getFieldValue(vehicle_id);
var odometer = record.getFieldValue(odometer_id);
var records = form.getRecords()
.filter(r => r.getFieldValue(vehicle_id) === vehicle)
.sort(odometerSorter)
var odometerValues = records.map(r => r.getFieldValue(odometer_id));
var currentRecordIndex = odometerValues.indexOf(odometer);
var previousRecordIndex = currentRecordIndex === 0 ? 0 : currentRecordIndex - 1
var mileageValue = odometerValues[currentRecordIndex] - odometerValues[previousRecordIndex]
return mileageValue;
}
run();
Attachments:
You must be
logged in to view attached files.
Hi Federico,
The scripts only get executed on the same form they’re referenced from. It happens when you call setFieldValue(field_id, value); I don’t traverse all of the linked fields to execute script fields on them.
if that field being edited is referenced. If so, then it executes that script.
Hi Brendan,
But this does not happen if it is edited from another script right? or at least not one in another form?
right now I was trying to do scripts between 3 forms an independent scoring form, a capture form and a brain form that does what I need it to do. they all work great independently but to see the changes I have to wait until its done and then hit the refresh records for the other scripts to do their things. In my capture form its not a big deal but it is in my scoring form. I am taking advantage of the “link to form join” I can go around this by making more functions on the brain side and duplicating the other scripts. just wanted to make sure this is intended and not a bug.
Thanks!!
It is a many to many. The script returns the correct value but when looking at the console log it gives the error.
After refreshing the record single column list the script field displays the correct result. The console log continues to display the same error.
Thanks for the example. I was able to adapt it to my form and when I run the script it returns the expected result, but it is not displayed in the script field on the layout. The console log returns this error:
s_Doc: TypeError: undefined is not an object (evaluating ‘med_doctor[0].getFieldValue’), column:25, line:3
s_Doc is the name of the script field.
var med_doctor = record.getFieldValue('fld-84c907c7af3c49daa19ca1e30975bc1b');
if(med_doctor){
var Name = med_doctor[0].getFieldValue('fld-b1cfe8adec9b4d7397683b068a262cc2');
Name;
}
med_doctor is the parent form and the var Name receives the correct value from the Name field of med_doctor.
The error does not show in the script edit window when the run button is clicked, it only shows in the console if I click the save button and then open the log.
I have attempted to create a script field to return the value from a parent record without success. Could you please give a sample of your script.
Thanks,
Ron
Hi Deborah,
The reason this doesn’t work the way you would like it to is because a Join Link Type works basically the same way as a Many to Many. That is, you can actually have multiple parent records that all join to the same set of child records using the Join fields.
So that means that a child cannot get access to just one parent record via the Calculation field.
However, this is possible by using a Script field and having Tap Forms get the first parent record from the array of parent records and then picking out the value for the field from the parent record.
Ah yes. The form.getFieldIds() method. I forgot about that.
You’ll see the File Attachment fields in your form appear on the left panel on the Script editor after the next update.
Well, it is still needed it seems. I’m not sure why yet. Because an NSArray in Objective-C is supposed to translate into a JavaScript array. But an NSDictionary just translates to a JavaScript Object, which I guess still needs to be stringified.
I was wondering how to get to the attachment ID…
Following scriptlet shows the filename of the first attachment.
var receipt_attachment_id = 'fld-8bbc0b1472a746cabc38f6d7ab045668';
var receipt = record.getFieldValue(receipt_attachment_id);
JSON.stringify(receipt);
var meta = receipt[0];
if (meta) {
meta.filename
}
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
Hi Guillaume,
I haven’t opened up the File Attachment field to the Script editor yet.
Did you get that field ID by changing the field type to something else and then changing it to File Attachment? Because for me File Attachment fields don’t show up in the list of available fields.
In any case, it should be an array of objects where each object is a dictionary.
I’ll have to do some tests with this though to see how I can open it up for scripting.
Hey there,
I’m trying to get an attachment’s filename as a string and then parse it using regex to populate new records. When trying to get the value of the field:
var attachments = 'fld-e4657ea48c5a4ff6b82ee2a3c43c96a8';
var fileObject = record.getFieldValue(attachments);
console.log(fileObject);
this outputs
[object Object]
How can get to the properties of that object to get the attachment’s file name? If the form I’ll be using, there will only be one file attachment per record.
The purpose of all this is that I’m trying to convert a file base invoice archive where file names were following a convention of type YYYY-MM-DD - vendor - amount.
Thanks!
-Guillaume
Hi Bryan,
You could add a Calculation field or a Script field to your Artwork Details form that returns the artist contact name and displays that. That would work without you having to do all that work.
Thanks,
Brendan