PHP, SugarCRM, Javascript, Cool Web Apps Developer


Using a relate field to populate a custom field

Posted by swapnil shinde on January 31, 2012 at 2:50 PM


In edit view of your module  once you select any record in relate field, if you want to populate any other field from same module then follow the following steps.

There are times a client requests, or common sense usability dictates the need to use a relate field to populate information related to a record. Often times, you can get away with an address or phone number; but there are times where you need to use a custom field.


The situation for our client required the use of an Account relate field and a field containing an account number that comes from another system our client uses. The requirement was that when the Account relate field is filled in that the account number field be populated with the number for that account. That leaves two situations we had to account for; the use of the Sugar Quick Search and the use of the Account popup.


The first example is how to complete this task using the Sugar Quick Search:


1. In your FTP client, navigate to your /custom/Extensions/modules/{MODULE NAME}/vardefs

2. From here you can either create a new file, or edit an existing file. Look for a file name sugarfield_{name_of_your_field}.php that’s the best place to add new code and maintain organization

3. Open that up and add the following lines:


$dictionary['{YOUR MODULE NAME}']['fields']['{YOUR FIELD NAME}']['populate_list'] = array('id','name','{NAME OF DB FIELD YOU WANT TO ADD}');


$dictionary['{YOUR MODULE NAME}']['fields']['{YOUR FIELD NAME}']['field_list'] = array('account_id_c','account_name','{THE ID OF YOUR FIELD ON THE EDITVIEW LAYOUT}');


4. You’ll want to replace anything contained in curly brackets with the specific information for your application. The populate_list is a list of the database field names that you are going to be pulling from the database. The field_list is the list of fields that you are going to be placing these values.

5. Once you have this in place, go to Admin/Repair/Quick Repair and this process will rebuild the vardefs file in /custom/modules/{MODULE NAME}/Ext/vardefs/vardefs.ext.php.


That’s all there really is to it from a technology standpoint, you can add fields to the arrays to populate more than 1field as well.


The Popup is where things are a little bit more tricky, at least until you know what you need to do. Setting it up in the code is easy enough.


1. Open /custom/modules/{YOUR MODULE}/metadata/editviewdefs.php

2. Field your field definition in the file, our full example is included below:


array (

'name' => 'accounts_hdsmi_nym_customer_service_name',

'displayParams' => array (

'field_to_name_array' => array(



'accountnum_c' => 'account_number_c',


'additionalFields' => array(

'accountnum_c' => 'account_number_c',






This is really all the code you need to get this set up. In the field_to_name_array and additionalFields array the key is the database field and the value is the Sugar field you are going to be populating. Pretty straight forward.


This next part is very important regardless of whether it’s working or not. Go into Studio and click on the module that corresponds to the relate field you are working with. (Account relate field requires you to open the Accounts Module). Go into your layouts and find the layout for your Popup List View. If it’s already working, then the custom field that’s being populated is already in that list view. If it’s not working, you’ll want to add that custom field to the list view. This is very important if it is working, because if that custom field is removed from the layout, it will stop working.



Categories: Jan2012-SugarCRM Help

Post a Comment


Oops, you forgot something.


The words you entered did not match the given text. Please try again.

You must be a member to comment on this page. Sign In or Register

1 Comment

Reply mansoor
1:21 AM on August 23, 2012 
I want to populate description field in contacts module when a account is selected in the accounts relate field , the following code is not working . Can anyone help,

$dictionary['Accounts']['fields']['industry']['populate_list'] = array('industry');
$dictionary['Contacts']['fields']['members']['description'] = array('description');