Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

global hashcookie apparently forgotten but then remembered Topic is solved

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
incoherence
Posts: 22
Joined: Sun May 08, 2022 2:36 pm
Has thanked: 9 times
Been thanked: 1 time

global hashcookie apparently forgotten but then remembered

Unread post by incoherence »

Hi,
I have a "browse and edit" form with a subform and, on each row of the subform, I have a button that lets me edit one row of the subform but on a new screen (with the fields laid out vertically with much more space than in the subform).

When you click on a row in the browse screen of the form, the custom code for the form runs the following code, so that we can remember the PK of the record we are editing in the edit form:-

Code: Select all

rememberRecord();
(nuFormType() == 'edit') && console.log("ID of system being edited is stored in global hash cookie 'saveithere': "+nuGetProperty('saveithere'));

function rememberRecord() {
  if(nuFormType() == 'edit') {
    nuSetProperty('saveithere', nuGetProperty('record_id'), true);
  }
}

This part works fine and I get the expected output in the console showing that the global hash cookie has been stored (BTW, the database is pre-populated, so the ID starting with "sys" is normal here - the ID was not generated by nuBuilder):-

ID of system being edited is stored in global hash cookie 'saveithere': sys131b507ac949150



Now, in the subform, I want to create a new subform row (but in the form with more space, rather than in the subform itself), by clicking the button on the very last row of the subform (which is usually blank in the subform with the "delete" checkbox ticked). The button's onclick code is:-

Code: Select all

nuForm('<form_id_hex>', nuSubformRowId(this), '', '', '0');

console.log("Button clicked to edit record in subform with id: " + nuSubformRowId(this) + " for main form with ID: >"+nuGetProperty('record_id')+"<");
if(nuSubformRowId(this) == -1) {
    console.log("nuSubformRowId(this) for this row is -1 (means: new record) and global hash cookie saveithere is: "+nuGetProperty('saveithere'));
}
So this code opens the new form in the UI, but I get the following in the console, which looks like the global hash cookie cannot be retrieved, even though we checked that it could be retrieved earlier! :-

Button clicked to edit record in subform with id: -1 for system with ID: ><
nuSubformRowId(this) for this row is -1 (means: new record) and global hash cookie saveithere is: undefined


The form with ID <form_id_hex> opens in the UI and its Custom Code is:-

Code: Select all

retrieveRememberedHashcookie();

function retrieveRememberedHashcookie() {
  if(nuGetProperty('record_id') == -1) {  // New record_id
    let sysID = nuGetProperty('saveithere');
    nuSetValue('fk_main_form_id', sysID);
    console.log("New record, so setting fk_main_form_id (from global hash cookie saveithere) to: "+sysID); 
  }
}
Again the output indicates that the global hash cookie is not set:-

New record, so setting fk_main_form_id (from global hash cookie saveithere) to: undefined

What's weird is:
1. I used to have this working in a previous version of nuBuilder.
2. If I CTRL-SHIFT-R at this point (=Refresh the form with ID <form_id_hex>), then it "suddenly remembers" the value of the global hash cookie and it works fine! This must be running the exact same code, I assume, yet the outcome is different when it tries again.

That is, when I do 2, I get:-

New record, so setting fk_main_form_id (from global hash cookie saveithere) to: sys131b507ac949150


The version I am seeing this issue in is:-

nuBuilder Forte 4.5

DB Version: V.4.5-2023.01.22.00
Files Version: V.4.5-2023.01.22.00

Any ideas about what is going on or how to debug this, would be appreciated!
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: global hashcookie apparently forgotten but then remembered

Unread post by kev1n »

I used to have this working in a previous version of nuBuilder.
In which version did it work?


Can the problem be simplified and replicated in such a way that you execute this JavaScript in a form 1

Code: Select all

nuSetProperty('testhk','123', true);

and in a form 2 you'd have a display object with the SQL

Code: Select all

SELECT '#testhk#' 
?
incoherence
Posts: 22
Joined: Sun May 08, 2022 2:36 pm
Has thanked: 9 times
Been thanked: 1 time

Re: global hashcookie apparently forgotten but then remembered

Unread post by incoherence »

Hi kev1n, I like your reductionist approach and thanks for your help...

> Can the problem be simplified and replicated..

I think that the answer is "yes" iff I can reproduce the issue, with your suggested code, on the nuBuilder version I had the issue with and I cannot reproduce the issue on the nuBuilder version that I didn't have the issue with. BTW, to answer your question, that working version is:-

nuBuilder Forte 4.5

DB Version: V.4.5-2022.01.30.00
Files Version: V.4.5-2022.02.22.02


I modified both versions for the experiment as follows:-
Created a new form with the Display field using the SQL you listed.
Modified the main form to set the testhk global hashcookie.
Modified the button onclick code to fire up the new form.

Both versions display the global hash cookie correctly ('123') in the Display field of the new form.
So this simplification is not equivalent, unfortunately, and there must be something else going on.
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: global hashcookie apparently forgotten but then remembered

Unread post by kev1n »

Can't you just use/update to the working version since it's newer than the flawed one?
incoherence
Posts: 22
Joined: Sun May 08, 2022 2:36 pm
Has thanked: 9 times
Been thanked: 1 time

Re: global hashcookie apparently forgotten but then remembered

Unread post by incoherence »

In short, no. Sorry!

The working version is 2022-Jan/Feb. The flawed version is 2023-Jan. I also completely reimplemented my entire UI from scratch using the newer version. I did that because I ran into an issue when I tried to use the Upgrade function, which meant I didn't really trust it. On top of that something went wrong today when I tried to back up/restore the old working version, so I am not even sure whether the old working version I had will be usable without a lot of other work.
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: global hashcookie apparently forgotten but then remembered

Unread post by kev1n »

I created the browse/edit + subform and additional edit form and tested with 22-Feb and 23-Jan. I'm now able to reproduce the problem and will hopefully be able to find a solution soon.
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: global hashcookie apparently forgotten but then remembered

Unread post by kev1n »

There's now a fix available.

To resolve the issue, you can try replacing the updated nuSetGlobalPropertiesJS() and nuAddJavaScript() with your existing ones. It's worth noting that the problem occurred because the (global) hash cookies were initialised after the custom code instead of before.


Feb 22 working:
https://streamable.com/3zuolq

Jan 23, not working:
https://streamable.com/w4pqy8

Jan 23 version, patched
https://streamable.com/qe6yw0
incoherence
Posts: 22
Joined: Sun May 08, 2022 2:36 pm
Has thanked: 9 times
Been thanked: 1 time

Re: global hashcookie apparently forgotten but then remembered

Unread post by incoherence »

Hi kev1n, thank you so much for this fix and for providing it so quickly! Very much appreciated!
Post Reply