Creating Custom Filters In SugarCRM 7

dranneysugarcrm —  March 10, 2014 — 4 Comments

This article is aimed at beginning to intermediate SugarCRM developers who want to create custom filters in SugarCRM version 7.

This does not cover the custom filters that users can create for themselves.  Those are stored on a per-user basis and are not available to all users.  Instead, this document will cover creating filters that all users of the system will have access to.

This article assumes some knowledge of SQL, PHP, Unix, and general web stuff.

About Filters

In Sugar 7 parlance a filter is used to control what rows of data are displayed to the user in a given view.  Sugar has a number of core filters which come with Sugar out of the box.  When you go to a page with a layout that makes use of the filter view and click on the “Filter” pulldown menu you’ll see them displayed as options:

Image

When you choose one of those filters you’ll notice that the rows that show up in the list view change.  The filter supplies certain characteristics the data must meet before the row is displayed in the view.

Filters can be extremely useful when you want to limit what the user sees and not just present every row in the table to the user.

How to create a very basic filter

The core filters are very convenient since you don’t have to do anything to make them show up. However, it’s not unusual to have a need for a more customized filter that sets criteria specific to your individual needs.  No core filter will be able to supply that.  Fortunately, SugarCRM 7 provides a way to  create such a filter without having to descend into the core functionality of the product and hack something into place.

So let’s dive in and create a very simple filter.

For the purposes of this document I have gone into Studio and created a module called “Random Custom Stuff” with a module name of “flt_RandomCustom”.  I know that’s a weird name, but it’s how we do things where I work, so I went with it.  I also added a custom field to the module to hold a random number.  This created both the “flt_randomcustom” and “flt_randomcustom_cstm” tables (among others) in the database.  That will be more interesting later.

When I go to

http://{my instance}/#flt_RandomCustom

I see the following:

Image

You see all the rows for this module displayed because the default filter is “All Random Custom Stuff” which simply shows everything.  We’ll change that by creating a new filter.

Let’s create a filter to only show rows which have a name of “Blorg” and call it the “Blorg Only” filter.  Let’s say that you installed SugarCRM in

/var/www/sugarcrm

The first thing to do would be to create a directory for your new filter.  Like other components such as layouts and views, you create a custom filter by adding a directory and files under the
“custom/” directory in the SugarCRM instance.  We want this filter to be available only to this module so we’ll create the directory:

/var/www/sugarcrm/custom/modules/flt_RandomCustom/clients/base/filters/blorgonly

At first glance that looks like a crazy path, but if you’ve done any other customization of SugarCRM 7 you’ll recognize that this fits with layouts, views, and other customizations.

In that directory create a file “blorgonly.php” which contains the following text:

<?php
$module_name = "flt_RandomCustom";
$viewdefs[$module_name]['base']['filter']['blorgonly'] = array(
    'create'               => false,
    'filters'              => array(
        array(
            'id'                => 'blorgonly',
            'name'              => 'LBL_BLORG_ONLY_FILTER',
            'filter_definition' => array(
                array(
                    'name' => 'Blorg',
                ),
            ),
            'editable'          => false,
        ),
    ),
);

It’s very important that the name of the PHP file match the name of the directory which needs to match the name of the filter.  You’ll notice in the code above that we specify the name of the filter in two places, once as the element of the “filter” viewdefs array, and again as the “id” of the filter.  Both are important.  For the “name” we’ve put in a placeholder for a label string.  Don’t worry, we’ll get to that in a minute.

The real action takes place in the “filter_definition” array.  You can see that we have a single element that says that the “name” has to be “Blorg”.  The “name” field of the array under “filter_definition” is referring to the name of the actual column in the underlying table (flt_randomcustom in this case).  And, just to make things confusing, the name of that column is “name”.

To make it actually work you have to go to the Admin menu, click on Repair, and then click on “Quick Repair and Rebuild”.

When you reload http://{my instance}/#flt_RandomCustom and click on the filter pulldown you should see something like this:

