Row Custom Action event to update view

im create view:

SELECT res_partner.id,
res_partner.name,
res_partner.x_referral,
account_invoice.state,
account_invoice.id AS idname,
account_invoice.amount_total,
users.bonus
FROM (((res_partner
LEFT JOIN account_invoice_line ON ((res_partner.id = account_invoice_line.partner_id)))
LEFT JOIN account_invoice ON ((account_invoice_line.invoice_id = account_invoice.id)))
JOIN users ON (((res_partner.x_referral)::text = (users.reff_id)::text)))

create:
// ListOptions Load event
function ListOptions_Load() {
$item = &$this->ListOptions->Add(“generatebonus”);
$item->Header = “Generate Bonus”; // Set the column header (for List page)
$item->OnLeft = TRUE; // Link on left
$item->ShowInButtonGroup = FALSE;
$item->ShowInDropDown = FALSE;
$item->MoveTo(0);
}

create:
// ListOptions Rendered event
function ListOptions_Rendered() {
$this->ListOptions->
Items[“generatebonus”]->
Body = "<a href="#" onclick="return ew.submitAction(event, {action: ‘generatebonus’, method: ‘post’, msg: ‘Akan generate Bonus?’, key: " . $this->KeyToJson(TRUE) . “});">Generate”;
}

create:
// Row Custom Action event
function Row_CustomAction($action, $row) {

$rsnew = [“bonus” => 75000]; // 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)
//$result = SendEmail($fromEmail, $toEmail, $ccEmail, $bccEmail, $subject, $mailContent, $format, $charset, $smtpSecure = “465”, $arAttachments = , $arImages = , $arProperties = NULL);
if (!$result) { // Failure
$this->setFailureMessage(“Failed to generate”);
return FALSE; // Abort and rollback
} elseif ($this->SelectedIndex == $this->SelectedCount) { // Last row
$this->setSuccessMessage(“Berhasil Generate Bonus.”);
}
return TRUE; // Success
}
but on web usualy. Failed to generate.

Is a process like this can’t be done in a relation table?
thx

Double check the “id” field. Is it an Integer or Numeric field type? Or, is it a String field type?

If String, then simply change this:
$result = $this->update($rsnew, “id = $row[id]”);

to:
$result = $this->update($rsnew, “id = '” . $row[id] . “'”);

id is type int4,

is there something wrong? thanks

i use database postgree

Try thise:

$result = $this->update($rsnew, "id = " . $row[“id”]); // Update the current record only (the second argument is WHERE clause for UPDATE statement)

newbiephp wrote:

Is a process like this can’t be done in a relation table?

If your database view is updateable, then it should work.

If not, then try to use Execute() (v2020) or ExecuteUpdate() (v2021) global function to execute the UPDATE SQL statement.

For more info and example regarding Execute() or ExecuteUpdate(), please read “Some Global Functions” sub-topic under “Server Events and Client Scripts” topic of PHPMaker Help menu.

function Row_CustomAction($action, $row) {
if ($action == “generatebonus”) {
$rsnew = [“bonus” => 75000]; // 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)

//$result = SendEmail($fromEmail, $toEmail, $ccEmail, $bccEmail, $subject, $mailContent, $format, $charset, $smtpSecure = “465”, $arAttachments = , $arImages = , $arProperties = NULL);
if (!$result) { // Failure
$this->setFailureMessage(“Failed to generate”);
return FALSE; // Abort and rollback
} elseif ($this->SelectedIndex == $this->SelectedCount) { // Last row
$this->setSuccessMessage(“Berhasil Generate Bonus.”);
}
return TRUE; // Success
}
}
im try this code . n still failed

Then please try my last suggestion above.

You should not update a view, you’d better execute an UPDATE statement to update user.bonus directly.

but I need data in another table to get additional information. is there a solution?
thx

mobhar wrote:

If your database view is updateable, then it should work.

It depends on your Database View supports updateable or not. If not, then try the solution below.


mobhar wrote:

If not, then try to use Execute() (v2020) or ExecuteUpdate() (v2021) global function
to execute the UPDATE SQL statement.

Here, you should be able to update just for “bonus” field in your “users” table. You should be able to identify the user id based on the id value from your Database View via Row_CustomAction server event.

I tried the view directly

$ result = Execute (“UPDATE account_invoice SET x_referral = ‘67000’ where id = 115157”);
does not work,
But if using the original table, the query works fine … can’t the view run row_customaction with another table update? thanks

No, you cannot.

oh ok, it’s a shame even though surely a need like this will be widely used in applications.
I hope in the future view can do this kind of thing. thanks for your help

This has nothing to do with PHPMaker itself. You need to handle it by yourself in the related server event.

This condition is actually similar to another case that uses Database View, too. You need to use “UpdateTable” property that belongs to the Table/Page class to decide which table that will be used to update the record, and exclude the unwanted field(s) by using PHP unset().