In this example, i am checking to see if the current contract i'm viewing is tied into any opportunity. If it does have an opportunity associated with it, than the start date for the contract will be set to the custom date field from the Opportunity.
So in all, this is 4 tables you are dealing with.
1. contracts
2. contracts_opportunities
3. opportunities
4. opportunities_cstm
First you setup your logic_hooks.php file under your custom/modules/Contracts directory. You will need to make sure that this is done in the after_save event as the before_save will not fire off the sql query (ignore the references of the names as before_save_logic as i was too lazy to go back in and change all of them).
PHP Code:<?php
// Do not store anything in this file that is not part of the array or the hook version. This file will
// be automatically rebuilt in the future.
$hook_version = 1;
$hook_array = Array();
// position, file, function
$hook_array['after_save'] = Array();
$hook_array['after_save'][] = Array(1, 'contract', 'custom/modules/Contracts/hook.php','ContractSaveLogicHook', 'contract_before_save_logic');
?>
And now your actual hook file.
PHP Code:<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class ContractSaveLogicHook {
function contract_before_save_logic (&$bean, $event, $arguments) {
//$GLOBALS['log']->fatal($bean->opportunity_name);
if ($bean->opportunity_name != "")
{
$sql = "update contracts "
. "set start_date = (select date_first_order_c "
. "from opportunities_cstm "
. "inner join opportunities on opportunities.id = opportunities_cstm.id_c "
. "inner join contracts_opportunities "
. "on contracts_opportunities.opportunity_id = opportunities.id "
. "where contracts_opportunities.contract_id = '".$bean->id."')";
$result = $bean->db->query($sql, true);
}
}
}
?>


LinkBack URL
About LinkBacks




Reply With Quote
Bookmarks