I’m trying to assign a numerical value to each record in a jobs database so that a particular job can be referenced by its job number. I’ve modified some code that I found which Yisrael had submitted to the Forum. Unfortunately, when I input some test jobs, all submitted records show a value of 0. If someone would take a look at my modified code and show me where I’ve erred, I would be greatly appreciative.
import wixData from “wix-data”; // you’ll need this too
export function Jobs_beforeInsert(item, context) {
return wixData.query(“Jobs”)
.limit(1)
.descending(‘reference’) // sorts the results in descending order by ‘reference’
.find()
.then((results) => {
if(results.length === 0) {
item.reference = 1; // start at 1 for an empty collection
}
else {
const lastItemInCollection = results.items[0]; // get item with last reference number
item.reference = lastItemInCollection.reference + 1; // increment last reference number
}
return item; // beforeInsert function returns the modified item (record) to be saved
});
}