Page 1 of 1

Row_Inserting $rsold not accessible

Posted: Tue Jul 26, 2022 8:31 am
by dquinlan

I am trying to override a status field when copying a record.

The following simple code in Row_Inserting causes an error when Copying that I can't trap

// Row Inserting event
function Row_Inserting($rsold, &$rsnew)
{
    // Enter your code here
    // To cancel, set return value to false
    if ($rsold["Status"]=="Issued") {
       	$rsnew["Status"]="Draft";
    }
	Log("Old Status " . $rsold["Status"]);
	Log("New Status " . $rsnew["Status"]);
    return true;
}

If I add a new record, it is saved and log is:

[2022-07-26T00:23:58.080070+00:00] log.DEBUG: Old Status [] []
[2022-07-26T00:23:58.080336+00:00] log.DEBUG: New Status Draft [] []

If I Copy and save a record I get

Error
An internal error has occurred while processing your request.

and no logs or console messages. Using V2022.12

Any thoughts?


Re: Row_Inserting $rsold not accesible

Posted: Tue Jul 26, 2022 10:04 am
by arbei

The $rsold only exists when you copy a record, when you insert a new record, there is no old record, so in your code you should check if ($rsold) first.


Re: Row_Inserting $rsold not accesible

Posted: Tue Jul 26, 2022 10:29 am
by dquinlan

Sorry I didn't explain that well, I understand rsold doesn't exist on Add.
My issue is I can't access rsold on Copy - it crashes?


Re: Row_Inserting $rsold not accesible

Posted: Tue Jul 26, 2022 11:01 am
by arbei

You may enable debug and check the actual PHP error message.


Re: Row_Inserting $rsold not accesible

Posted: Tue Jul 26, 2022 12:05 pm
by dquinlan

Enable Debug did display error detail

/opt/bitnami/apache/htdocs/models/Invoices.php(2568): Cannot use object of type PHPMaker2022\stables\Recordset as array

I don't understand, because I can operate on $rsnew on Row_inserting e.g

public function rowInserting($rsold, &$rsnew)
{
   	$rsnew["Status"]="Draft";
	Log("New Status " . $rsnew["Status"]);
    return true;
}

I can also operate on both $rsold and $rsnew in Row_Updated function

I just can't operate on $rsold in Row_Inserting when copying a record?


Re: Row_Inserting $rsold not accesible

Posted: Tue Jul 26, 2022 12:50 pm
by arbei

As the error said, the $rsold is Recordset, not array, so you may check first, e.g.

if ($rsold && property_exists($rsold, "fields")) { // Recordset
	$rsold = $rsold->fields;
}
$myValue = $rsold["MyFieldName"];

Re: Row_Inserting $rsold not accesible

Posted: Wed Jul 27, 2022 5:37 am
by dquinlan

That is the answer, many thanks

On Row_Inserting $rsold is a recordset and $rsnew is an array.

	$oldstat = $rsold->fields["Status"];
	if ($oldstat=="Issued") {
             	$rsnew["Status"]="Draft";
	}

While on Row_Updated both are arrays.