Predefined searches in SugarCRM CE 6.5 or SuiteCRM

Written Saturday November 03 2018

In ​SugarCRM CE 6.5 or in SuiteCRM​ each user can create, on each module, some saved search filters to be reused many times in the ListView, moreover the system "remember" the last search done in a particolar module.

Very often, however, customers request some predefined search filters to be defined for all users, or users of a given role, to find them ready in the modules they work on most frequently, or that some module "always starts" with a specific pre-set search.

I found an interesting article by Chad Hutchins that put me on the right track to do both of these things.

Using the logic_hook "after_login" it's possible to apply filters or saved searches to all users when they login.

You need to add a logic_hook in custom/modules/Users/logic_hook.php like this:

$hook_array['after_login'][] = Array(0,
'Logic hook to create filters and saved search for current user',
'custom/modules/Users/UsersHooks.php', 'UsersHooks', 'addDefaultFilters');

Then you create the file custom/modules/Users/UsersHooks.php (in the method addDefaultFilters you can view two examples of how to use it):

<?php

require_once('modules/SavedSearch/SavedSearch.php');
require_once('modules/MySettings/StoreQuery.php');

class UsersHooks {

    /**
     * Logic hook to create filters and saved search for current user
     * @param object $bean
     * @param object $event
     * @param array $arguments
     */
    function addDefaultFilters(&$bean, $event, $arguments) {
        // Create a starting filter on customer accounts
        $this->createStoreQuery('Accounts', array('searchFormTab' => 'advanced_search', 'query' => true,
            'account_type_advanced' => 'Customer', 'search_module' => 'Accounts', 'action' => 'index',
            'orderBy' => 'NAME', 'sortOrder' => 'ASC'));
        // Create a saved search on prospect accounts
        $this->createSavedSearch('Accounts', 'Prospect accounts', array('searchFormTab' => 'advanced_search', 'query' => true,
            'account_type_advanced' => 'Prospect', 'search_module' => 'Accounts', 'action' => 'index',
            'orderBy' => 'NAME', 'sortOrder' => 'ASC'));
    }

    /**
     * Create a SavedSearch for current user
     *
     * @param string $strModule The module on which to create the SavedSearch
     * @param string $strName The name of the SavedSearch
     * @param array $arySearchQuery The query to save
     */
    private function createSavedSearch($strModule, $strName, $arySearchQuery) {
        global $current_user, $db;

        $beanSavedSearch = new SavedSearch('');
        $beanSavedSearch->name = $strName;
        $beanSavedSearch->search_module = $strModule;
        $beanSavedSearch->contents = base64_encode(serialize($arySearchQuery));
        $beanSavedSearch->assigned_user_id = $current_user->id;
        $strQuery = 'SELECT id FROM saved_search WHERE deleted = "0" AND assigned_user_id = "'
            . $current_user->id . '"' . ' AND search_module =  "' . $strModule . '" AND name = "' . $strName . '"';
        $rst = $db->query($strQuery);
        if ($row = $db->fetchByAssoc($rst)) {
            $beanSavedSearch->id = $row['id'];
            $strId = $beanSavedSearch->save();
            $GLOBALS['log']->debug('SavedSearch ' . $strName . ' for module ' . $strModule . ' updated for user '
                . $current_user->name . ' [' . $current_user->id . ']: ' . $row['id']);
        }
        else {
            $beanSavedSearch->new_schema = true;
            $strId = $beanSavedSearch->save();
            $GLOBALS['log']->debug('SavedSearch ' . $strName . ' for module ' . $strModule . ' created for user '
                . $current_user->name . ' [' . $current_user->id . ']: ' . $strId);
        }
    }

    /**
     * Create a StoreQuery for current user
     *
     * @param string $strModule The module on which to create the StoreQuery
     * @param array $arySearchQuery The query to save
     */
    private function createStoreQuery($strModule, $arySearchQuery) {
        $beanStoreQuery = new StoreQuery();
        $beanStoreQuery->query = $arySearchQuery;
        $beanStoreQuery->SaveQuery($strModule);
    }
}
?>

Problems? Contact us for professional advices on SugarCRM CE or SuiteCRM.