Saving new record not returning Primary Key value

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
// 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 , 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?

hi,

Maybe this will work,

var record = forms[formName].foundset.getRecord(forms[formName].foundset.newRecord());

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

Regards

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.

Hi
try refreshing the record after the save

				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

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

ayadav1:
Thanks! This worked :)

Glad to help ;-)
[It took me a little while recently to sort this out for my own solution…]

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

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

	/** @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

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
// 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

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.