Customizing SugarCRM Popup Search (Relate Fields)

For Sugar CE and Professional editions, the Relate Field’s Popup Picker provides an easy, searchable list of a related module’s records. Sometimes, though, there are many more records than the user needs to see for a particular field. For example, suppose a user knows that the Account related to an Opportunity is a particular type of Account, namely a customer, not a competitor. Using code, we can customize the Relate Field to use an initial filter for the Search Window that opens. We do this within the Edit View’s MetaData, specifically in custom/modules/Opportunities/metadata/editviewdefs.php. Originally, the field is referenced in this way:

<br /> $viewdefs['Opportunities']['EditView']['panels']['default'][0] = array (<br /> array(<br /> 'name' =&gt; 'name',<br /> ),<br /> 'account_name',<br /> );<br /> 

The account_name field is abbreviated, which tells the system to use the defaults according to the vardef. Let’s extend this a bit.

<br /> $viewdefs['Opportunities']['EditView']['panels']['default'][0] = array (<br /> array(<br /> 'name' =&gt; 'name',<br /> ),<br /> array(<br /> 'name' =&gt; 'account_name',<br /> ),<br /> );<br /> 

Here we’re saying exactly the same thing, but this format more easily lends to our end-goal of creating an initial filter. Examining the Relate Field Smarty Template, we can see that a subset of the definition is an array called displayParams, and a usable value of displayParams is initial_filter. The filter string will be simple text, with markup presented like a url-encoded GET request. Following our example of specifying an account type,

<br /> $viewdefs['Opportunities']['EditView']['panels']['default'][0] = array (<br /> array(<br /> 'name' =&gt; 'name',<br /> ),<br /> array(<br /> 'name' =&gt; 'account_name',<br /> 'displayParams' =&gt; array(<br /> 'initial_filter' =&gt; '&amp;account_type=Customer',<br /> ),<br /> ),<br /> );<br /> 

When opening the Popup Window, you’ll see that &account_type=Customer was added to the URL, and it is used in retrieving the list of Accounts (even if account_type isn’t in the listview or a normal search parameter of the window). Here’s the before-and-after screenshot:

a typical Popup Search window

a typical Popup Search window

Same Search but using an initial_filter value (highlighted in the URL)

Same Search but using an initial_filter value (highlighted in the URL)

Unfortunately, initial_filter does not affect the type-ahead quick search. I suppose the thought is that if you know the name of the account already, it doesn’t really matter.

You pick the next write-up!

This entry was posted in SugarCRM. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

