Canvas is not supported in your browser, please install a newer HTML5 compactible browser

Colleagues subpanel for Contacts in SugarCRM

Hi Guys and Gals,

It’s been a very long time since my last blog post and the reason is that I have been busy learning SugarCRM at my work and to get it up and running in my company.

One of the requirements that was required was to have a colleague subpanel under the contact. Basically the sales team wanted to see other people related to this contact without actually going into the organization.

So the workaround for this was to make a subpanel to only show the contacts but no actual relationship is created.

Below are the steps as to how you can do it

1) Create a Many to Many relationship under contacts using the studio.
Go to the admin

Go to studio under the admin panel

Click on Contacts and then click on relationships and then add a relationship

Create a many to many relationships to the contacts and mark the label as “Colleagues” and the click “Save and Deploy”

2) You should now have a file under custom/Extension/modules/Contacts/Ext/Layoutdefs/contacts_contacts_1_Contacts.php with the code

$layout_defs["Contacts"]["subpanel_setup"]['contacts_contacts_1'] = array (
  'order' => 100,
  'module' => 'Contacts',
  'subpanel_name' => 'default',
  'sort_order' => 'asc',
  'sort_by' => 'id',
  'title_key' => 'LBL_CONTACTS_CONTACTS_1_FROM_CONTACTS_R_TITLE',
  'get_subpanel_data' => 'contacts_contacts_1',
  'top_buttons' => 
  array (
    0 => 
    array (
      'widget_class' => 'SubPanelTopButtonQuickCreate',
    ),
    1 => 
    array (
      'widget_class' => 'SubPanelTopSelectButton',
      'mode' => 'MultiSelect',
    ),
  ),
);

You have replace the file with these contents.

<?php
 // created: 2013-04-24 09:14:38
$layout_defs["Contacts"]["subpanel_setup"]['contacts_contacts_1'] = array (
  'order' => 100,
  'module' => 'Contacts',
  'subpanel_name' => 'default',
  'sort_order' => 'asc',
  'sort_by' => 'id',
  'title_key' => 'LBL_CONTACTS_CONTACTS_1_FROM_CONTACTS_R_TITLE',
  'top_buttons' => array (),
  'get_subpanel_data' => 'function:getSubpanelQueryParts', 
    // Set to true to indicate we are building a custom SQL query
    'generate_select' => true,             
    'function_parameters' => array(
        // File where the above function is defined at
        'import_function_file' => 'custom/include/custom_utils.php', 
        )
);

3) Now create a file under custom/include/custom_utils.php and enter the following code

<?php
function getSubpanelQueryParts($params)
{

	$bean = $GLOBALS['app']->controller->bean;
	$accid = $bean->account_id;
	$memof = new Account();
	$memof->retrieve($accid);
	$memof = $memof->parent_id;
	$return_array['select']='SELECT contacts.id ';
	$return_array['from']='FROM contacts ';
	$return_array['where']= "WHERE contacts.id  IN ( SELECT contact_id FROM accounts_contacts where contact_id != '$bean->id' AND account_id IN ( '$bean->account_id','$memof' ) )";
	return $return_array;
}

4) Now Remove the edit and the remove button from the subpanel.
Go To admin->studio->contacts->Subpanels->Contacts->Colleagues. Remove the edit and remove button and click on “Save & Deploy”

Now Some Explanation

<?php
 // created: 2013-04-24 09:14:38
$layout_defs["Contacts"]["subpanel_setup"]['contacts_contacts_1'] = array (
  'order' => 100,
  'module' => 'Contacts',
  'subpanel_name' => 'default',
  'sort_order' => 'asc',
  'sort_by' => 'id',
  'title_key' => 'LBL_CONTACTS_CONTACTS_1_FROM_CONTACTS_R_TITLE',
  'top_buttons' => array (),
  'get_subpanel_data' => 'function:getSubpanelQueryParts', 
    // Set to true to indicate we are building a custom SQL query
    'generate_select' => true,             
    'function_parameters' => array(
        // File where the above function is defined at
        'import_function_file' => 'custom/include/custom_utils.php', 
        )
);

What I do here is to remove the top button because all we are using this subpanel is to display content and NOT actually use the relationship. We are also passing a custom query to find the contacts of the parent organization. That is defined in custom/include/custom_utils.php

<?php
function getSubpanelQueryParts($params)
{

	$bean = $GLOBALS['app']->controller->bean;
	$accid = $bean->account_id;
	$memof = new Account();
	$memof->retrieve($accid);
	$memof = $memof->parent_id;
	$return_array['select']='SELECT contacts.id ';
	$return_array['from']='FROM contacts ';
	$return_array['where']= "WHERE contacts.id  IN ( SELECT contact_id FROM accounts_contacts where contact_id != '$bean->id' AND account_id IN ( '$bean->account_id','$memof' ) )";
	return $return_array;
}

Here I am changing the where class to include the contact ids of the parent organization. Here I am doing another trick of pulling in the member organization also.

Final Output will look like this

I will come up with more tips and tricks soon as I go on and make new features for my company.

Posted in MySql, PHP, SugarCRM.