basic_in_place

When you click on the new filter you should see only the row with “Blorg” as the name appear.  My friend, you’ve just created your first custom filter in SugarCRM 7.  Savor the moment.

Now, if you don’t want a reputation for shoddy work, you’re going to want to make that label look like something real.  To do that we have to set up a string for it.  To do that we create another PHP file:

/var/www/sugarcrm/custom/Extension/modules/flt_RandomCustom/Ext/Language/en_us.RCFilt.php

This filename has to start with “en_us.” and end with “.php” but it can be named anything within that.  The contents of that file should be something like:

<?php
$mod_strings['LBL_BLORG_ONLY_FILTER'] = 'Only Blorg Rows';

Once you have that file in place, do the “Quick Repair and Rebuild” again.  Reloading the page and clicking on the filter pulldown should now look like this:

filter_label_done

And there you have it, a simple custom filter users can employ within a custom module.

Making more complex filters

Filters which restrict rows to those matching a single value are all fine and good, you say, but not much use in the real world.  Reality demands much more complex criteria for filters.  I completely agree.  Let’s see if we can construct a less trivial filter.

Constructing a complex filter is exactly the same as the simple filter above except that the “filter_definition” array contents are more complicated.

Here’s where knowledge of SQL comes in handy.  The way that the filter_definition array works is not unlike the WHERE clause of a SELECT statement.  Let’s consider this example:

'filter_definition' => array(
    array(
        'random_number_c' => '17',
        'date_entered' => '2014-01-22 13:21:01',
    ),
),

By default, things that are grouped together within arrays have an implied AND relationship.  In this case the filter will only show rows with a random number of 17 and the given date_entered value.  By the way, did you notice that the random number field ends with “_c”?  That’s right, you can specify custom fields in these filters as well.  They just get treated like the columns of the main table.

Okay, that’s slightly cooler, but how would you do an OR instead of an AND?

The filter mechanism has these command directives which start with $ that you can use.  In this case we want to make use of the “$or” directive:

'filter_definition' => array(
    array(
        '$or' => array(
            array('random_number_c' => '17'),
            array('random_number_c' => '827'),
        ),
    ),
),

This array groups together two conditions with the $or directive which means that rows with either value for that column should be shown.  You’ve probably noticed that the two conditions need to be wrapped in arrays of their own within the $or array.  That’s important.  Bad stuff happens if you forget to do that.  Also, don’t make the mistake of using double-quotes or PHP will try to interpret the $or as a variable and fail.

So now let’s try to construct a really complex filter.

Let’s say you need to create a filter that’s the equivalent of this WHERE clause:

WHERE
(
    date_entered = "2014-03-01 19:54:47"
    OR (
        random_number_c > 0
        AND random_number_c < 40
    )
)
AND (
    name LIKE “A%"
    OR name LIKE "B%"
    OR name LIKE "R%"
)

Wow.  Okay, so it’s kind of tough to just rattle off the PHP necessary to do this, so let’s break it down logically.  First, we have two big conditions AND’ed together which implies:

'filter_definition' => array(
    array(
        //The thing with ‘date_entered’ and other stuff
    ),
    array(
        //The thing with the three ‘name’ conditions
    ),
),

The second condition is a little more straightforward, so let’s handle that one first.  To reproduce the behavior with the LIKE’s above, SugarCRM filters have a $starts directive which tests for whether the value of a string starts with the given value.  So we would use it as follows:

'filter_definition' => array(
    array(
        //The thing with the ‘date_entered’ and other stuff
    ),
    array(
        '$or' => array(
            array(
                'name' => array(
                    '$starts' => 'A',
                ),
            ),
            array(
                'name' => array(
                    '$starts' => 'B',
                ),
            ),
            array(
                'name' => array(
                    '$starts' => 'R',
                ),
            ),
        ),
    ),
),

While that may look intimidating, actually it makes sense.  It’s just three $starts conditions OR’ed together.

