Page 1 of 2
#RECORD_ID# from a subform object
Posted: Mon May 28, 2018 2:45 pm
by marcvander
Hey,
I have a form, in which I have a subform. This subform itself has an object. This object is a Select object (dropdown). Here is the SQL query:
Code: Select all
SELECT opportunite_id, opportunite_nom
FROM opportunite
WHERE opportunite_contact_client='#RECORD_ID#'
Though, the dropdown is empty on the subform:
Capture d’écran 2018-05-28 à 14.40.36.png
Is #RECORD_ID# a valid hash cookie on a subform to get the record_id of the main form the subform is on ?
Re: #RECORD_ID# from a subform object
Posted: Mon May 28, 2018 3:49 pm
by toms
marcvander wrote:
Is #RECORD_ID# a valid hash cookie on a subform to get the record_id of the main form the subform is on ?
No. Try this:
Add this to your main form's js:
Code: Select all
if (nuFormType() == 'edit') {
nuSetProperty('MAIN_FORM_RECORD_ID', nuCurrentProperties().record_id);
}
(Note: For new forms, record_id is -1)
Then in your SQL query:
Code: Select all
SELECT opportunite_id, opportunite_nom
FROM opportunite
WHERE opportunite_contact_client='#MAIN_FORM_RECORD_ID#'
Re: #RECORD_ID# from a subform object
Posted: Mon May 28, 2018 5:10 pm
by marcvander
Toms, thank you! There is an interesting thing happening:
I added a display object on the main form to display the value of '#MAIN_FORM_RECORD_ID#'. When I enter the form in edit mode, the display object is empty (called "test" in the following screenshot), and thus my subform select object is empty too:
Capture d’écran 2018-05-28 à 17.06.19.png
But then when I click on Arrange objects in the options, that I move the display object containing the value of '#MAIN_FORM_RECORD_ID#', and I save, the display object works and the select object in the subform works as well:
Capture d’écran 2018-05-28 à 17.05.54.png
When I leave the form and come back, again nothing shows in the display and select objects.
Re: #RECORD_ID# from a subform object
Posted: Mon May 28, 2018 5:33 pm
by toms
I think #MAIN_FORM_RECORD_ID#' is unknown/not yet set when the subform is created.
Arrangning objects/saving will refresh the form and at that time the hash cookie is available and therefore resolves.
What you could try: Save the record id of the main form to a text input field (e.g.mainformrecordid and use that field as hash cookie in your sql (#mainformrecordid#)
Code: Select all
function nuBeforeSave() {
if (nuFORM.edited == true) {
$("#mainformrecordid").val(nuCurrentProperties().record_id).change();
}
return true;
}
Re: #RECORD_ID# from a subform object
Posted: Mon May 28, 2018 6:11 pm
by marcvander
So I tried, and this time, even after moving an object and saving, nothing shows.
My display object:
Code: Select all
SELECT contact_id
FROM contact
WHERE contact_id='#mainformrecordid#'
My select object:
Code: Select all
SELECT opportunite_id, opportunite_nom
FROM opportunite
WHERE opportunite_contact_client='#mainformrecordid#'
Re: #RECORD_ID# from a subform object
Posted: Mon May 28, 2018 6:13 pm
by marcvander
I think #MAIN_FORM_RECORD_ID#' is unknown/not yet set when the subform is created.
What if we try to define #MAIN_FORM_RECORD_ID# before the subform is created. Like at the moment the user clicks on a line in the browse form, before entering edit form ?
Re: #RECORD_ID# from a subform object
Posted: Mon May 28, 2018 8:45 pm
by toms
marcvander wrote:So I tried, and this time, even after moving an object and saving, nothing shows.
...and there's a field on your form (mainformrecordid) with the record id in it?
marcvander wrote:What if we try to define #MAIN_FORM_RECORD_ID# before the subform is created. Like at the moment the user clicks on a line in the browse form, before entering edit form ?
You could try this:
Code: Select all
function nuSelectBrowse(e) {
var r = $('#' + e.target.id).attr('data-nu-primary-key');
nuSetProperty('#MAIN_FORM_RECORD_ID#', r);
nuForm(nuGetProperty('form_id'), r, '', '', '1');
}
Re: #RECORD_ID# from a subform object
Posted: Tue May 29, 2018 6:46 pm
by marcvander
Hey toms,
...and there's a field on your form (mainformrecordid) with the record id in it?
No there are no record id in it. I try to show the current value of #mainformrecordid# but it is empty.
I tried with your last suggestion, still the same, nothing shows both on the subform select object and on the form display object with this query:
Code: Select all
SELECT contact_id
FROM contact
WHERE contact_id='#MAIN_FORM_RECORD_ID#'
Re: #RECORD_ID# from a subform object
Posted: Tue May 29, 2018 7:23 pm
by marcvander
Solved. A little typo in your last code:
Code: Select all
function nuSelectBrowse(e) {
var r = $('#' + e.target.id).attr('data-nu-primary-key');
nuSetProperty('MAIN_FORM_RECORD_ID', r); //Without #
nuForm(nuGetProperty('form_id'), r, '', '', '1');
}
You put '#MAIN_FORM_RECORD_ID#' instead of 'MAIN_FORM_RECORD_ID'
Re: #RECORD_ID# from a subform object
Posted: Tue May 29, 2018 7:50 pm
by marcvander
In the same idea, I'm trying to implement a function so that when on a 1st form, clicking on a button on this 1st form brings on a 2nd form, and on this 2nd form the record id value of the 1st form is pre-entered in one field of the 2nd form.
To do that, I added this on my 1st form:
Code: Select all
function nuSelectBrowse(e) {
var r = $('#' + e.target.id).attr('data-nu-primary-key');
nuSetProperty('ENTREPRISE_ID', r);
nuForm(nuGetProperty('form_id'), r, '', '', '1');
}
And this on my 2nd form:
Code: Select all
if (nuFormType() == 'edit') {
$("#contact_entreprise_id").val('#ENTREPRISE_ID#').change();
}
But nothing is pre-entered in the field contact_entreprise_id