Need help to translate VFP code to Servoy

Hello everybody.

I´m a newbie and I need help with something that I do with VFP but do not know how to proceedd with Servoy.

Here is the scenario:
I have a table named Counters, it just have 2 fields CounterName and Counter. I use it to keep track of some counters when registers are added to other tables. For example, the pk for the members table is created with this format: YYBBOOOOOO, where YY are the 2 last digits of the date, BB is the branch where the record is created and OOOOOO is a sequential number for the YYBB.

I need that everytime a new record is going to be added the pk for the members table be created so I think that I must place some code in the method where the new record is created.

I use to do this with VFP this way:

! First we check if the counter name exists.
lcCounterName=RIGHT(TRANSFORM(YEAR(DATE()),2) + oApp.cBranch
IF NOT SEEK(lcCounterName,‘counters’,‘countername’)
! Does not exist - > create it
INSERT INTO counter (countername) VALUES (lcCounterName)
ENDIF
! The current record in counters is the one that I need, because it was found or just created.
! Now I add 1 to the counter
REPLACE counters.counter WITH counters.counter + 1 in counters
lcPK=lcCounterName + PADL(TRANSFORM(counters.counter),6,‘0’)
RETURN lcPk

Any help about how to do this with Servoy?

Thanks in advance

Hi Juan,

var _counterName = application.getServerTimeStamp().getDay() + _brancheC
var _fs = databaseManager.getFoundSet(serverName,'counters)
_fs.find()
_fs.countername = _counterName
var _res = _fs.search()
if (! _res) {
_fs.newRecord()
_fs.countername = _counterName
_fs.counter = 0
}
_fs.counter++
return _counterName + yourOwnPadLeft(_fs.counter,6,‘0’)

Note 1 : You must write your own padl function. It does not exist in JavaScript.
Note 2 : It is better multi-user to lock the counter record before reading and writing the counter value (and unlock afterwards of course)

Regards,

Hi Lambert thank yhou very much

The big problem is that I did not know how to search in a table that is not the master of the form. Anyway in your code I do not see where the record is saved, I that you create the record, stores the data but where do you save it?

Thanks.

Hi Juan,

Oops, sorry. I forgot the save.

The best way is :

databaseManager.saveData(_fs.getRecord(_fs.getSelectedIndex()))

This way you only save the current record and you don’t touch other data…

Maybe I can explain it better sitting in the Las Palmas sun for a week of two :lol: :wink:

Regards,

Thank you. Sure you´d be welcome.

Hi Juan,

You could accomplish your padl functiontion with:

utils.numberFormat(counters.counter, ‘000000’)

Good Luck,
Lach