Retuning Values to Local Storage

I have a table with invoice numbers. These are stored for multiple users.

I have developed code that generates a new Invoice Number and it updates the Invoice Table correctly.

The problem is that I can't send the new invoice number back to local storage. I have attempted to do this with the last line which doesn't work. (see below)

Any suggestions?

Mike

select * from invnbr2 where Userno = {{localStorage.values.Userno}};

SELECT @invnbrnext:=max(invnbr) FROM invnbr2;

SELECT @invnbrnextp:=@invnbrnext + 1;

INSERT INTO invnbr2 (Userno, invnbr) VALUES ({{localStorage.values.Userno}}, @invnbrnextp);

localStorage.setValue ("minvnumber", @invnbrnextp);

It depends on the database.

If you're using RetoolDB you add a RETURNING clause into your insert. I can't link directly to it but search for that word here. PostgreSQL: Documentation: 16: INSERT

Then you'd just reference yourQuery.data for the value.

i think @kschirrmacher means something like this:

SELECT @invnbrnext:=max(invnbr) FROM invnbr2;

SELECT @invnbrnextp:=@invnbrnext + 1;

INSERT INTO invnbr2 (Userno, invnbr) VALUES ({{localStorage.values.Userno}}, @invnbrnextp);

RETURNING *

then you can add a Success event handler:
image
I don't know what your query will return so i just used a placeholder. you'll need to change myQuery and mostlikely the .invnbrnextp also

I am not sure about the code in the Success handler.

I am attaching a screen capture. This doesn't seem to work.

Mike
pict13

I have worked a little further with this.

I have a particular variable as circled in the first screen capture. This value saves to the table just fine.

The second capture shows the script in the Success attempting to put this same value in local storage and this part does not work. If I replace the circled value with say 55, this value goes to local storage just fine. The problem seems to be that the circled value does not retain as I go to the Success section.

Any thoughts?

Mike

PICT3

You still need to implement the returning clause, as outlined above, in the query in order for it to return the data that you want.

Then you'll use nameOfYourQueryHere.data.invnbr[0] as your value when you set your localStorage value.

Screenshot 2024-02-13 at 3.26.31 PM

is MYQUERY what you named the query you've added this Success event handler to? If so, you'd actually want to use MYQUERY.data.invnbrnextp, but if that doesn't work could you attach a screenshot of the State for MYQUERY? to get there:
image

after you click 'View State' you should see something like this
image

if the data property is undefined for you like it is in this pic, just run MYQUERY once and it'll populate to looks something like:
image

the yellow warning squiggly line is likely saying something like: value undefined in MYQUERY. it's saying the object named MYQUERY doesn't have a property named invbrnextp. which it's right. MYQUERY does have a property named data though which is also an object and this one MIGHT have the property your looking for.

{
  MYQUERY: {
    data: {
      invnbrnextp: your_value,
    }
  }
}

OK, let me show you where I am now.

The first screen shot shown the initial query which has the RETURNING * clause. This generates an error message as shown.

The second shows the SUCCESS code.

The third screen shows the query that is in the Success Screen.

None of this works, but hopefully I am getting closer.

Mike
Screen 1

Screen 2
pict2

Screen 3

Remove the ; between the insert values and the returning *.

Yes, removing the ; at the end of the second last line eliminates the error message and the table updates properly.

But nothing happens in the Success Event which is supposed to return the minvnumber

value back to localStorage.

Mike

Try getinvnbr3.data.invnbr[0] as the value. The variable that you're setting is not available in the response so referencing it in the success handler won't work.

The value that goes to localStorage is the text "getinvnbr3.data.invnbr[0]", the result of the query does not get sent.

Mike

Do you have it in brackets? {{getinvnbr3.data.invnbr[0]}}

If I add brackets, I get an error message as below.

If I run it anyway, nothing is returned to localStorage.

Mike
pict1