HOWTO: Using a relate field to populate a custom field

bsoremsugar —  August 31, 2011 — 11 Comments

Editor’s Note: This post comes from Matt Frost, a Web Developer from SugarCRM Gold Partner Highland Solutions. In this article, he details how he solved a problem of using a data from a related module to populate custom fields on an EditView.

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:
  4. $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}');
  5. 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.
  6. 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(
            'id'=>'accounts_h66a5ccounts_ida',
            'name'=>'accounts_hdsmi_nym_customer_service_name',
            '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.

11 responses to HOWTO: Using a relate field to populate a custom field

  1. 

    Does not work with checkboxes, or dropdowns with non-alpha-numeric entities (such as slashes “/”).

  2. 
    Harshal bagul July 12, 2012 at 1:12 am

    Thanks it’s working

  3. 

    hi,
    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['Account']['fields']['industry']['populate_list'] = array(‘industry’);
    $dictionary['Contacts']['fields']['description']['field_list'] = array(‘description’);

    I tried with Account and Accounts both :)

  4. 

    hi,

    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['Account']['fields']['industry']['populate_list'] = array(‘industry’);$dictionary['Contacts']['fields']['description']['field_list'] = array(‘description’);

    I tried with Account and Accounts both :)

  5. 

    doesn’t work in sugarcrm ce 6.4.4… any help? thanks!

  6. 

    yoooo its working….

Trackbacks and Pingbacks:

  1. copy the value of a relate field, to other module - SugarCRM Forums - September 15, 2011

    [...] [...]

  2. value from employees module to custom fields - SugarCRM Forums - October 27, 2011

    [...] [...]

  3. Modify Default Module via Module Builder package - December 12, 2012

    [...] [...]

  4. SugarCRM: Populating fields using a Relate field | johndopenotes - March 18, 2013

    [...] Reference: http://developers.sugarcrm.com/wordpress/2011/08/31/howto-using-a-relate-field-to-populate-a-custom-… Share this:TwitterFacebookLike this:Like Loading… [...]

  5. SugarCE: How to pass data (selected checkboxes) from popup window to main form? - June 19, 2013

    [...] [...]

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