James
James

Reputation: 1

SugarCRM: Custom Logic Hook

I am looking to develop a custom logic hook for our SugarCRM system (uses PHP) which provides the following function:

On Save of a Quote record (either new or update):

I have created the logic hook as per SugarCRM developer documentation and loaded into our dev instance (we run in SugarCloud) but the hook does not appear to fire. Can anyone help me?

./custom/Extension/modules/Quotes/Ext/LogicHooks/initialiseRecord.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/Quotes/initialise_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 {Used by SugarCRM Package Loader to install the package}

<?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'
    )
  )
);

?>

References:

Upvotes: 0

Views: 618

Answers (2)

Vikram
Vikram

Reputation: 33

To get related contacts from opportunities

$opp = $bean->opportunities->getBeans();
$opportunity_record_id = $opp->id;

Upvotes: 1

Jay
Jay

Reputation: 3950

Does your Hook get executed at all? How did you verify that it does or doesn't?

If it does get loaded, I suspect that it skips/quits at

    //Determine if contacts have already been linked
    if ($bean->load_relationship('contacts')) {
        //Fetch related beans
        $relatedBeans = $bean->contacts->get();

contacts in 2nd and last line isn't an actual field in Quotes, is it? Did you mean to use quotes_contacts_1 in those two places? (Which you referred to farther below)

Also with regards to the manifest.php:

I'm not familiar with hookdefs (I just use regular copy to install the hook file), but 'to_module' => 'application',, seems odd - don't you mean 'to_module' => 'Quotes',?

Upvotes: 1

Related Questions