I created a hook and it is working but my error message is ignored and the meaasge “you are not authorized to view page” comes up. It does bring me back to the create invoice page and does not update.
<?php
$hook_version = 1;
$hook_array = Array();
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(
1, // Processing index
'Check Duplicate Invoice Name', // Label for the hook
'Z:/xampp/htdocs/suitecrm8/public/legacy/custom/modules/AOS_Invoices/CheckDuplicateInvoiceName.php', // Path to the custom class file
'CheckDuplicateInvoiceName', // Class name
'checkDuplicate', // Method name within the class
);
?>
<?php
class CheckDuplicateInvoiceName {
function checkDuplicate($bean, $event, $arguments) {
// Get the invoice name from the current bean
$invoiceName = $bean->name;
// Query the database for existing invoices with the same name
$query = "SELECT id FROM aos_invoices WHERE name = '{$GLOBALS['db']->quote($invoiceName)}' AND id != '{$GLOBALS['db']->quote($bean->id)}' AND deleted = 0";
$result = $GLOBALS['db']->query($query);
// If a duplicate is found, prevent saving and display an error
if ($GLOBALS['db']->fetchByAssoc($result)) {
// Add an error message to prevent saving
SugarApplication::appendErrorMessage("Duplicate Invoice Name: An invoice with this name already exists.");
// Prevent the record from being saved
SugarApplication::redirect('index.php?module=AOS_Invoices&action=EditView&record=' . $bean->id); // Redirect back to edit view
die(); // Stop execution
}
}
}
?>
I am the administrator don’t know why I would not have permission?
You should tell us which kind of hook you’re using, but generally speaking, hooks are back-end code, I don’t think you can do front-end redirects from a hook.
Do you have any working example of a hook with redirects?
Thanks for getting back so soon. By the way, I am using Suitecrm X8.8.1 on a Windows machine. I am new to PHP and new to writing logic hooks. The 2 samples I left on my original post are all I have. I created 2 PHP files. The first one I believe defines the hook. The 2nd one is the actual hook. The hook is working because the Invoice Module will not update if I use a duplicate Invoice Name. Thats when I get the message “you are not authorized to view this record” - which is not my hook error message. If I use a new invoice name the Invoice Module will update and write to the server. If I edit an Invoice that works also. So, it seems the hook is working like it is supposed to, other than giving me the wrong error message. Any ideas as to why this is happening will be appreciated.
Hello PGR,
I was wondering if you had any suggestions on why my hook is ignoring my error message. I am using the invoice name as my invoice # and I want to make sure I don’t get duplicates. The redirect is in the next to last line in the code. After the $result = $GLOBALS[‘db’]->query($query); runs, it ignores thew rest of the code. The hook is working because it always brings me back to the create invoice page without updating. If I use a good number, it runs normal. Just wish I could get my error message to come up. Any help would be appreciated. I will keep digging on this, not much info on hooks.
You’re not supposed to die in logic hooks. You can probably just return false and that likely prevents saving (I am not sure - try it).
But anyway, before_save logic hooks are not the proper way to do validation, since they are only back-end, and are not tied to the front-end, where you’re expected to provide some feedback to the user. And more so in v8.
Windows isn’t officially supported anymore in Suite 8.
It might be possible that several parts could work, but overall I’d go with a fairly standard Linux system.
There are very little issues with a LAMP server and SuiteCRM.
Bastian,
I was able to setup suitecrm v8 on ubuntu with hyper v. On a fresh install as soon as I tried to login it says wrong credentials. My permissions seem right. I can’t seem to find anything in the logs to help. Maybe you can give me some insite. Don’t hear much from too many people on this forum.
I ran a php bin/console doctrine:schema:validate and came up with “database schema not sync to mapping file”. This seems like it could be the problem which was created on installation. I have tried to sync but got no where. My installation checks showed no errors - don’t understand why I am having login errors.
I have setup invoiceninja. invoiceplane, and dolibarr with no login errors. I have suitecrm v7 and v8 running on windows with xampp.
These login errors should not be happening if I went thru the checks. Bastian Hammer sent me an email saying v8 is not supported on windows, that is why I am trying to setup on windows2022 server with Hyper v on Ubuntu. So here I am and no response from Bastian. Hopefully you can come up with something on the sync errors.
So maybe that’s an error / issue to be investigated separately.
What made you execute bin/console doctrine:schema:validate in the first place?
As for the login (yet another issue than the original topic suggests):
Wrong credentials sounds fairly obvious to me.
For a fresh installation, try a simple password first - sth. like: xCO2GoeyNioZbvcVgLL
I had some issues before with some combination of MariaDB / PW encryption and password with special characters.
As for the Hyper v setup: I’ve never tried that and heard from someone that it caused some issues as well. But I don’t have any experience to what exactly happened there.
I usually use a fresh Linux VPS, vanilla LAMP system, no GUI and it’s really incredibly stable / simple to get SuiteCRM running flawlessly there.