Page 1 of 1

Redirecting to another form by filling the foreign key

Posted: Sun Apr 04, 2021 10:51 am
by absalom
Hi, being on an edit form for a given record (having "id" as its id on current table A), I would like to put two buttons for leaving the current form and redirecting to other forms related to table B:

A button for creating a new record in B with foreign key being automatically filled as A.id (and being invisible/uneditable).

A button for browsing records in B having foreign key being A.id

How can I achieve that?

Re: Redirecting to another form by filling the foreign key

Posted: Sun Apr 04, 2021 11:36 am
by kev1n
The solution depends on how/where the form is opened (new tab, same breadcrumb, new breadcrumb) etc. See Related question)
A button for creating a new record in B with foreign key being automatically filled as A.id (and being invisible/uneditable).
Using sessionStorage allows you to transfer a value from one form to another, regardless of where it is opened (New Breadcrumb, new tab, came Breadcrumb etc.)

Set a Hash Cookie in the button's onclick event:

Code: Select all

sessionStorage.setItem('PK_A',nuCurrentProperties().record_id); 
In the 2nd form (in the form's Custom Code):

Code: Select all

var  fk = sessionStorage.getItem('PK_A'); // Retrieve the PK of Table A
sessionStorage.removeItem('PK_A'); // Remove the item from session storage
$('#Object_FK_ID)').val(fk).change(); // Assign the fk value to the object with ID "Object_FK_ID" on the 2nd form
A button for browsing records in B having foreign key being A.id
Set a global Hash Cookie in the button's onclick event. It the form is going to be opened in the same Breadcrumb, the third parameter can be omitted.

Code: Select all

nuSetProperty('PK_A', nuCurrentProperties().record_id, true)
and in the 2nd form's Browse use that to filter like:

Code: Select all

WHERE foreign_key = '#PK_A#'

Re: Redirecting to another form by filling the foreign key

Posted: Sun Apr 04, 2021 2:27 pm
by absalom
My button is a "Run" object; as long as I open the console before clicking the button for checking the value of

Code: Select all

nuCurrentProperties()
I have the relevant values, but if I add a

Code: Select all

console.log(nuCurrentProperties())
in the onclick event of the "Run" button, the fields get empty; maybe the Javascript is evaluated after the values have been reset?

Re: Redirecting to another form by filling the foreign key

Posted: Sun Apr 04, 2021 2:34 pm
by kev1n
Try using the event onmousedown instead of onclick

Re: Redirecting to another form by filling the foreign key

Posted: Sun Apr 04, 2021 2:41 pm
by absalom
I tried onchange and onkeydown; finally I got it working with onfocus.