sugarcrmsuitecrm

filter records in popup list view if assigned user is login SUITECRM


I want to filter records so that the assigned user can only see the records that are assigned to him from the popup list view.

The reason why I'm not doing this in the roles management is because if I assigned a user to a client record then other users that have the same role wouldn't able to see it so I've set the role->list tab to "all" and added custom code in list view that only the login user can see their own records.

Here's what I've done.

<?php

    require_once('include/MVC/View/views/view.popup.php');

    class AccountsViewPopup extends ViewPopup
    {
        public function display()
        {
            parent::display(); // TODO: Change the autogenerated stub

            require_once 'modules/ACLRoles/ACLRole.php';
            $ACLRole = new ACLRole();
            $roles = $ACLRole->getUserRoles($GLOBALS['current_user']->id);

            if (in_array('User1', $roles)) {

                global $db, $current_user;

                $this->where .= " AND accounts.assigned_user_id = '$current_user->id' AND deleted=0 ";
            }
        }
    }

But i get this error:

Undefined property: AccountsViewPopup::$where


Solution

  • For list view only: custom/modules/MODULE_NAME/views/view.list.php

    and following is the helping code:

    require_once('include/MVC/View/views/view.list.php');
    class MODULE_NAMEViewList extends ViewList {
    
        function listViewProcess() {
            global $current_user;
            $this->params['custom_where'] = ' AND module_name.name = "test" ';
    
            parent::listViewProcess();
    }
    
    }
    



    For list and popup view(both):

    You need to change the logic inside create_new_list_query function which actually prepares a query. Some modules have override it a bean level(e.g. see modules/Leads/Lead.php).

    If you want to override it in upgrade safe manner then create a file in custom directory e.g: custom/modules/Leads/Lead.php, then extend it from the core bean class like following:

    <?php
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    
    require_once('modules/Leads/Lead.php');
    class CustomLead extends Lead {
    
        function create_new_list_query($order_by, $where,$filter=array(),$params=array(), $show_deleted = 0,$join_type='', $return_array = false,$parentbean=null, $singleSelect = false, $ifListForExport = false)
        { 
            // Code from create_new_list_query in and then modify it accordingly. 
        }
    }
    

    Register new bean class in this location: custom/Extension/application/Ext/Include/custom_leads_class.php and registration code will look like following:

    <?php
    $objectList['Leads'] = 'Lead';
    $beanList['Leads'] = 'CustomLead';
    $beanFiles['CustomLead'] = 'custom/modules/Leads/Lead.php';
    ?>