我希望为我们的SugarCRM系统(使用PHP(开发一个自定义逻辑挂钩,它提供以下功能:
保存报价记录(新的或更新的(时:
- 如果没有联系人记录已链接到报价,则
- 获取链接到相关Opportunity的联系人
- 将这些联系人链接到报价
我已经根据SugarCRM开发人员文档创建了逻辑挂钩,并加载到我们的开发实例中(我们在SugarCloud中运行(,但挂钩似乎没有启动。有人能帮我吗?
/custom/Extension/moduless/Quotes/Ext/LogicHooks/ininitializeRecord.php
<?php
$hook_version = 1;
$hook_array['after_save'][] = Array(
//Processing index. For sorting the array.
1,
//Label. A string value to identify the hook.
'after_save example',
//The PHP file where your class is located.
'custom/modules/Quotes/initialise_record.php',
//The class the method is in.
'after_save_initialise_class',
//The method to call.
'after_save_initialise'
);
.custom/modules/报价/ininitialize_record.php
<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
// bean in this context will be Quotes bean being saved
class after_save_initialise_class
{
function after_save_initialise($bean, $event, $arguments)
{
require_once('include/SugarQuery/SugarQuery.php');
$sugarQuery = new SugarQuery();
//Determine if contacts have already been linked
if ($bean->load_relationship('contacts')) {
//Fetch related beans
$relatedBeans = $bean->contacts->get();
// If no contacts have already been connected then initialise. Otherwise abort.
if (count($relatedBeans) === 0) {
// Step 1: Get Opportunity related to quote.
if ($bean->load_relationship('opportunities')) {
//Fetch related beans
$relatedOpportunityBeans = $bean->opportunities->get();
//Retrieve the bean id of the linked opportunity record
$opportunity_record_id = $relatedOpportunityBeans[0];
// Step 2: Get Contacts linked to Opportunity
//Retrieve Opportunity bean
$opportunity_bean = BeanFactory::retrieveBean('Opportunities', $opportunity_record_id);
if ($opportunity_bean->load_relationship('contacts')) {
//Retrieve all contacts connected to that opportunity
$relatedContactBeanIds = $bean->contacts->get();
// Loop through these adding them to the quote bean
foreach ($relatedContactBeanIds as &$value) {
// quotes_contacts_1 is the name of custom many-to-many relationship in SugarCRM between the Quotes and Contacts module created in 'Studio' within SugarCRM
$bean->quotes_contacts_1->add($value);
};
};
};
}
}
}
}
.manifest.php{由SugarCRM程序包加载器用于安装程序包}
<?php
$manifest = array(
'key' => 202106270819,
'name' => 'Sync Contacts To Quote',
'description' => 'Adds Custom Logic Hook to Sync Contacts from Opportunity to Quote Level',
'author' => 'TBC',
'version' => '1.0',
'is_uninstallable' => true,
'type' => 'module',
'acceptable_sugar_versions' =>
array(
'regex_matches' => array(
'11.0.*' //any 11.0 release
),
),
'acceptable_sugar_flavors' =>
array(
'PRO',
'ENT',
'ULT'
),
'readme' => '',
'icon' => '',
'remove_tables' => '',
'uninstall_before_upgrade' => false,
);
$installdefs = array (
'id' => 'SyncContactOpportunityQuote',
'hookdefs' => array(
array(
'from' => '<basepath>/custom/Extension/modules/Quotes/Ext/LogicHooks/initialiseRecord.php',
'to_module' => 'application',
)
),
'copy' => array(
0 => array(
'from' => '<basepath>/custom/modules/Quotes/initialise_record.php',
'to' => 'custom/modules/Quotes/initialise_record.php'
)
)
);
?>
参考文献:
- https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_11.0/Architecture/Logic_Hooks/
- https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_11.0/Data_Framework/Models/SugarBean/#Updating_a_Bean
你的Hook被执行了吗?你是如何验证它是否存在的?
如果它真的加载了,我怀疑它在跳过/退出
//Determine if contacts have already been linked
if ($bean->load_relationship('contacts')) {
//Fetch related beans
$relatedBeans = $bean->contacts->get();
第二行也是最后一行的contacts
不是报价中的实际字段,是吗?你的意思是在那两个地方使用quotes_contacts_1
吗?(您在下文中进一步提到(
同样关于manifest.php:
我不熟悉hookdefs
(我只是使用常规的copy
来安装挂钩文件(,但'to_module' => 'application',
似乎很奇怪——你不是说'to_module' => 'Quotes',
吗?
从机会中获取相关联系人
$opp = $bean->opportunities->getBeans();
$opportunity_record_id = $opp->id;