Repeatable items appear in the order they were added. You can use a Data Event script to reorder them automatically based on the value of a field inside the repeatable, such as an item number. This article shows how to add the script to your app and what to do if the new order does not appear right away. The script works the same way in the Fulcrum web app and the Fulcrum mobile app.
Prerequisites
The data name of the repeatable section (for example,
rooms).The data name of the field inside the repeatable that you want to sort by (for example,
item_number).Access to edit the app in the App Designer.
he data names in the script must match the data names in your app exactly. If they do not match, the script will not sort the items.
Fields inside a repeatable section do not appear in the Form Fields list in the Data Events editor. To find a field's data name, open the field's settings in the App Designer.
Adding the Sorting Script
On the Fulcrum homepage, select edit app next to your app's name to open the App Designer.
In the Settings section on the right side of the App Designer, select Data Events.
Paste the following script into the workspace of the Data Events editor. Replace
roomsanditem_numberwith the data names from your app.
function rawSetValue(dataname, value) {
const field_key = FIELD(dataname).key;
CONFIG().results.push({ type: 'set-value', key: field_key, value: JSON.stringify(value) });
}
function sortRepeatableItems() {
const key = FIELD('item_number').key;
const original = ($rooms || []).slice();
const getNum = (item) => String((item.form_values && item.form_values[key]) || '').trim();
const sorted = original.slice().sort((a, b) => {
const A = getNum(a);
const B = getNum(b);
if (!A && !B) return 0;
if (!A) return 1; // empty numbers last
if (!B) return -1;
return A.localeCompare(B, 'en', { numeric: true });
});
const changed = sorted.some((item, i) => item.id !== original[i].id);
if (changed) rawSetValue('rooms', sorted);
}
ON('change', 'rooms', sortRepeatableItems);
ON('change', 'item_number', sortRepeatableItems);
Select Save in the Data Events editor. If the script is valid, the button in the App Designer changes to Data Events (Enabled). If the script is not valid, an alert appears and you need to fix the code before you can save.
At the bottom of the App Designer, select Save app.
Open a record and add or edit items in the repeatable to see the new order.
What the script does
Reads the value of the sort field for each item in the repeatable.
Places items with an empty value last.
Compares the remaining values using a locale-aware, numeric comparison, so
2comes before10.Updates the repeatable only when the order changes.
Runs whenever the repeatable or the sort field changes.
Expected outcome: When you add an item or change the sort field value, the repeatable items reorder automatically.
Troubleshooting
The updated sorting does not appear
After you add or update the script, the new order may not appear right away. Use one of the following options to reload the record and run the updated script:
Go back to the parent record, and then return to the repeatable section.
Close the record, and then reopen it.
On the mobile app, sync the app to receive the updated app design before you open the record.
The script does not run
Confirm that the Data Events button shows Data Events (Enabled) and that you selected Save app.
Confirm that the data names in the script match the data names in your app exactly.
Best Practices
Test first: Test the script in a non-production copy of your app before you use it in the live app.
Account for empty values: Keep the empty-value check so that items without a value do not cause unexpected ordering.
Keep scripts lightweight: Data events run on the device while the record is being edited, so keep your logic simple.
Notes
The sort is ascending. To change the locale used for the comparison, replace
'en'in thelocaleComparecall with the locale that matches your data.This script is based on the Repeatable record sorting example in the Fulcrum developer documentation.
