Page 1 of 1
Updating a subform field in BS (before save)
Posted: Fri Jan 07, 2022 7:45 pm
by rrandall
Hi,
I have been struggling with the following problem for a while now and need some suggestions.
Use case: Auto-correct some text contained in a field on a subform (grid), before it gets saved to the db table. The auto-correct function does a lookup in another db table to get the new "auto-corrected" text.
I had a look into using nuSetNuDataValue() in the form’s BS (before save), but that seems to be used to update fields on the main form only? Is there any way to accomplish this for subforms?
Re: Updating a subform field in BS (before save)
Posted: Fri Jan 07, 2022 9:32 pm
by kev1n
Hi,
nuGetNuDataValue() + nuSetNuDataValue() only work for the main form. The get & set values from a subform, you can use the functions getSubformValues() + setSubformValues() from below.
In the example below all dates that equal to
2022-01-07 are replaced with
2022-01-08 in the subfrom with id
sub_something, in the column with object id
cus_date.
Code: Select all
$formId = 'sub_something'; // <<<-- Your subform object id here
$field = 'cus_date'; // <---- Replace with your object Id
$values = getSubformValues($nudata, $formId, $field); // get all values of the $field, for each row
for ($c = 0;$c < count($values);$c++) { // loop through the value
if ($values[$c] == '2022-01-07') { // if the values eqauls 2022-01-07
setSubformValues($nudata, $formId, $field, $c, '2022-01-08'); // set a new value
}
}
function getSubformValues($nudata, $formId, $field) {
$arr = array();
for ($d = 0;$d < count($nudata);$d++) {
$sf = $nudata[$d];
if ($formId == $sf->id) {
$key = array_search($field, $sf->fields);
if ($key != false) {
for ($r = 0;$r < count($sf->rows);$r++) {
array_push($arr, $sf->rows[$r][$key]);
}
}
}
}
return $arr;
}
function setSubformValues(&$nudata, $formId, $field, $c, $value) {
for ($d = 0;$d < count($nudata);$d++) {
$sf = $nudata[$d];
if ($formId == $sf->id) {
$key = array_search($field, $sf->fields);
if ($key != false) {
$nudata[$d]->rows[$c][$key] = $value;
$nudata[$d]->edited[$c][$key] = 1;
return true;
}
}
}
return false;
}
Let me know if you have further questions.
Re: Updating a subform field in BS (before save)
Posted: Fri Jan 07, 2022 10:46 pm
by rrandall
It works! Thank you
