(888) 685-3101 ext. 2

In any CRM system, there are scenarios where users prefer to see a more restricted list when they have to display records from another entity.

For example:
When users create an opportunity and they want to add opportunity products to it, they would prefer the list to be filtered down to only show available/active products. If users were presented with a long list that contains discontinued/inactive/unavailable products, its not very user friendly. They would have to scroll through the list and find the active products and be extra careful not to add product(s) that is/are discontinued/inactive/unavailable.

In SugarCRM this filtering can be done via logic hooks, however the key is you have to know which type of hooks to use. There are 7 types of hooks available in SugarCRM and those 7 hooks are broken down further into specific events. You can refer the type of logic hooks that are available for SugarCRM version 7.6.1.0 here :
http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.6/Logic_Hooks/

In the above example, we have to use before_filter in application hooks.
The before_filter executes when the v10+ REST Service is about to route the request.
The before_filter logic hooks have 3 parameters:

  1. The bean object
  2. The event
  3. The arguments

The arguments parameter is the one that we need to leverage.

$arguments is an Array that contains additional information related to the event.
$arguments[0] is the SugarQuery Object
$arguments[1] is an Array that contains the query options

In this example we will modify the SugarQuery object to inject our custom WHERE clause which will filter the data for us.
Just to give you a crash course in SugarQuery object, the Where() method will need 2 parameters. The first one is the field name and the other one is the value that you want to use as the filter.

Create the following two php files to create an installable package.

This is for the manifest.


<?php
    $manifest =array(
    'acceptable_sugar_versions' => array(
 array(
    'regex_matches' => array (
      '7.*',
    ),
  ),

        'acceptable_sugar_flavors' => array('CE','PRO','CORP','ENT','ULT',),
        'author' => 'Xtivia - Damijaya',
        'description' => 'Opportunity Project Custom Hooks',
        'icon' => '',
        'is_uninstallable' => true,
        'name' => 'Opportunity Project Custom Hooks',
        'published_date' => '2016-01-21',
        'type' => 'module',
        'version' => '1.0',
    );
    
    $installdefs =array(
        'id' => '120384',
        'copy' => array(
			0 => array(
                'from' => '/Files/Custom/modules/xtivia_projects/oppProjectCustomHook.php',
                'to' => 'custom/modules/xtivia_projects/oppProjectCustomHook.php',
            ),
        ),
        'logic_hooks' => array(
            array(
                'module' => 'xtivia_project',
                'hook' => 'before_filter',
                'order' => 99,
                'description' => 'Filter for selection-list view to list only active projects',
                'file' => 'custom/modules/xtivia_projects/oppProjectCustomHook.php',
                'class' => 'oppProjectCustomHookClass',
                'function' => 'filterInactiveProject',
            ),
        ),
    );
 
?>

and this one is for the logic hook


<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class oppProjectCustomHookClass
{
	function filterInactiveProject($bean,$event,$arguments)
	{
         where()->equals('status','Active');
	}
}
?>

I hope this will help you in some way and avoid hours of research!
If you have any questions, please call the XTVIA CRM support line at 1-877-777-9779.

Share This