vtiger.entity.aftersave.final triggering before links are made?
I am writing a custom Event Handler for a customer so that if a user creates a new Trouble Ticket which is related to a Project (this is a get_related_list relationship in crmentityrel) we prefix the Ticket No with a "P". So , for example a new Ticket with a number of TT1234 becomes PTT1234.
I wrote the event handler to trigger on vtiger.entity.aftersave.final. But, if I am in a project and go to the related list of Tickets and add a new ticket (which clearly is related to this project) when the event handler fires the crmentityrel relationship has not yet been made so the ticket no does not get modifed. The user must edit and save the ticket again before the event handler works.
Another good reason for #213 perhaps?
Here is the interesting part of my event handler:
if($eventName == 'vtiger.entity.aftersave.final' && $entityData->getModuleName() == 'HelpDesk') {
$ticketId = $entityData->getId();
$query = "SELECT DISTINCT vtiger_crmentity.crmid
FROM vtiger_crmentity
INNER JOIN vtiger_crmentityrel
ON (vtiger_crmentityrel.relcrmid = vtiger_crmentity.crmid
OR vtiger_crmentityrel.crmid = vtiger_crmentity.crmid
)
WHERE (vtiger_crmentityrel.crmid IN (?) AND vtiger_crmentityrel.relmodule = ?)
OR
(vtiger_crmentityrel.relcrmid IN (?) AND vtiger_crmentityrel.module = ?)";
$result = $adb->pquery($query, array($ticketId, 'Project', $ticketId, 'Project'));
$count = $adb->num_rows($result);
if($count) {
for($n = 0; $n < $count-1; $n++) {
$crmid = $adb->query_result($result, $n, 'crmid');
if($crmid != $ticketId) {
$query = "UPDATE vtiger_troubletickets SET ticket_no = CASE
WHEN SUBSTRING(ticket_no, 1, 1) = 'T'
THEN CONCAT('P', ticket_no)
ELSE ticket_no
END
WHERE ticketid = ?";
$adb->pquery($query, array($ticketId));
}
}
}
}