{"id":305,"date":"2020-05-21T17:16:44","date_gmt":"2020-05-21T17:16:44","guid":{"rendered":"https:\/\/www.techcrm.in\/blogs\/?p=305"},"modified":"2020-09-08T14:51:15","modified_gmt":"2020-09-08T09:21:15","slug":"predefined-searches-in-sugarcrm-suitecrm","status":"publish","type":"post","link":"https:\/\/www.techcrm.in\/blogs\/predefined-searches-in-sugarcrm-suitecrm\/","title":{"rendered":"Predefined searches in SugarCRM \/ SuiteCRM"},"content":{"rendered":"\n<p>In \u200bSugarCRM or  SuiteCRM\u200b each user can create, on each module, some saved search filters to be reused many times in the ListView, moreover the system &#8220;remember&#8221; the last search done in a particolar module.<\/p>\n\n\n\n<p>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 &#8220;always starts&#8221; with a specific&nbsp;pre-set search.<\/p>\n\n\n\n<p>The key is&nbsp;<strong>to use the &#8220;after_login&#8221; event of the Users module<\/strong>, creating or extending the&nbsp;<strong>custom\/modules\/Users\/logic_hooks.php<\/strong>&nbsp;file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$hook_version = 1; \n$hook_array = Array(); \n$hook_array&#91;'after_login'] = Array(); \n$hook_array&#91;'after_login']&#91;] = Array(1, 'Pre-saved searches',\n    'custom\/modules\/Users\/DefaultSearches.php', 'DefaultSearches', 'addSavedSearch');<\/code><\/pre>\n\n\n\n<p>If the file already exists we should only add the logic hook that interests us, without eliminating those already present.<\/p>\n\n\n\n<p>At this point we can create the&nbsp;<strong>custom\/modules\/Users\/DefaultSearches.php<\/strong>&nbsp;file which will contain all the logic to create our default saved search filters&nbsp;and preset searches&nbsp;for all users.<\/p>\n\n\n\n<p>In this file I have inserted two generic methods that are&nbsp;<strong>createdSavedSearch($strModule, $strName, $arySearchQuery)<\/strong>, which is responsible for creating a saved search named&nbsp;<strong>$strName<\/strong>, on the module&nbsp;<strong>$strModule<\/strong>&nbsp;with the search query&nbsp;<strong>$arySearchQuery<\/strong>&nbsp;(here&nbsp;we have to specify how we will compile the search filter form, in which all the fields we want to filter on should be present), and&nbsp;<strong>createStoreQuery($strModule, $arySearchQuery)<\/strong>, which instead creates the&nbsp;<strong>$arySearchQuery<\/strong>&nbsp;search filter preset for the&nbsp;<strong>$strModule<\/strong>&nbsp;module.<\/p>\n\n\n\n<p>\u200bIn the&nbsp;<strong>addSavedSearch(&amp;$bean, $event, $arguments)<\/strong>&nbsp;method, you can see some examples of using these methods.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ Manage pre-saved searches (SavedSearch and StoreQuery) for the current user\n\nrequire_once('modules\/SavedSearch\/SavedSearch.php');\nrequire_once('modules\/MySettings\/StoreQuery.php');\n\nclass DefaultSearches {\n\n    \/**\n     * Creation of SavedSearch and StoreQuery for the current user\n     * \n     * @param object $bean\n     * @param object $event\n     * @param array $arguments\n     *\/\n    function addSavedSearch(&amp;$bean, $event, $arguments) {\n        global $current_user;\n\n        \/\/ Create and save the query filter for the Prospect customers (Accounts) assigned to the current user\n        $this->createSavedSearch('Accounts', 'Prospects', array('searchFormTab' => 'advanced_search',\n                'query' => true, 'account_type_advanced' => 'Prospect', 'assigned_user_id_advanced' =>\n                $current_user->id, 'search_module' => 'Accounts', 'saved_search_action' => 'save',\n                'advanced' => true, 'orderBy' => 'NAME', 'sortOrder' => 'ASC'));\n\n        \/\/ Create and save the query filter for the not started tasks (Tasks) assigned to the current user\n        $this->createSavedSearch('Tasks', 'Not Started Tasks', array('searchFormTab' => 'advanced_search',\n                'query' => true, 'status' => 'Not Started', 'assigned_user_id_advanced' => $current_user->id,\n                'search_module' => 'Tasks', 'saved_search_action' => 'save', 'advanced' => true));\n\n        \/\/ Create and save the preset query for the filter on the meetings (Meetings)\n        $this->createStoreQuery('Meetings', array('searchFormTab' => 'advanced_search', 'query' => true,\n                'status_advanced' => 'Planned', 'assigned_user_id_advanced' => $current_user->id,\n                'module' => 'Meetings', 'action' => 'index', 'orderBy' => 'DATE_START', 'sortOrder' => 'ASC'));\n    }\n\n    \/**\n     * Creation of SavedSearch for the current user\n     * \n     * @param string $strModule The module on which to create the SavedSearch\n     * @param string $strName The name of the SavedSearch\n     * @param array $arySearchQuery The search to be saved\n     *\/\n    private function createSavedSearch($strModule, $strName, $arySearchQuery) {\n        global $current_user, $db;\n\n        $beanSavedSearch = new SavedSearch('');\n        $beanSavedSearch->name = $strName;\n        $beanSavedSearch->search_module = $strModule;\n        $beanSavedSearch->contents = base64_encode(serialize($arySearchQuery));\n        $beanSavedSearch->assigned_user_id = $current_user->id;\n        $strQuery = 'SELECT id FROM saved_search WHERE deleted = \"0\" AND assigned_user_id = \"'\n            . $current_user->id . '\"' . ' AND search_module =  \"' . $strModule . '\" AND name = \"' . $strName . '\"';\n        $rst = $db->query($strQuery);\n        if ($row = $db->fetchByAssoc($rst)) {\n            $beanSavedSearch->id = $row&#91;'id'];\n            $strId = $beanSavedSearch->save();\n            $GLOBALS&#91;'log']->debug('SavedSearch ' . $strName . ' for module ' . $strModule . ' updated for user '\n                . $current_user->name . ' &#91;' . $current_user->id . ']: ' . $row&#91;'id']);\n        }\n        else {\n            $beanSavedSearch->new_schema = true;\n            $strId = $beanSavedSearch->save();\n            $GLOBALS&#91;'log']->debug('SavedSearch ' . $strName . ' for module ' . $strModule . ' created for user '\n                . $current_user->name . ' &#91;' . $current_user->id . ']: ' . $strId);\n        }\n    }\n\n    \/**\n     * Creation of StoreQueryfor the current user\n     * \n     * @param string $strModule The module on which to create the StoreQuery\n     * @param array $arySearchQuery The search to be saved\n     *\/\n    private function createStoreQuery($strModule, $arySearchQuery) {\n        global $current_user, $db;\n\n        $beanStoreQuery = new StoreQuery();\n        $beanStoreQuery->query = $arySearchQuery;\n        $beanStoreQuery->SaveQuery($strModule);\n    }\n\n}\n?><\/code><\/pre>\n\n\n\n<p>Hope you find this blog post helpful.<\/p>\n\n\n\n<p>Feel free to add comments and queries, that helps us to improve the quality of posts.<\/p>\n\n\n\n<p>You can contact us at&nbsp;<a rel=\"noreferrer noopener\" href=\"mailto:contact@urdhva-tech.com\" target=\"_blank\">info@techcrm.in<\/a><\/p>\n\n\n\n<p>Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In \u200bSugarCRM or SuiteCRM\u200b each user can create, on each module, some saved search filters to be reused many times in the ListView, moreover the system &#8220;remember&#8221; 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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,9],"tags":[30,12,13],"class_list":["post-305","post","type-post","status-publish","format-standard","hentry","category-sugarcrm","category-suitecrm","tag-search","tag-sugarcrm","tag-suitecrm"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Predefined searches in SugarCRM \/ SuiteCRM - TechCRM<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.techcrm.in\/blogs\/predefined-searches-in-sugarcrm-suitecrm\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Predefined searches in SugarCRM \/ SuiteCRM - TechCRM\" \/>\n<meta property=\"og:description\" content=\"In \u200bSugarCRM or SuiteCRM\u200b each user can create, on each module, some saved search filters to be reused many times in the ListView, moreover the system &#8220;remember&#8221; 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 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.techcrm.in\/blogs\/predefined-searches-in-sugarcrm-suitecrm\/\" \/>\n<meta property=\"og:site_name\" content=\"TechCRM\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-21T17:16:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-09-08T09:21:15+00:00\" \/>\n<meta name=\"author\" content=\"Navin Rakhonde\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Navin Rakhonde\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.techcrm.in\/blogs\/predefined-searches-in-sugarcrm-suitecrm\/\",\"url\":\"https:\/\/www.techcrm.in\/blogs\/predefined-searches-in-sugarcrm-suitecrm\/\",\"name\":\"Predefined searches in SugarCRM \/ SuiteCRM - TechCRM\",\"isPartOf\":{\"@id\":\"https:\/\/www.techcrm.in\/blogs\/#website\"},\"datePublished\":\"2020-05-21T17:16:44+00:00\",\"dateModified\":\"2020-09-08T09:21:15+00:00\",\"author\":{\"@id\":\"https:\/\/www.techcrm.in\/blogs\/#\/schema\/person\/992dfe427bb53dcdfd72dd80e3ef9dbc\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.techcrm.in\/blogs\/predefined-searches-in-sugarcrm-suitecrm\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.techcrm.in\/blogs\/predefined-searches-in-sugarcrm-suitecrm\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.techcrm.in\/blogs\/predefined-searches-in-sugarcrm-suitecrm\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.techcrm.in\/blogs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SugarCRM\",\"item\":\"https:\/\/www.techcrm.in\/blogs\/category\/sugarcrm\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Predefined searches in SugarCRM \/ SuiteCRM\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.techcrm.in\/blogs\/#website\",\"url\":\"https:\/\/www.techcrm.in\/blogs\/\",\"name\":\"TechCRM\",\"description\":\"Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.techcrm.in\/blogs\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.techcrm.in\/blogs\/#\/schema\/person\/992dfe427bb53dcdfd72dd80e3ef9dbc\",\"name\":\"Navin Rakhonde\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.techcrm.in\/blogs\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/9cc8fd1b948255055b85e5d41dabfc6e704f806d180a1e21cb8fb378e2f5c022?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/9cc8fd1b948255055b85e5d41dabfc6e704f806d180a1e21cb8fb378e2f5c022?s=96&d=mm&r=g\",\"caption\":\"Navin Rakhonde\"},\"sameAs\":[\"https:\/\/www.techcrm.in\/\"],\"url\":\"https:\/\/www.techcrm.in\/blogs\/author\/navin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Predefined searches in SugarCRM \/ SuiteCRM - TechCRM","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.techcrm.in\/blogs\/predefined-searches-in-sugarcrm-suitecrm\/","og_locale":"en_US","og_type":"article","og_title":"Predefined searches in SugarCRM \/ SuiteCRM - TechCRM","og_description":"In \u200bSugarCRM or SuiteCRM\u200b each user can create, on each module, some saved search filters to be reused many times in the ListView, moreover the system &#8220;remember&#8221; 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 [&hellip;]","og_url":"https:\/\/www.techcrm.in\/blogs\/predefined-searches-in-sugarcrm-suitecrm\/","og_site_name":"TechCRM","article_published_time":"2020-05-21T17:16:44+00:00","article_modified_time":"2020-09-08T09:21:15+00:00","author":"Navin Rakhonde","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Navin Rakhonde","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.techcrm.in\/blogs\/predefined-searches-in-sugarcrm-suitecrm\/","url":"https:\/\/www.techcrm.in\/blogs\/predefined-searches-in-sugarcrm-suitecrm\/","name":"Predefined searches in SugarCRM \/ SuiteCRM - TechCRM","isPartOf":{"@id":"https:\/\/www.techcrm.in\/blogs\/#website"},"datePublished":"2020-05-21T17:16:44+00:00","dateModified":"2020-09-08T09:21:15+00:00","author":{"@id":"https:\/\/www.techcrm.in\/blogs\/#\/schema\/person\/992dfe427bb53dcdfd72dd80e3ef9dbc"},"breadcrumb":{"@id":"https:\/\/www.techcrm.in\/blogs\/predefined-searches-in-sugarcrm-suitecrm\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.techcrm.in\/blogs\/predefined-searches-in-sugarcrm-suitecrm\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.techcrm.in\/blogs\/predefined-searches-in-sugarcrm-suitecrm\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.techcrm.in\/blogs\/"},{"@type":"ListItem","position":2,"name":"SugarCRM","item":"https:\/\/www.techcrm.in\/blogs\/category\/sugarcrm\/"},{"@type":"ListItem","position":3,"name":"Predefined searches in SugarCRM \/ SuiteCRM"}]},{"@type":"WebSite","@id":"https:\/\/www.techcrm.in\/blogs\/#website","url":"https:\/\/www.techcrm.in\/blogs\/","name":"TechCRM","description":"Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.techcrm.in\/blogs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.techcrm.in\/blogs\/#\/schema\/person\/992dfe427bb53dcdfd72dd80e3ef9dbc","name":"Navin Rakhonde","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.techcrm.in\/blogs\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/9cc8fd1b948255055b85e5d41dabfc6e704f806d180a1e21cb8fb378e2f5c022?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9cc8fd1b948255055b85e5d41dabfc6e704f806d180a1e21cb8fb378e2f5c022?s=96&d=mm&r=g","caption":"Navin Rakhonde"},"sameAs":["https:\/\/www.techcrm.in\/"],"url":"https:\/\/www.techcrm.in\/blogs\/author\/navin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.techcrm.in\/blogs\/wp-json\/wp\/v2\/posts\/305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.techcrm.in\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcrm.in\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcrm.in\/blogs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcrm.in\/blogs\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":2,"href":"https:\/\/www.techcrm.in\/blogs\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"predecessor-version":[{"id":312,"href":"https:\/\/www.techcrm.in\/blogs\/wp-json\/wp\/v2\/posts\/305\/revisions\/312"}],"wp:attachment":[{"href":"https:\/\/www.techcrm.in\/blogs\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcrm.in\/blogs\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcrm.in\/blogs\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}