SugarCRM: Populating fields using a Relate field

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

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

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>');

* In the example:

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

* Note that 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

* This doesnt work when populating a relate field

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

(Example if you want to populate another relate field)

'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>
    ),
),

* Dont forget to include the fields of the relate field we are using to populate another relate field

4. Run Quick Repair and Rebuild.

Reference:

http://developers.sugarcrm.com/wordpress/2011/08/31/howto-using-a-relate-field-to-populate-a-custom-field/

About these ads

4 thoughts on “SugarCRM: Populating fields using a Relate field

  1. Could you kindly provide a step-by-step guide on how field-copying is implemented RELATE-to-RELATE field (the link your provided is not particularly clear). The simpler the better of course. Perhaps there is such a guide somewhere but I have not found it after thorough searching.

    Essentially, I have 3 custom modules: PATIENTS (PAT), THERAPY ROUNDS (TRPY) and DOCUMENTS (DOCS).
    PAT>TRPY is a one-many relationship: creates a field in TRPY for the PAT record (field A)
    TRPY>DOCS is a one-many relationship: create a field in DOC for the TRPY record (field X)
    PAT>DOCS is a one-many relationship: creates a field in DOC for the PAT record (field B)

    What I would like to do is this: when I
    (1) quick create a DOC from TRPY module, I would like [field A] to copy/clone/populate [field B], so that the DOC gets linked to the PAT record and appears in the PAT module subpanel list
    AND/OR
    (2) associate a DOC record with a TRPY using the relate [field X] to copy/clone/populate [field B] from [field A] of that related TRPY record, so that the DOC gets linked to the PAT record and appears in the PAT module subpanel list

  2. My case is as follows:
    I have a custom module named Contrating_janitor in this module create a field related to the Contacts module (which renowned as Clients).

    Being in … / editviewdefs.php custom module delacracion seek the related field. the structure of the statement is:

    array (
    ‘name’ => ‘empleador_c’,
    ‘studio’ => ‘visible’,
    ‘label’ => ‘LBL_EMPLEADOR’,
    ),
    1 => ‘name’,
    ),

    What part exactly do I add the code you mention?
    on the other hand where or how I can find the ; of the module to create.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s