18 Comments

  1. Max
    Posted January 25, 2012 at 6:13 am | Permalink

    Your solution works well but do you know how to add a variable based on the current record?

    Example:

    Select a Contact from the task module. How can i add the assigned account name in the select box?

    Thanks

    • Matthew Poer
      Posted January 25, 2012 at 10:41 am | Permalink

      Cool question. I bet that we could do something like ‘initial_filter’ => ‘&account_id={$fields.parent_id}’,

      The variable in the {curly brackets} should be parsed by Smarty, so it’s a matter of identifying the correct field in the Contacts module (I believe it’s account_name) and the same from the Task record that’s being displayed (pretty sure it’s a flex relate, so parent_id). Why don’t you try that and let me know how it goes? (don’t forget developer mode and/or repair & rebuild).

      • Michael
        Posted January 30, 2012 at 7:43 am | Permalink
        'initial_filter' => '&account_name_advanced={$fields.parent_name.value}'

        this is the better way because you see the name of the account in the search form.

        When you need a dynamic relate filter you can also add some smarty logic in the editviewdefs.php to get the $fields.parent_type and replace the account_name_advanced with the selected module in the multiselect field.

  2. Posted April 3, 2012 at 5:20 am | Permalink

    awesome tutorial man,
    one thing i want to know,
    i made a custom team module in sugarCE, now i have a relate field to the module( say Calls module),
    now i have a way of getting the team(s) of individual users logged in, but i want to limit my pop up to only show the teams that a certain user has,

    thanks in advance for any suggestion/help :D

    • Matthew Poer
      Posted April 3, 2012 at 7:45 am | Permalink

      I think that, for outside beans’ information (global $current_user), you will need to extend the Relate field to a custom field, e.g. CustomTeamRelate extends Relate. You’ll only need to extend the field type and the specific function that sets up the edit view and the EditView.tpl. Drop the files in custom/include/SugarFields/SugarFieldCustomTeamRelate/CustomTeamRelate.php and they should get picked up.

      I think I would override that setup function to generate a list of appropriate Team record IDs, and then override/utilize the initial_filter param from the editviewdefs to be some array … that is if initial_filter even supports multiple arguments for a single filter, which I’m not sure about. Joel, if you figure out a good sustainable solution to this, I’d be very interested to hear about it!

      • Posted April 10, 2012 at 1:43 am | Permalink

        i found a solution to my problem, its upgrade safe(i think :D), what i did was copy popupdefs.php for the specific module and put it in custom/module//metadata/.
        next i inserted this line of code to the $popupMeta array:
        ‘whereStatement’=> $limit,
        (where limit is the value(query) that i insert into the popup)
        and then made this short code:
        global $current_user;
        $limit = NULL;
        if(!is_admin($current_user))
        {
        $cn = 1;
        $rel = $current_user->get_linked_beans(‘cstm_team_users’, ‘cstm_Team’);
        $limit = ” cstm_team.id in (“;
        foreach($rel as $key=>$team)
        {
        if($cn>=2)
        {
        $limit .=”,”;
        }
        $limit .= “\”{$team->id}\” “;
        $cn++;
        }
        $limit .= “)”;
        }
        basically what it does is get all the teams that the current user is assigned to and add them into my $limit string which in turn will be an additional query for the popup, also i made a condition that will exclude the admin from the limit and make him/her see all the teams

        • Posted April 10, 2012 at 1:46 am | Permalink

          about the path its custom/module/< module name > /metadata/

          i forgot to put & gt . & lt, hehe :D

        • Anil
          Posted August 2, 2013 at 5:19 am | Permalink

          Where do I put below short code (i.e. in which file). can you share the code file:
          global $current_user;
          $limit = NULL;
          if(!is_admin($current_user))
          {
          $cn = 1;
          $rel = $current_user->get_linked_beans(‘cstm_team_users’, ‘cstm_Team’);
          $limit = ” cstm_team.id in (“;
          foreach($rel as $key=>$team)
          {
          if($cn>=2)
          {
          $limit .=”,”;
          }
          $limit .= “\”{$team->id}\” “;
          $cn++;
          }
          $limit .= “)”;
          }

  3. Posted April 11, 2012 at 10:02 pm | Permalink

    Hi Matthew,

    Here is yet another problem, and i was wondering if you could give me some advice
    (a small recap i’m making a team module for sugarCE)
    now my last problem involves the “reports to” function of sugar teams (implicit and explicit team addition),
    now i have worked up the logic as how to do this,
    but my problem is i dont know where to start
    from user subpanel i click the select button and a popup appears, now what i want is when i click a user from the popup, it will be processed by my code to check if the user is reporting to another user and include him/her as well,
    i was wondering what class/function/file(php,js) this could be located
    thanks in advance for any help/advice you might give

  4. Posted May 8, 2012 at 8:10 am | Permalink

    if i make a package and save the popup code in a tpl file it returns an error “call_back_function is not a function” …. any idea??? i’m using sugarcrm ce 6.4.3

  5. an_darnold
    Posted November 13, 2012 at 9:24 am | Permalink

    My question is how did you get a “Create Account” Button on the popup? I am attempting to do that with a custom module I am building in SugarCRM Pro 6.5.7.

    Regards,
    Derek

    • Matthew Poer
      Posted November 14, 2012 at 10:43 am | Permalink

      Derek, check out /modules/Accounts/metadata/popupdefs.php and dig into the $popupMeta['create'] array. I recreated this once for a custom module a year or so ago. I don’t recall the entire process, but I know that it can be done and you can create/use a custom popupdef.php so that it’s upgrade-safe.

  6. Nick
    Posted February 10, 2014 at 10:29 am | Permalink

    How to solve the issue of custom popup using different popupdefs inside /modules/module_name/metadata/ ? Simple scenario, let’s say I want to use custom popup which I can modify thoroughly by appending Javascripts during run-time.

    • Matthew Poer
      Posted February 17, 2014 at 11:08 pm | Permalink

      Nick, could you be more specific? Use an example or elaborate on what you’re trying to do?

  7. Ana
    Posted March 20, 2014 at 11:23 am | Permalink

    Hi, do you know how to enable some kind of range sech in a pop up?

  8. Ana
    Posted March 20, 2014 at 11:25 am | Permalink

    Do you know how to enable range search in a popup?

  9. Posted May 15, 2014 at 4:04 am | Permalink

    I always emailed this web site post page to all my contacts,
    for the reason that if like to read it after that my contacts will too.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Sage President\

Profiling Solutions, Inc
174 Carroll Street
Atlanta, GA 30312
Sales: 1 (866) 515-9959
Office Phone: (404) 525-8003
Office Fax: (404) 525-8005
info@profilingsolutions.com