Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Convert 2 fields
- This topic has 15 replies, 3 voices, and was last updated 1 year, 1 month ago by Glen Forister.
-
AuthorPosts
-
September 12, 2023 at 6:17 PM #49829
Glen ForisterParticipantI have 2 fields “feet” and “meters” which relate to the elevation of a location of a record.
I often have feet, or meters but not both. I want a script (I assume to put in the “Script” tab of that form to check if a record has one of the fields not empty, then to convert to the other field. If both fields are not empty, leave it alone.
I asked ChatGPT and it gave me this to do a simple conversion to see if I could get started and I pasted in between the Function Names.
My first stumble was I couldn’t find the command in the directory of Script commands in the first statement “form.getSelectedRecords()[0]”. Do I insert something in the ()? I asked in the other forum, but couldn’t get any script help there. I think I understand how to put in the “sourceField” and the “targetField”.
function Convert_Elev() {
// Get the current record
var record = form.getSelectedRecords()[0];// Replace “sourceField” and “targetField” with the actual field names in your form
var sourceValue = record.getFieldValue(“sourceField”);// Perform the conversion (e.g., convert from inches to centimeters)
var conversionFactor = 2.54; // 1 foot = 0.3048 meters
var targetValue = sourceValue * conversionFactor;// Update the “targetField” with the result
record.setFieldValue(“targetField”, targetValue);// Save the record to apply the changes
record.save();}
Convert_Elev();
Convert_Elev() {
// Get the current record
var record = form.getSelectedRecords()[0];// Replace “sourceField” and “targetField” with the actual field names in your form
var sourceValue = record.getFieldValue(“sourceField”);// Perform the conversion (e.g., convert from inches to centimeters)
var conversionFactor = 2.54; // 1 foot = 0.3048 meters
var targetValue = sourceValue * conversionFactor;// Update the “targetField” with the result
record.setFieldValue(“targetField”, targetValue);// Save the record to apply the changes
record.save();}
Convert_Elev();
September 14, 2023 at 10:27 AM #49839
BrendanKeymasterThere’s no function
getSelectedRecords()
. There is aform.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 useform.saveAllChanges()
instead.September 15, 2023 at 9:32 AM #49842
Glen ForisterParticipantOK, I get the following error. Do you see what is wrong?
Also, will this be active on all records when activated? If a record field has “0” in it that be changed according to the second field or do I need to put in an IF statement to say ignore the zero? All my fields have a real number or a zero.
9/15/23, 9:26:42 AM / Location_Info / Convert elev
Convert elev: SyntaxError: Unexpected EOF, line:6function Convert_Elev() { // Get the current record // Replace "sourceField" and "targetField" with the actual field names in your form var sourceValue = record.getFieldValue("var ft_id = 'fld-50c724b718da4b6dbcb64556bcec55d9'; "); // Perform the conversion (e.g., convert from inches to centimeters) var conversionFactor = 2.54; // 1 foot = 0.3048 meters var targetValue = sourceValue * conversionFactor; // Update the "targetField" with the result record.setFieldValue("var meter_id = 'fld-21efc0b907ac4d14871fcb9cccb22e2b'; ", targetValue); // Save the record to apply the changes form.saveAllChanges() ; } Convert_Elev();
- This reply was modified 1 year, 2 months ago by Brendan. Reason: Edited to format the code
September 15, 2023 at 10:25 AM #49843
BrendanKeymasterThis isn’t right:
var sourceValue = record.getFieldValue(“var ft_id = ‘fld-50c724b718da4b6dbcb64556bcec55d9’; “);
you need to separate those out:
var ft_id = 'fld-50c724b718da4b6dbcb64556bcec55d9'; var sourceValue = record.getFieldValue(ft_id);
And in your other part too.
You’re mixing separate line statements into the same line.
September 15, 2023 at 10:27 AM #49846
BrendanKeymasterFYI, if you put back tick characters around your code, the forum will format it for you. I edited your response to show you how much better it looks formatted as code instead of just pasted into the response. It’s easier to read that way.
September 15, 2023 at 10:29 AM #49848
BrendanKeymasterYou will need to check to see if the target field is empty or not before doing the conversion. And it will be executed every time you make a change as long as this code is a Field script and not a Form script. A Field script is one that’s added as a regular field to your form, but can contain script code. A Form script is one you add to the Scripts tab on the Form inspector panel.
September 15, 2023 at 6:22 PM #49849
Glen ForisterParticipantOk, my Ft filed is a Number record.
My meter filed is a Script field.It doesn’t do anything. At least there isn’t a error. I’m not checking the field for being empty because that is beyond me. Trying to do one step at a time. (in a test bed Form).
This is the code.
function Meter() { // Get the current record // Replace "sourceField" and "targetField" with the actual field names in your form var ft_id = 'fld-50c724b718da4b6dbcb64556bcec55d9'; var sourceValue = record.getFieldValue(ft_id); // Perform the conversion (e.g., convert from inches to centimeters) var conversionFactor = 2.54; // 1 foot = 0.3048 meters; var targetValue = sourceValue * conversionFactor; // Update the "targetField" with the result var meter_id = 'fld-21efc0b907ac4d14871fcb9cccb22e2b'; var FieldValue = record.getFieldValue(meter_id); // Save the record to apply the changes form.saveAllChanges() ; } Meter();
September 18, 2023 at 2:29 PM #49868
BrendanKeymasterNothing is happening because you’re not setting the value in the end. You’re getting the value.
Your comment is correct. Your code is not:
// Update the "targetField" with the result var meter_id = 'fld-21efc0b907ac4d14871fcb9cccb22e2b'; var FieldValue = record.getFieldValue(meter_id);
You’ll want something like:
// Update the "targetField" with the result var meter_id = 'fld-21efc0b907ac4d14871fcb9cccb22e2b'; record.setFieldValue(meter_id; targetValue);
- This reply was modified 1 year, 1 month ago by Brendan.
September 18, 2023 at 2:46 PM #49871
Glen ForisterParticipantThat just gives me the following error. Code below I added the line numbers so you can tell where the error is.
9/18/23, 2:43:02 PM / Location_Info / Meter
Meter: SyntaxError: Unexpected token ‘;’. Expected ‘)’ to end an argument list., line:16\14. // Update the “targetField” with the result
15. var meter_id = ‘fld-21efc0b907ac4d14871fcb9cccb22e2b’;
16. record.setFieldValue(meter_id; targetValue);September 19, 2023 at 9:25 AM #49875
BrendanKeymasterSorry, my mistake. The parameters in
setFieldValue
should be comma separated. Not semi-colon separated. For some reason I had the calculation field syntax stuck in my head at the time.But also take a look at the error message and it does tell you what the problem is. Something was up with the semi-colon character. Although it’s not smart enough to tell you it should be a comma.
September 19, 2023 at 9:59 AM #49878
Glen ForisterParticipantOK, that does make it work. I see now that those were parameters, so a comma was needed.
Thanks.
I assume though, that making it work both ways, with some records getting the value of ft to be converted to meters, and other records being given the Meters to be converted to Ft. would not work because apparently one has to be a number field and the other has to be a Script field.
Is that correct? It would be nice if the calc went both ways because I sometimes know one value and other times I know the other.
September 19, 2023 at 11:48 PM #49880
BrendanKeymasterWell, instead of the Script field actually returning a value, you can just have it set the value on the field, depending on what field you entered data into. The script will execute when you change a value in a field that’s reference in the
getFieldValue()
function. You can even hide the Script field from your form and it will still execute when the fields you reference are updated.September 20, 2023 at 9:10 AM #49885
Glen ForisterParticipantYes, I would like to do that. I’m wondering though why create a new field to house the script instead of putting the script in the list of Scripts in the Scripts Tab. Just a curious question.
But, the Sctipt would be completely different that the above, so can you give me what I need. Sorry I can’t do it and with the above, I’ve proven that the ChatGPT version isn’t great if you don’t already know Scripting and can debug it.
Thanks.
September 28, 2023 at 12:37 AM #49930
Michael TuckerParticipantYou can do it without scripts, I’ve attached a sample file.
Attachments:
You must be logged in to view attached files.September 28, 2023 at 1:33 PM #49936
BrendanKeymasterThere’s two places for scripts. Either a Script Field, or a Form Script. Both have their uses.
Script fields will get triggered to run whenever a value that the script field references is modified. Form scripts require you to manually run them.
Each have their uses. If you want something to happen whenever you change a value, use a Script Field. You don’t have to even display it as you can hide it. it will work silently behind the scenes.
If you had some sort of bulk process you’d like to do on all your records that you want to run manually, then use a Form Script.
September 28, 2023 at 5:57 PM #49938
Glen ForisterParticipantThanks, I’ll give that a try sometime. Really busy now.
-
AuthorPosts
You must be logged in to reply to this topic.