This one isn’t too bad to get started on, when ever anyone has told me they want to learn programming my first question is what is the project or problem they want to solve. You’ve already got a small problem to solve which is a great place to start. I will suggest if possible maybe switching over from table to a link to form if you don’t have too much data because I know the link to form works but the table I haven’t used as much. Apart from having an extra form, it can behave more or less identically.
Just to expand on the script, let me go line by line since it’s a relatively small script. This is a script field inside the new form and make sure you set up a Link to Form field from the existing form to your new form and also make sure the Link to Form field has ‘show inverse relationship’ ticked.
var order = record.getFieldValue('fld-c3a6725d7d9446da92bbb880ffe90a9e');
This first line is there to get a reference to the parent record (your current form right now). This is the piece I’m not so certain about how you get in the table field hence the suggestion to use a new form and Link to Form/Link from Form. In my case I have an Order form and then an Order Items form. For your situation you’ll need to change the fld-c3a6725d7d9446da92bbb880ffe90a9e to match the Link From Form field. To do that in the script editor for the script field, double click on the form header in the left panel.
var order_items = order.getFieldValue('fld-9db0c7698499435ab6b18b7eb420e0ae');
This is the Link To Form field and to get the value of this you’ll need to be in the equivalent of your Orders form (your current parent form). If you don’t have an existing script field to use, I generally create a new form script (third tab on the right panel), open up the editor and then get the ID I need. In this case you’re looking for the section heading with the name of your new form, in my case ‘Order Item’ and double click on that to get field ID to splice in above.
In this situation, order_items returns a set of records linked to the parent record via the field. This essentially is all of the sibling records of the currently selected record.
var line_number = record.getFieldValue('fld-f95b68d488cb4b058bbf3de84e1a7c3b');
This is the new field in the new form that stores essentially your offsets. You can get this in the script editor by double clicking on the field name and it should insert this or a similar line with the correct field ID.
if (!line_number)
{
This is a check to see if the line_number is not set on this record already. This just makes sure once it has a value set, it doesn’t overwrite it again.
record.setFieldValue('fld-f95b68d488cb4b058bbf3de84e1a7c3b', order_items.length);
This line sets the value of the line_number field (notice the ID is the same) to the number of records currently linked to the parent (order_items.length). When this is working properly, each time a new record is created, the number of items will be incremented automatically. It’s not 100% perfect but it’s a good enough approximation that doesn’t require scanning all records to find the current maximum and set it to be higher than that.
console.log('Setting line number to ' + order_items.length);
}
I like lots of logging and I put in a log message just to see what is going on and we need to close out the if statement block hence the }.
Hopefully this helps, a little more detail on what this script means and how to make it happen for yourself. Again I recommend switching over to a second form and a Link to Form field because I’m not sure how well this works with table fields.
Good luck! Scripting in Tap Forms unlocks a large number of capabilities and gives you a large amount of power to customise your workflows.