SugarCRM - Search for records related to the current record

Sun 20 April 2014

Say you have two modules in Sugar: Animals and Ringmasters and they aren't directly related via a standard relationship but they are related through some convoluted means. Now, imagine if you want to override the SugarCRM search to allow you to search for all Animals indirectly related to a Ringmaster, I've written about how to do something similar recently. Conversely, you want to search for all Animals related to a Ringmaster you're currently viewing. It's not a link that exists in Sugar so you can directly see a Dashlet and you don't want to build anything from scratch so that's going to take effort.

What you could do, is create a link on the action drop-down while viewing the Ringmaster that linked you to a search that would call animals supplying the Ringmaster's id as the argument.

This is based on the previous tutorial I've shared.

  • Firstly add a new logic hook in custom/modules/RingMasterModule/logic_hooks.php
$hook_array['after_ui_frame'][] = Array(2, 'Custom Ring Master Buttons', 'custom/modules/RingMasterModule/CustomButtons.php
','RingMasterButtons', 'add');
  • Then add the custom buttons class in custom/modules/RingMasterModule/CustomButtons.php
class RingMasterButtons {
    function add($bean)
    {
        echo $bean;
        $ringMasterID = $_GET['record'];
        if(!$ringMasterID) return;
        // Based on what action we're in, add some buttons!
        switch ($GLOBALS['app']->controller->action)
        {
            case "DetailView": // Add buttons to Detail View

                $button_code = <<<EOQ
<script type="text/javascript">
    $(document).ready(function(){
        var button = $('<li><a href="javascript:void(0)" onclick="$('#ringmaster_search').submit();">Ring Master Animals</a></li>")');

        // Add item to action dropdown on a detail view
        $("#detail_header_action_menu").sugarActionMenu('addItem',{item:button});

        // Add item to action dropdown for all subpanel records on the detail view
        $("#subpanel_list ul.records").each(function(){
            var button = $('<li><a href="javascript:void(0)" onclick="$('#ringmaster_search').submit();">Ring Master Animals</a></li>")');
            $(this).sugarActionMenu('addItem',{item:button});
        });
    });
</script>
<form method='post' action='index' id='ringmaster_search'>
    <input type='hidden' name='ringmaster_search_basic' value='{$ringMasterID}'>
    <input type='hidden' name='module' value='Animals'>
    <input type='hidden' name='action' value='index'>
</form>
EOQ;
                echo $button_code;
                break;
        }
    }
}

Quick Repair and Rebuild for good measure. This should add a hidden form that will be called when the user clicks the Ring Master Animals.

Category: SugarCRM Tagged: search sugarcrm php

comments

Page 1 of 1