SugarCRM: Populating fields using a Relate field

This will enable you to populate a field/fields when a value is selected in a relate field.

Steps are as below,

Step 1:  Go to <sugar>/custom/Extension/modules/<MODULE_NAME>/Ext/Vardefs/ and create a new file (ex. vardef.ext.php)

Step 2:  Add the following codes to the file: (This is for the quicksearch)

$dictionary['<MODULE_NAME>']['fields']['<RELATE_FIELD>']['populate_list'] = array('name', 'id', '<FIELD_FROM_RELATED_MODULE>');
$dictionary['<MODULE_NAME>']['fields']['<RELATE_FIELD>']['field_list'] = array('account_name', 'account_id_c', '<FIELD_TO_POPULATE>');

Step 3: Search the field declaration in custom/modules/<MODULE_NAME>/metadata/editviewdefs.php and add the code below (This is for the popup window)

'displayParams' => array(
    'field_to_name_array' => array(
         '<ID_FROM_RELATED_MODULE>' => '<ID_OF_RELATE_FIELD>',
         '<NAME_FROM_RELATED_MODULE>' => '<NAME_OF_RELATE_FIELD>',
         '<ID_OF_ANOTHER_RELATE_FIELD>' => <ID_OF_ANOTHER_RELATE_FIELD_TO_POPULATE>
         '<NAME_OF_ANOTHER_RELATE_FIELD>' => <NAME_OF_ANOTHER_RELATE_FIELD_TO_POPULATE>
    ),
),

Step 4: Run Quick Repair and Rebuild.

Note :

RELATE_FIELD is the Relate field id we want to use (in the example, we used the account_name)
FIELD_FROM_RELATED_MODULE is the db name of the field we want to get from related module
FIELD_TO_POPULATE is the field in current module that we want to populate with value from FIELD_FROM_RELATED_MODULE
account_id_c is the hidden field in a Relate Field where the id of the record selected is stored
The name should be the first item in the array because the auto complete of Relate field shows the result from the first item in the array
Don’t forget to include the fields of the relate field we are using to populate another relate field

Hope you find this blog post helpful.

Feel free to add comments and queries, that helps us to improve the quality of posts.

You can contact us at info@infotechbuddies.com

Thank you.

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *