Saving new record not returning Primary Key value

Forum to discuss the Web client version of Servoy.

Saving new record not returning Primary Key value

Postby ayadav1 » Wed Aug 16, 2017 3:39 pm

I have been trying to insert a new record in my database.
My code is as follows:
// Create new record
var idx = forms[formName].foundset.newRecord();
var record = forms[formName].foundset.getRecord(idx);
// Get record from where to copy data
var fsProfile = databaseManager.getFoundSet( "DatabaseName", tableName )
var whrString = "'"+whereKey+"'"
var _queryProfile ='Select "'+pkId+'" from "'+tableName+'" where "'+pkId+'" ='+whrString
application.output('Select "'+pkId+'" from "'+tableName+'" where "'+pkId+'" ='+whrString)
var _argsProfile = new Array()
fsProfile.loadRecords(_queryProfile,_argsProfile)

var dprovider = ''
for(var i = 0; i < fsProfile.alldataproviders.length; i++) {
dprovider = fsProfile.alldataproviders[i]
// keyDataProvider is my primary key
if(dprovider != keyDataProvider) {
record.foundset.setDataProviderValue(dprovider,fsProfile[dprovider])
}
}


Now when I check my record in the database, all values are getting updated as expected but when I try to retrieve the primary key value by using

returnVal = record.foundset.getDataProviderValue(keyDataProvider);

OR

returnVal = record[keyDataProvider]

it is returning <null>, even though the value is set in the new record (verified by checking the database).
I even tried using

record = forms[frmObject].foundset.getRecord(idx);

before retrieving the primary key value so as to refresh the contents of 'record' but still no luck.
Any workaround for this?
ayadav1
 
Posts: 17
Joined: Thu Oct 20, 2016 2:40 pm

Re: Saving new record not returning Primary Key value

Postby Peter de Groot » Thu Aug 17, 2017 12:23 pm

hi,

Maybe this will work,

Code: Select all
var record = forms[formName].foundset.getRecord(forms[formName].foundset.newRecord());


No need to get the record
record = forms[frmObject].foundset.getRecord(idx);


Regards
Servoy 8.1.3

NEM Energy B.V.
Peter de Groot
 
Posts: 210
Joined: Thu Jan 10, 2008 8:38 pm
Location: Not sure...

Re: Saving new record not returning Primary Key value

Postby ayadav1 » Mon Aug 21, 2017 7:28 am

Thanks, but I have already tried this. The problem is that the contents of 'record' variable are not getting updated with the values which are not explicitly saved but are generated using triggers. So my database has all the correct values saved in it but the variable 'record' doesn't get updated with those values.
ayadav1
 
Posts: 17
Joined: Thu Oct 20, 2016 2:40 pm

Re: Saving new record not returning Primary Key value

Postby rafig » Mon Aug 21, 2017 9:00 am

Hi
try refreshing the record after the save
Code: Select all
            var saved_rec = databaseManager.saveData(record);
            if (saved_rec) {
               databaseManager.refreshRecordFromDatabase(record, 1);
            } else {
               log_message('Failed to Save Record', 'ERROR');
            }

this should give you the saved PK.

Rafi
Servoy Certified Developer
Image
rafig
 
Posts: 483
Joined: Mon Dec 22, 2003 12:58 pm
Location: Watford, UK

Re: Saving new record not returning Primary Key value

Postby ayadav1 » Mon Aug 21, 2017 12:24 pm

Thanks! This worked :)


EDIT: I cross checked the Primary Key value from my table. This code is actually returning the primary key of the parent record which I am using for duplication instead of the primary key of the duplicated record
Last edited by ayadav1 on Mon Aug 21, 2017 2:47 pm, edited 1 time in total.
ayadav1
 
Posts: 17
Joined: Thu Oct 20, 2016 2:40 pm

Re: Saving new record not returning Primary Key value

Postby rafig » Mon Aug 21, 2017 12:59 pm

ayadav1 wrote:Thanks! This worked :)

Glad to help ;-)
[It took me a little while recently to sort this out for my own solution...]
Servoy Certified Developer
Image
rafig
 
Posts: 483
Joined: Mon Dec 22, 2003 12:58 pm
Location: Watford, UK