Now let’s attack the first bit.  What if all we had to do was do the two tests for the random number?  It would look like this:

'filter_definition' => array(
    array(
        array(
            'random_number_c' => array(
                '$lt' => '40',
            ),
        ),
        array(
            'random_number_c' => array(
                '$gt' => '0',
            ),
        ),
    ),
    array(
        //The thing with the three ‘name’ conditions
    ),
),

That’s not too bad, right?  Now let’s OR in the date_entered test:

'filter_definition' => array(
    array(
        '$or' => array(
            array(
                '$and' => array(
                    array(
                        'random_number_c' => array(
                            '$lt' => '40'
                        ),
                    ),
                    array(
                        'random_number_c' => array(
                            '$gt' => '0'
                        ),
                    ),
                ),
            ),
            array(
                'date_entered' => '2014-03-01 19:54:47'
            ),
        ),
    ),
    array(
        //The thing with the three ‘name’ conditions
    ),
),

Okay, I admit it, that’s a little weird.  You have to explicitly use the $and directive because in this case it gets confused if you leave the AND implied as before.  So let’s put the whole filter together in all its glory:

'filter_definition' => array(
    array(
        '$or' => array(
            array(
                '$and' => array(
                    array(
                        'random_number_c' => array(
                            '$lt' => '40'
                        ),
                    ),
                    array(
                        'random_number_c' => array(
                            '$gt' => '0'
                        ),
                    ),
                ),
            ),
            array(
                'date_entered' => '2014-03-01 19:54:47'
            ),
        ),
    ),
    array(
        '$or' => array(
            array(
                'name' => array(
                    '$starts' => 'A',
                ),
            ),
            array(
                'name' => array(
                    '$starts' => 'B',
                ),
            ),
            array(
                'name' => array(
                    '$starts' => 'R',
                ),
            ),
        ),
    ),
),

Sadly, the filter mechanism lacks some of the features of an SQL WHERE clause such as NOT, a true LIKE, and a few other things.  But you can handle most conditions with filters.

For a complete list of directives and additional documentation you can go to:

http://{your instance}/rest/v10/help

Search for “/<module>/filter” next to “GET” and click on it.

Making your filter the default filter

Normally with a layout that includes the filter pulldown the filter that shows all rows is the default.  This is usually fine, but sometimes it’s not ideal.  It’s pretty easy to set a custom filter as the default filter for a module.

To do this you need to create a “default/” directory in the same “filters/” directory as your other custom filters.  In this directory you create a “default.php” file.  But instead of the filter code you saw above, the file would instead contain something like this:

<?php
$module_name = 'flt_RandomCustom';
$viewdefs[$module_name]['base']['filter']['default'] = array(
    'default_filter' => 'blorgonly',
);

As I imagine you figured out, this will set the default filter to the blorgonly filter we created earlier.  To see this work you sometimes have to clear your browser cache completely after doing the repair and rebuild, but before reloading the page.

Debugging techniques

Remember the horrible complex filter we saw above?  It was as ugly to write as it is to read.  Developing it took a lot of iterations.  There aren’t a lot of tools which will help you figure things out, but there are a few.

Examining the API Call

When SugarCRM 7 tries to apply a filter it makes a GET call to the appropriate filter API.  If you open the JavaScript console on your browser (or Firebug, or whatever your tool of choice is) you can see the API call go by along with any errors.  It can sometimes be helpful to pull out the URL it calls and examine it to see what it might be trying to do.

Here’s the URL for the API call for the big ugly filter (with the various entities replaced with their original characters, of course):

http://{whatever+your+intance+is}/rest/v10/flt_RandomCustom/filter?fields=name,random_number_c,date_entered,my_favorite&max_num=20&order_by=date_modified:desc&filter[0][$or][0][$and][0][random_number_c][$lt]=40&filter[0][$or][0][$and][1][random_number_c][$gt]=0&filter[0][$or][1][date_entered]=2014-03-01+19:54:47&filter[1][$or][0][name][$starts]=A&filter[1][$or][1][name][$starts]=B&filter[1][$or][2][name][$starts]=R

