Monday, 4 February 2013

Showing related module's related data

Today I came across a requirement where client wanted to see Contacts related to a Case at Account's level.

That means, if I open an account, and it has a Case linked to it, I should be able to see the Contacts related to that Case right in the same subpanel.

Few steps and done!

Step 1: Is to create a non-db field in Cases module. Create a file in custom/Extension/modules/Cases/Ext/Vardefs/<any_name>.php and write following code.

<?php
$dictionary["Case"]["fields"]["contacts_non_db"] = array (
    'name' => 'contacts_non_db',
    'type' => 'varchar',
    'default'=> '',
    'vname' => 'LBL_CONTACTS',
    'reportable' =>true,
    'source' => 'non-db'
);

Step 2: Place the field in subpanel. Go to Admin > Studio > Accounts > Subpanels > Cases and press Save.


We cannot find non-db fields here, so we need to change it from file manually.


This will create a file under custom/modules/Cases/metadata/subpanels/Account_subpanel_cases.php

Open that and add following code where you want to see your field.

'contacts_non_db' =>
  array (
    'name' => 'contacts_non_db',
    'vname' => 'LBL_CONTACTS',
    'width' => '30%',
    'default' => true,
    'sortable' => false,
  ), 
Step 3: Now we need to give the value to the field. We need to create logic hook to process and display contacts.

Check if you have logic_hooks.php under folder custom/modules/Cases? If no, create one and add following code.

<?php
$hook_array['process_record'][] = Array(2, 'Show contacts', 'custom/modules/Cases/showContacts.php','showContactsC', 'showContactsF'); 

Then create file at custom/modules/Cases/showContacts.php and add following code in there.

<?php
class showContactsC{
    function showContactsF($bean, $event, $args){
        // fetching related contacts
       $oContacts = $bean->get_linked_beans('contacts', 'Contacts');
       
        $contact = '';
       
        foreach($oContacts as $iContacts => $oContact)
        {  
            if($iContacts < 3)
                $contact .= ",<a href = 'index.php?action=DetailView&module=Contacts&record=".$oContact->id."'>".$oContact->full_name."</a>";
            else{
                $contact .= ',...';
                break;
            }
        }
        $bean->contacts_non_db = substr($contact,1);
    }
}
There you are! Do Quick Repair and Rebuild! The result should be in front of your eyes!


P.S. To optimize the view I have shown only first 3 Contacts, if you want to show all Contacts, remove condition on $iContacts.


No comments:

Post a Comment

Urdhva Tech Pvt. Ltd. Powered by Blogger.

Urdhva Tech

My photo
SugarCRM Consultants Contact us at contact@urdhva-tech.com Skype : urdhvatech

Search This Blog

Stay connected Socially

     

Total Pageviews

Popular Posts