-2

I want to get the last data created on my database, more specifically the field called: "Saldo Atual" and after this get the form data field "Valor" and make a sum between this fields (the Old one "Saldo Atual" and the new "Valor") before create a new record and add this "Valor" and the sum of "Saldo Atual"+"Valor".

I don't know how to get the database last record

var saldoAtual = 

record.saldoAtual = saldoAtual + record.valor;

And I want to know if the second line it's correct to save the sum of "Saldo Atual"+"Valor"

halfelf
  • 9,737
  • 13
  • 54
  • 63

1 Answers1

0

Based on my comment, I think that your records will need to have a Date_Created field. Then in your onBeforeCreate event the following script should work (this is untested, so you need to do your own testing):

var query = app.models.YourModelTheSameAsYourEventModel.newQuery();
query.sorting.Date_Created._descending();
query.limit = 1;
var lastrecord = query.run();

record.saldoAtual = lastrecord.saldoAtual + record.valor; //You may need to use lastrecord[0].saldoAtual

record.Created_Date = new Date();

Try that and do some testing to see if this yields your desired output.

Markus Malessa
  • 1,841
  • 2
  • 9
  • 11
  • I tryed to do this on my appmaker and is getting this error: TypeError: Cannot read property "modelMovCaixa" from undefined. at models.modelMovCaixa.onBeforeCreateEvent:1 Creating new record: (Error) : Cannot read property "modelMovCaixa" from undefined. at models.modelMovCaixa.onBeforeCreateEvent:1 at novaMovimentacao.Content.Form2.Form2Footer.Form2SubmitButton.onClick:1:19 Creating new record failed. – Vinicius Belegi May 10 '19 at 20:08
  • @ViniciusBelegi Did you end up figuring out what was causing the error? – Markus Malessa May 13 '19 at 13:21