Re: Saving new record not returning Primary Key value

Postby ayadav1 » Mon Aug 21, 2017 2:50 pm

I cross checked the Primary Key value from my table. This code is actually returning the primary key of the parent record which I am using for duplication instead of the primary key of the duplicated record


What I am doing is :

var saved_rec = databaseManager.saveData(record);
if (saved_rec) {
databaseManager.refreshRecordFromDatabase(record.foundset, 1);
} else {
application.output('Failed to Save Record');
}
var retrunVal = record.foundset.getDataProviderValue(keyDataProvider)


If I write
databaseManager.refreshRecordFromDatabase(record, 1);

or

databaseManager.refreshRecordFromDatabase(record, -1);

the record is still not refreshed and returns the primary key as null
ayadav1
 
Posts: 17
Joined: Thu Oct 20, 2016 2:40 pm

Re: Saving new record not returning Primary Key value

Postby rafig » Mon Aug 21, 2017 4:18 pm

Hi,
unless you really have to, don't do things using 'forms', try to do it in memory as it's easier & more efficient
so I am doing something like this
Code: Select all
   /** @type {JSFoundSet<db:/dbserver/mytable>} */
   var fs = databaseManager.getFoundSet('db:/dbserver/mytable');
   /** @type {JSRecord<db:/dbserver/mytable>} */
   var record1;
   /** @type {JSRecord<db:/dbserver/mytable>} */
   var record2;
      // create 1st record
        record1 = fs.getRecord(fs.newRecord());
   var saved_rec = databaseManager.saveData(record1);
   if (saved_rec) {
      databaseManager.refreshRecordFromDatabase(fs, 1); // as we created default new record, it will be 1 in foundset
   } else {
      application.output ('Failed to Save');
   }
      // now do what you need to set up record2
     // then you can access record 1 PK using
     record2.fk = record1.pk ;

or something like that, but hope that gives you an idea
Servoy Certified Developer
Image
rafig
 
Posts: 483
Joined: Mon Dec 22, 2003 12:58 pm
Location: Watford, UK

Re: Saving new record not returning Primary Key value

Postby ayadav1 » Tue Aug 22, 2017 9:55 am

Now my code looks something like this::

var fs = databaseManager.getFoundSet('db:/DatabaseName/'+ tableName);
var record = fs.getRecord(fs.newRecord());

// Get record from where to copy data
var fsProfile = databaseManager.getFoundSet( "DatabaseName", tableName )
var whrString = "'"+whereKey+"'"
var _queryProfile ='Select "'+pkId+'" from "'+tableName+'" where "'+pkId+'" ='+whrString
application.output('Select "'+pkId+'" from "'+tableName+'" where "'+pkId+'" ='+whrString)
var _argsProfile = new Array()
fsProfile.loadRecords(_queryProfile,_argsProfile)

var dprovider = ''
for(var i = 0; i < fsProfile.alldataproviders.length; i++) {
dprovider = fsProfile.alldataproviders[i]
// keyDataProvider is my primary key
if(dprovider != keyDataProvider) {
record[dprovider] = fsProfile[dprovider]
}
}
var saved_rec = databaseManager.saveData(record);
if (saved_rec) {
databaseManager.refreshRecordFromDatabase(fs, 1)
} else {
application.output('Failed to Save Record');
}
application.output(record[keyDataProvider])


Still getting <null>
ayadav1
 
Posts: 17
Joined: Thu Oct 20, 2016 2:40 pm

Re: Saving new record not returning Primary Key value

Postby ayadav1 » Thu Aug 24, 2017 7:21 am

I guess the whole issue lies in the fact that when I perform the action databaseManager.refreshRecordFromDatabase(foundset,index), my record in the foundset gets shuffled since there are no fixed indexes of records in the foundset and then when I try to retrieve the contents of my record using the old, original index value I get some other record altogether. If only there was some way to refresh just the record or refresh the foundset while retaining the value of the record's index.
ayadav1
 
Posts: 17
Joined: Thu Oct 20, 2016 2:40 pm


Return to Servoy Web Client

Who is online

Users browsing this forum: No registered users and 3 guests

cron