Page 1 of 1

Creating new records with data in code

PostPosted: Tue Feb 06, 2024 3:28 pm
by alasdairs
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:
Code: Select all
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

Re: Creating new records with data in code

PostPosted: Tue Feb 06, 2024 3:35 pm
by cmanolescu
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 {JSFoundset<db:/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.

Re: Creating new records with data in code

PostPosted: Tue Feb 06, 2024 3:40 pm
by bodnarescu.diana
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.

Re: Creating new records with data in code

PostPosted: Tue Feb 06, 2024 3:52 pm
by mboegem
In recent versions you can also use
Code: Select all
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.

Re: Creating new records with data in code

PostPosted: Tue Feb 06, 2024 3:58 pm
by alasdairs
Thank you all! Have been thinking it was something like that but have just been drawing a blank for a while.