Custom Action

Hi,

I using the following code with version 2023.
I have a field with Name “Starred” Varchar(6) in the same table.
Primary key of table is “Id”.
But it does work.
What could be wrong?

    // Page Load event
 function Page_Load() {
$this->CustomActions["star"] = new ListAction("star", "Add Star"); // Where "star" is the id and "Add Star" is the caption of the custom action
 }

  // Row Custom Action event
function Row_CustomAction($action, $row) {
  if ($action == "star") { // Check action name
    $rsnew = ["Starred" => "Y"]; // Array of field(s) to be updated
    $result = $this->update($rsnew, "Id = " . $row["Id"]); // Update the current record only (the second argument is  WHERE clause for UPDATE statement)
    if (!$result) { // Failure
        $this->setFailureMessage("Failed to update  record, Id = " . $row["Id"]);
        return false; // Abort and rollback
     } elseif ($this->SelectedIndex == $this->SelectedCount) { // Last row
        $this->setSuccessMessage("All selected records updated.");
     }
    return true; // Success
   }
  }

What is the field type of Id field? Is it an integer/numeric or a string type?

  1. The example is working fine, make sure you have generated scripts again after adding the “Starred” field.
  2. Whenever you want to debug, you better enable Debug so you can see the error message, if any.

Hi,

I just tried again.
But it is the same.
It is not working.

Id (INT 11) Auto INC is primary key of table.
Starred (VARCHAR 6) .
I am on Windows 11 with Xammp and PHPMAKER 2023.

Thanks.
mpol_ch

If it does not work, did you see this failure message Failed to update record, Id = …?

Hi,

no. I do not see any message.
Debug is enabled.
And no messaged also by debug.
I deleted the entire projekt folder on the web server and regenerated the files.

It should work, as I’ve tried this following code in products table of demo2023 project:This is the code in Page_Load server event under List Page:

$this->CustomActions["star"] = new ListAction("star", "Add Star"); // Where "star" is the id and "Add Star" is the caption of the custom action

This is the code in Row_CustomAction server event under List Page:

    if ($action == "star") { // Check action name
        $rsnew = ["Discontinued" => "1"]; // Array of field(s) to be updated
        $result = $this->update($rsnew, "ProductID = " . $row["ProductID"]); // Update the current record only (the second argument is  WHERE clause for UPDATE statement)
        if (!$result) { // Failure
            $this->setFailureMessage("Failed to update  record, ProductID = " . $row["ProductID"]);
            return false; // Abort and rollback
        } elseif ($this->SelectedIndex == $this->SelectedCount) { // Last row
            $this->setSuccessMessage("All selected records updated.");
        }
    }
    return true;

It works properly, as I see this success message: All selected records updated.

thank you for your efforts.
But it is not working for me.
Only your “return true;” is different then the official code.
Even then it is not working for me, when I replace the return true after one curly bracet…

My Db is MariaDB.
Everything else seems to work.

I have a project imported from 2022 into 2023 and my row_customactions are not working.
And in log file and debug no error messages.mpol_ch

You may read the docs about Debug more carefully. Make sure you have enabled all the related advanced setting during development.Also, as said in the docs:

Make sure you have also configured your php.ini to display errors (and log errors if necessary):

  • error_reporting (int) - set it to E_ALL
  • display_errors (string) - set it to On
    Otherwise there will no errors reported by PHP.

If there is no log, then you should debug by logging the variables in your code to check if your code is correct, e.g.

Log("Row_CustomAction", [
   "action" => $action, // Check if $action == "star"
   "row" => $row, // Check value of $row["Id"]
   "result" => $result // Check if the update is successful
]);

Hi,
I just installed the demo projekt 2023 and taked the code from mobhar.
It does not update the records.
Could it be something with string?

Double check, and make sure you have already put the code in the right place. In additioin, always post your latest code for more discussion.

Hi mobhar,
I just put your code on the demo projekt (2023).


 // Page Load event
 function Page_Load()
 {
     //Log("Page Load");
     $this->setFixedHeaderTable(true, "mh-400px");
     $this->CustomActions["star"] = new ListAction("star", "Add Star"); // Where "star" is the id and "Add Star" is the caption of the custom action

 }

 // Row Custom Action event
 function Row_CustomAction($action, $row)
 {
// Return false to abort
if ($action == "star") { // Check action name
    $rsnew = ["Discontinued" => "1"]; // Array of field(s) to be updated
    $result = $this->update($rsnew, "ProductID = " . $row["ProductID"]); // Update the current record only (the second argument is  WHERE clause for UPDATE statement)
    if (!$result) { // Failure
        $this->setFailureMessage("Failed to update  record, ProductID = " . $row["ProductID"]);
        return false; // Abort and rollback
         } elseif ($this->SelectedIndex == $this->SelectedCount) { // Last row
             $this->setSuccessMessage("All selected records updated.");
         }
     }
     return true;
 }

Nothing happend. And no updates.
mpol_ch

mobhar wrote:

Double check, and make sure you have already put the code in the right place.

Did you put the code under products table?

Yes, it is in products table.

I wonder why don’t you just add a few lines in your code as suggested above to write some log and help yourself debug.

Tank you your advisr.
Where schould i put the recommended Code?Tanks…

Put it in Row_CustomAction server event.

thank you.
I just provided the project file and db to the phpmaker support.
I hope they will find the reason.

thanks,
mpol_ch

Hi,

now it is working as expected after the support updated the template.

thanks
mpol_ch