Creating new records with data in code

Hiya,

I’m looking to add data to my database through code and cant figure out how to do it. I have a bunch of strings that all need to be added to the database under their own individual records.

Here’s a basic version of my current code:

var vfoundset = databaseManager.getFoundSet(server, table)
var vnewRecord = vfoundset.newRecord()
vnewRecord[databaseColumn] = 'string'

I thought this would work but haven’t had any luck yet and all the records are just empty. Anyone have any insights into this?

Thanks,
Alasdair

Hello,
Here its a little example from the tooltip.

var ridx = fs.newRecord()
var record = fs.getRecord(ridx)
record.emp_name = ‘John’
databaseManager.saveData()

Please try this way.

If you want to have autocomplete you can specify the table you have like this
/** @type {JSFoundsetdb:/server1/table1} */
var vfoundset = databaseManager.getFoundSet(server, table)

and then on vfoundset you will have the autocomplete from the table1, since it will know all the columns etc.

Hi Alasdair,

vfoundset.newRecord() does not return the newly created record, just the index of it.
You might want to try:
var vnewRecord = vfoundset.getRecord(vfoundset.newRecord())

Also add:
databaseManager.saveData(vnewRecord)
at the end to save your record, or without a parameter to save all outstanding changes.

In recent versions you can also use

var _rec = foundset.createRecord()
if(_rec) {
   _rec[databaseColumn] = 'string'
}

This will return the record right away.
This function has been added to the Servoy API a while back.

Thank you all! Have been thinking it was something like that but have just been drawing a blank for a while.