Yeah, yikes.  But, you can ignore everything up to the first &filter parameter which leaves:

&filter[0][$or][0][$and][0][random_number_c][$lt]=40&filter[0][$or][0][$and][1][random_number_c][$gt]=0&filter[0][$or][1][date_entered]=2014-03-01+19:54:47&filter[1][$or][0][name][$starts]=A&filter[1][$or][1][name][$starts]=B&filter[1][$or][2][name][$starts]=R

That’s still not wonderful, but if you split it out by parameter, you get:

&filter[0][$or][0][$and][0][random_number_c][$lt]=40
&filter[0][$or][0][$and][1][random_number_c][$gt]=0
&filter[0][$or][1][date_entered]=2014-03-01+19:54:47
&filter[1][$or][0][name][$starts]=A
&filter[1][$or][1][name][$starts]=B
&filter[1][$or][2][name][$starts]=R

Which actually starts to make some sense, sort of.  Note, for example, how there are two main arrays, just like the arrays we have in our filter_definition array.

Watching how these parameters change as you change your filter code can shed light on how you need to arrange your arrays and directives.  It’s not a slam dunk by any means, but it can help when you’re stuck.  You’ll often see things grouped together in ways you didn’t expect which can point to a problem.

Actually calling the API

Sometimes it’s helpful to actually make the API call that SugarCRM makes so you can see everything in detail including any errors, warnings, and any returned rows.  You can also mess with the parameters directly to see what effect that has on the results.  Fortunately it’s not that hard to do.  Here’s what you need to do:

Bring up your favorite utility for making API calls.  For example, POSTman in Chrome, or RESTClient in Firefox are good choices.  For the purposes of this explanation we’ll use POSTman in Chrome, but the basic ideas are the same regardless of the tool you choose.

Bring up the JavaScript console in Chrome, select the “Network” tab, and reload your page with your filter selected.  You should see a bunch of calls go by.  Once the page is loaded, from the bottom up, hover over each call until you find a one resembling:

http://{your instance}/rest/v10/{your module name}/filter?...

That’s the API call that Sugar makes to apply the filter to the rows.

Click on that entry and make sure the “Headers” tab is selected.  This should show you, among many other things, the OAuth-Token.  Save that string.

java_console

Copy the “Request URL” into the url line for POSTman, set the method to GET, and add OAuth-Token to the header with a value of the string you just saved.

When you hit “Send” you should get a result with a return status of 200 and a “records” array appropriate for your filter.

This too isn’t necessarily a slam dunk in terms of debugging, but sometimes you’ll see errors or other things which may point to the problem.  Also you can manually change the parameters to experiment with different things to try to solve the problem.

dranneysugarcrm

Posts

Internal Applications Developer at SugarCRM

4 responses to Creating Custom Filters In SugarCRM 7

  1. 

    Hi,

    I’ve been trying to implement your example but with some extra customization. I need to create a “custom default filter” for a a relate field selection popup (which ends up using all the regular list code)

    I need to get a date range already typed in by the user but not yet saved in order to filter the relate item the user can select. It’s a rental customization so only items available for the entirity of a date range should show up.

    How can I get the typed in values from the parent panel? I assume it’s some javascript/document get value thing but how do I place that in the PHP filter file?

    Thanks in advance for any input!

    • 

      Hi yc,

      I have added a case in “Sugar Portal” about the functionality you want to develop, and they replied that this functionality does not currently exist in Sugar7, so we should wait to develop it.

      When I have more news, I will report here in another answer.

      Greetings,
      Rubén Recacha.

  2. 

    How would one add a related_field to the available fields in a filter?

    • 

      I solved this on my own. Inside: custom/modules/Opportunities/clients/base/filters/default/default.php
      I copied the functionality of account_name and added my custom related field instead.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s