Creating new records with data in code

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

Creating new records with data in code

Postby alasdairs » Tue Feb 06, 2024 3:28 pm

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
Software Engineer
alasdairs
 
Posts: 45
Joined: Wed Dec 27, 2023 10:06 pm
Location: The Internet

Re: Creating new records with data in code

Postby cmanolescu » Tue Feb 06, 2024 3:35 pm

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.
cmanolescu
 
Posts: 2
Joined: Wed Oct 19, 2022 4:15 pm

Re: Creating new records with data in code

Postby bodnarescu.diana » Tue Feb 06, 2024 3:40 pm

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.
Diana Bodnarescu
Servoy Valued Professional
Freelance Servoy Developer
bodnarescu.diana
 
Posts: 25
Joined: Tue Mar 29, 2016 10:43 am

Re: Creating new records with data in code

Postby mboegem » Tue Feb 06, 2024 3:52 pm

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.
Marc Boegem
Solutiative / JBS Group, Partner
Servoy Specialist
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance Developer

Image
User avatar
mboegem
 
Posts: 1752
Joined: Sun Oct 14, 2007 1:34 pm
Location: Amsterdam

Re: Creating new records with data in code

Postby alasdairs » Tue Feb 06, 2024 3:58 pm

Thank you all! Have been thinking it was something like that but have just been drawing a blank for a while.
Software Engineer
alasdairs
 
Posts: 45
Joined: Wed Dec 27, 2023 10:06 pm
Location: The Internet


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 31 guests

cron