HOWTO: Grab fields from a related record without Sugar Logic

sugarcrmdevelopers —  March 13, 2012 — 65 Comments

Many of you will remember this blog post from a few weeks back, where I showed you how to add a field from a related module by leverage Sugar Logic. While this is an easy to use method that works great for simple situations and doesn’t require any code level customization, there can be a bit of overhead when using lots of Sugar Logic formulas on a single record as there’s lots of overhead involved in this approach ( for example, if you are using 40+ Sugar Logic formulas on a record ). Plus, it doesn’t currently work in CE as it doesn’t support Sugar Logic.

However, you can still do this without Sugar Logic, leveraging the existing vardefs to grab the record directly. Let’s assume you are wanting to grab the employees field from the related Account record into the Contact record; just add this vardef entry into the a file named custom/Extension/modules/Contacts/Ext/Vardefs/accountsemployeefield.php followed by a ‘Quick Rebuild and Repair’

$dictionary['Contact']['fields']['account_employees_c'] = array (
    'name' => 'account_employees_c',
    'id_name' => 'account_id',
    'type' => 'relate',
    'rname' => 'employees',
    'vname' => 'LBL_ACCOUNT_EMPLOYEES',
    'link' => 'accounts',
    'module' => 'Accounts',
    'source' => 'non-db',
    'dbType' => 'non-db',
    'studio' => 'visible',

They key parts of the above code snippet is the following:

  • type, which is set to relate indicating this is a relate field
  • rname, which points to the field name in the related module record
  • link, which is the link field in the Contacts vardef which specifies this relationship
  • module, which is the module which contains the related record

With this vardef entry in place, you can add the account_employees_c field to the DetailView of the Contacts module to have it appear there. Big thanks to Jerry Clark in our Customer Support team for this tip.

Edited 7/30/2012 – Replaced ‘join_name’ with ‘link’ in examples above.

65 responses to HOWTO: Grab fields from a related record without Sugar Logic


    This really works in DetailView. I tried it in ListView but with NO success. Is it possible to extend given solution to display fields from related record in ListView?


       It should work, but I think you may need to hit the record once to save it first after you add the custom field in.


         Unfortunately it doesn`t work in listview. Maybe there is a way that it would?


           We are looking into this now, here the bug tracking the issue…

            Kirill Belousov July 4, 2012 at 10:43 am

            To make it work properly in ListView as well as in DetailView just add 
            one more line into field definition file:
            ‘link’ => ‘accounts’,


            Best wishes from Russia


               @erop:disqus Can you confirm this working for you?


                Adding ‘link’ => ‘accounts’ (as Kirill Belousov wrote above) is essential to display field content in ListView. Without that one can see only an “empty” link text (see screenshot attached).


                   I was able to implement this and display the related data.  However, it does show up as a “relate field, complete with buttons” and appears to disable saving of changes.  I’m displaying the “name” field from the parent record.  Clicking the select button allows me to select a different parent.  If this code is active, clicking the “Save” button will not save the change on the parent record.  If this code is not in use, it saves the change as expected.

                Kirill Belousov July 20, 2012 at 9:06 pm

                by the way for listview
                it works in 6.5, 
                but doesn’t in 6.3.2 (upgraded from 5.1 … 6.3.2 & fresh one 6.3.2)
                at least for me
                if i add this related field on ListView – search return 0 rersults.
                DetailView is OK

                any idea where to dig?


              Thanks Kirill! I’ve updated the post with your suggested change. Note that the ‘join_name’ field now is no longer required.


    its not working in my case. I,ve tried it many times.


       Can you provide the code changes you made to make this happen?


        In my case I have two related modules. Module  A from where I select a related value from module B. In module B is stored which questions should show up in module A depending on the selection.
        In module A i have all the 45 textboxes. But per selection only a few should be visible.

        When I have selected a value i want to look up al the Yes values in module B related to the selection. These should be accesible via the Formula builder. Because in the formula builder I will create the ( depended )  function
        : equal(“yes_no_module_B”, “1”) in all the 45 textboxes.
        I did it previous with the related function in formula builder :  equal(related($link, “yes_no_module_B”),”0″)  it worked, but it took 30 seconds to load the form.

    GalacticDomin8r April 20, 2012 at 3:47 am

    I had to add ‘studio’ => ‘visible’,


     I t works well …..


    The ‘join_name’ array element should be ‘link’ otherwise you get another ‘relate’ type field complete with buttons


    When I create this for a custom module it returns empty. The field shows up but does not populate with the information from the other module. Code:

    $dictionary[“CL_RAN”][“fields”][“account_office_phone_c”] = array (‘name’ => ‘account_office_phone_c’,’id_name’ => ‘account_id’,’type’ => ‘relate’,’rname’ => ‘phone_office’,’vname’ => ‘LBL_OFFICE_PHONE’,’join_name’=>’accounts’,’module’ => ‘Accounts’,’source’ => ‘non-db’,’dbType’ => ‘non-db’,’studio’ => ‘visible’,); 


    I tried the above on my custom module. But it didn’t work mine senaraio is like that,

    I am trying to fetch some records from mine custom module to accounts module. I have one relate field in the accounts module, I want to get some field values of accounts module are filled from the records of mine custom module. So, If I click the relate module in the accounts module some of the fields of the accounts module get filled.

    I tried the above customization but unable to fetch record from the custom module to accounts module…..

    Looking forward for your attention and reply…..

    Thanks & Regards
    Sumit Sahay 


    How do you grab related fields from custom modules? In the modules//_sugar.php I’m missing the needed information like ‘account_id’ in the given example. How do I find this information?


    Let me repost because of accidental autotagging:

    How do you grab related fields from custom modules? In modules/’my_custom_module’_sugar.php I’m missing the needed information like ‘account_id’ in the given example. How do I find this information?


      Instead of account_id you would use field from relationship table (one you put in ‘link’) like my_module1_my_module2_c. Field would be _ida or _idb, depending on how they are related.


      Instead of account_id you would use field from relationship table (one you put in ‘link’) like my_module1_my_module2_c. Field would be _ida or _idb, depending on how they are related.


    Hi All,
    I want to grab some field from the related module. For that I write the custom code as suggested in the Blog as follows,

    I have one relate field named as division_c in the Accounts module and a dropdown named as category_c in the Accounts module.

    The description of fields in the accounts module are as(want to grab the field)
    field name – division_c
    dropdown name – category_c            //want to grab the this dropdown from the custom module.

    The Related module details are as,
    module name – d_division
    field name – category

    now what I did, I customize the file in the accounts module the file name is as,
    the code for this customization are below,

        ‘id_name’ => ‘account_id’,
        ‘type’ => ‘relate’,
        ‘rname’ => ‘category’,
        ‘vname’ => ‘LBL_CATEGORY’,
        ‘link’ => ‘Accounts’,
        ‘module’ => ‘d_Division’,
        ‘source’ => ‘non-db’,
        ‘dbType’ => ‘non-db’,
        ‘studio’ => ‘visible’,

    Now, what is happening when I select the relate field in the Accounts module the value of category is coming in the  relate module, means the dropdown values are coming in the relate field.
    How would I rectify this.


    New to SugarCRM and having some trouble getting this working like i want. If i use the code as is i can get standard fields from Accounts into Contacts. If i try to grab a new field i’ve added to accounts i cannot seem to get it to work. I’ve added a field to Accounts named GNPI, which shows up as gnpi_c. Then i tried to grab the value into contacts with this:

    dictionary[‘Contact’][‘fields’][‘account_gnpi_c’] = array (
        ‘name’ => ‘account_gnpi_c’,
        ‘id_name’ => ‘account_id’,
        ‘type’ => ‘relate’,
        ‘rname’ => ‘gnpi_c’,
        ‘vname’ => ‘LBL_GNPI’,
        ‘link’ => ‘accounts’,
        ‘module’ => ‘Accounts’,
        ‘source’ => ‘non-db’,
        ‘dbType’ => ‘non-db’,
        ‘studio’ => ‘visible’,

    I never get anything back, can anyone see what it is i am screwing up?

    many thanks.



      I’ve had the exact same issue when doing this with 6.2. It’ll work for a standard field in the related record but it won’t get the value of a custom field.


        I wonder if it is a limitation, or a bug? I suppose it could be that we are going about the wrong way as well. Without direction though, its hard to tell which.


          It is a bug, and I reported a while back

          Feel free to vote it up to get it higher priority



            The bug is closed saying that the method to source the contents of a related field is invalid. The closed explanation then goes on to say that the method that needs to be used is the SugarLogic method – which of course defeats the whole purpose of this posting which is to provide a method when SugrLogc is not available (CE versions)

            Any suggestions now?

            I have a Custom Module trying to pull a value from another custom module.



              [ WORKAROUND for custom fields ] Hello all, I’ve had the same problem, unfortunately this method doesn’t work for custom fields. My workaround was to use Logic Hooks. Basically I’ve setup “after_retrieve” (for the EditView and DetailView) and “process_record” (for the ListView) logic hooks to a custom code where I retrieve the related record from anywhere in the system, such as:

              class MyLogicHookClass {
              function myLogicHookFuncion(&$focus, $event, $arguments) {
              // Move on only if the field is not empy, so that it's not overwritten if set
              if (empty($focus->fetched_row['my_custom_field_in_target_module_c'])) {
              $bean = BeanFactory::getBean('Module', $focus->parent_id); // Module is your related module, such as 'Accounts'
              $bean->custom_fields->retrieve(); // Ref:
              $focus->my_custom_field_in_target_module_c = $bean->my_custom_field_in_related_module_c;

              In my case, I created a custom field on the target module that is filled up with information from the equivalent fields in the related record.

              Worked great for Detail, Edit and List views. In Studio you’ll have to add ‘my_custom_field_in_target_module_c’ to Detail, Edit and List views accordingly.

              Hope this helps everyone!



                Can you please describe the entire process – where to put this code. What other files to edit.

                If we need to show a custom field created in Contacts to show up in Calls


                  Please, try to search for Logic Hooks examples and how this mechanism work. After that you’ll understanding what I mean above. I suggest you start with this one:
                  But in short:
                  1. Create a file named “logic_hooks.php” under “custom/modules/Calls/logic_hooks.php” (in this case under the Calls modules because it’s where you want to show the field from Contacts). You’ll probably want to set up this file to call your function both for the “after_retrieve” and “process_record” Logic Hooks actions;

                  2. Create a file named “whatever.php” under “custom/modules/Calls/whatever.php” (you’ll have to inform the full path to this file in your logic_hooks.php file you’ll create in step 1 above;

                  3. Inside “whatever.php” you’ll create a PHP Class and a PHP function inside this class to be referenced in your logic_hooks.php file created in step 1, in a similar way of my example in the comment above.

                  As previously stated, have a look on how Logic Hooks works and I’m sure you’ll understand what I’m suggesting.

                  Regards, Celso.


                    Thanks for the quick reply. This is what I had tried (which broke the Calls module). Can you tell me the error with this logic hook.

                    1. Additions in the logic_hooks.php file present in the Calls module (custom/Calls/logic_hooks.php)

                    $hook_array[‘process_record’][] = Array(3,
                    2. Additions in the logic_hooks_class.php file (/custom/Calls/logic_hooks_class.php)

                    class MyLogicHookClass {
                    function myLogicHookFuncion($focus, $event, $arguments, $bean) {

                    if (empty($focus->fetched_row[‘status_c2’])) {
                    $bean = BeanFactory::getBean(‘Contacts’, $focus->parent_id);


                    $focus->status_c2 = $bean->status_c;


                      I didn’t understand quite well why are you testing “$focus->fetched_row …”. Maybe you could test the field directly instead of going through fetched_row?
                      After that, you issue a “getBean()” over the Contacts module. Since you’re calling your logic hook function for the “process_record” action, this will be called for every record in a list view. If it happens to have a Call in that view a record related to a parent that is NOT a Contact, you’ll probably break the module when you enter ListView under it.
                      Maybe you’ll want to test first if the parent record is in fact a contact, like this:
                      if ($focus->parent_type == ‘Accounts’) {
                      … your code
                      One thing you should try while debugging is to add lines like this to your code:
                      $GLOBALS[‘log’]->fatal(“Some text. Let’s check a variable value like $focus->parent_type”);



                Do I add my_custom_field_in_target_module_c in studio or should it be already there?



    I´m trying to add the address field from the Accounts module to the Tasks module detailed display.
    I added the following code at:


    $dictionary[‘Task’][‘fields’][‘billing_address_street_c’] = array ( ‘name’ => ‘billing_address_street_c’, ‘id_name’ => ‘account_id’, ‘type’ => ‘relate’, ‘rname’ => ‘billing_address_street’, ‘vname’ => ‘LBL_BILLING_ADDRESS_STREET’, ‘link’ => ‘accounts’, ‘module’ => ‘Accounts’, ‘source’ => ‘non-db’, ‘dbType’ => ‘non-db’, ‘studio’ => ‘visible’,);
    After a quick rebuild the field “billing_address_street_c” it´s added to the ones availables on the Tasks module and it can be added to the editing and detailed displays.
    The problem I found is that it does not populate automatically once I select an account for the task, and that if I select the corresponding value manually it appears OK on the editing view but it´s not recorded when I save the record, so it appears empty on the detailed view or if I retrieve that record again.
    I´m using SugarCRM CE version 6.1.6 with spanish language pack.. I´ll appreciate any help you can give me!!


    I am using this method to get information about a grand-parent record. It works fine for displaying the name with link of the grand-parent in the Detail View, but it will not display in the List View.

    Here is what I used (the actual field and module names have been made generic)

    $dictionary[‘Child’][‘fields’][‘grandparent_id_c’] = array(
    ‘name’ => ‘grandparent_id_c’,
    ‘id_name’ => ‘parent_id’,
    ‘type’ => ‘relate’,
    ‘rname’ => ‘grandparent_id’,
    ‘vname’ => ‘LBL_GRANDPARENT_ID’,
    ‘link’ => ‘parents’,
    ‘module’ => ‘Parents’,
    ‘source’ => ‘non-db’,
    ‘dbType’ => ‘non-db’,

    $dictionary[‘Child’][‘fields’][‘grandparent_name_c’] = array(
    ‘name’ => ‘grandparent_name_c’,
    ‘id_name’ => ‘grandparent_id_c’,
    ‘type’ => ‘relate’,
    ‘rname’ => ‘name’,
    ‘vname’ => ‘LBL_GRANDPARENT_NAME’,
    ‘link’ => ‘grandparent’,
    ‘module’ => ‘Grandparent’,
    ‘source’ => ‘non-db’,
    ‘dbType’ => ‘non-db’,
    ‘studio’ => ‘visible’,


    I am trying to add the phone number for a lead to the call. Below is what I added to the Call’s module Vardef. Not getting errors but number not being displayed in field. Any tips would be greatly appreciated.

    $dictionary[‘Call’][‘fields’][‘lead_phone_c’] = array (

    ‘name’ => ‘lead_phone_c’,

    ‘id_name’ => ‘lead_id’,

    ‘type’ => ‘relate’,

    ‘rname’ => ‘phone_home’,

    ‘vname’ => ‘LBL_LEAD_PHONE’,

    ‘link’ => ‘leads’,

    ‘module’ => ‘Leads’,

    ‘source’ => ‘non-db’,

    ‘dbType’ => ‘non-db’,

    ‘studio’ => ‘visible’,



    I have tried
    But it not help me . I have created
    with this code

    $dictionary[‘Drive_driversettlement’][‘fields’][‘drive_movementtypes_rate_type_c’] = array (

    ‘name’ => ‘drive_movementtypes_rate_type_c’,

    ‘id_name’ => ‘movementtype_id’,

    ‘type’ => ‘relate’,

    ‘rname’ => ‘rate_type’,

    ‘vname’ => ‘Rate Type’,


    ‘link’ => ‘drive_movementtypes’,

    ‘module’ => ‘Drive_movementtypes’,

    ‘source’ => ‘non-db’,

    ‘dbType’ => ‘non-db’,

    ‘studio’ => ‘visible’,


    and in popupdef.php, I have added like that


    array (

    ‘type’ => ‘relate’,

    ‘studio’ => ‘visible’,

    ‘label’ => ‘LBL_MOVEMENT_TYPE’,


    ‘link’ => true,

    ‘width’ => ‘10%’,

    ‘default’ => true,




    ‘label’ => ‘RATE TYPE’,

    ‘type’ => ‘relate’,

    ‘width’ => ‘10%’,

    ‘default’ => true,


    ‘studio’ => ‘visible’,

    ‘link’ => ‘DRIVE_MOVEMENTTYPES’,

    ) ,

    Please let me know what is wrong in my code. I am using Sugarcrm CE 6.5.8


    Suppose this example: I have to list opportunities, and one of the fields I need in the list view, is the contact role “Originator”, so I need to add this condition to the definition.

    I have set up this:

    $dictionary[‘Opportunity’][‘fields’][‘presentador’] = array (
    ‘name’ => ‘presentador’,
    ‘id_name’ => ‘contact_id’,
    ‘type’ => ‘relate’,
    ‘rname’ => ‘contact’,
    ‘vname’ => ‘LBL_REFERIDOPOR’,
    ‘link’ => ‘contacts’,
    ‘module’ => ‘Contacts’,
    ‘source’ => ‘non-db’,
    ‘dbType’ => ‘non-db’,
    ‘studio’ => ‘visible’,

    …but it brings me anyone of the related contacts (i suppose that the first one it finds), so I need to specify a condition here:

    ‘condition’ => ‘contact_role = “Originator”‘

    or something like this.. how should it be?


    I’m trying to apply this to my custom modules, a Sold product named module’s quantity_sold named field to a Profit calculator named’s item_quantity_sold field. I’m trying to get the number from the Sold product’s to the Profit calculator’s but I just can’t figure out how to do it well. I have a one to many relation frome sold product to Profit calc. but if I want to get the data in the Profit calc.’s form it gives me a ‘Bad data passed in;’ error in the popup.

    Please help me if you have an idea what am I doing wrong!



    I am also having an issue with this. I’m trying to get a custom field from the Accounts module from the sd_ServiceDesk module. Here’s my code that I put into a file called account_sd_time_field.php in the custom\Extensions\modules\sd_ServiceDesk\Ext\Vardefs folder

    ‘id_name’ => ‘account_id’,
    ‘type’ => ‘relate’,
    ‘rname’ => ‘prepaid_service_time_c’,
    ‘vname’ => ‘LBL_PREPAID_SERVICE_TIME’,
    ‘join_name’=> ‘accounts’,
    ‘link’ => ‘accounts’,
    ‘module’ => ‘Accounts’,
    ‘source’ => ‘non-db’,
    ‘dbType’ => ‘float’,
    ‘studio’ => ‘visible’,


    The name loads and everything, but the value never shows. I tried in editView and detailView.


    For me, this is working great in SearchView. I’m able to filter the list view this way. But for EditView, the field is not being populated. Why? The input field name is ok, but there’s no SQL query to fill that input. I know because I’m watching the queries log.
    (note I’m talking about a “grandparent” relation).


    I’m in EditView, when I select record from the related module it appears in the input, but after I click Save it dissapears, there is a record in the relationship between both modules but it is not shown in detailview or editview after hitting the save button.


    Good day. I have successfully used this method to ordinary text fields, but do not quite understand two things:
    1.) how to extract the “label” from drop-down field, now extracted only techical value, not label
    2) and the second, I can’t understand how to extract a field that is already associated with children, and is a sub-children relation to it


    Sugar Version 6.5.20 (Build 1001)
    I cant seem to get the field into studio. The field does not create in studio for me to drag it across. I was trying this on 2 different modules and could not get it to work. So I tried it with the very same as tutorials and I dont have account_employees_c in studio/Contacts. I also did a repair?


    I have tried this and I don’t get account_employees_c in studio. Ive copied everything as in tutorial but dont get account_employees_c in studio/Contacts. I also did a repair?


      Hi, this solution dois not work for custom fields. You have to go through Logic Hooks to achieve that. Please have a look at the comments above to see how you can solve this problem.


        HI Celso I tried your way but it broke my Cases module so I have something wrong in the logic hooks. I am trying to copy a field from Contracts (end date) to a custom field in Cases called memberenddate. I added below to my logic_hooks.php

        $hook_array[‘after_retrieve’] = Array();
        $hook_array[‘after_retrieve’][] = Array(1, ‘Description of Logic Hook’, ‘custom/modules/Cases/FirstLogicHook.php’, ‘FirstLogicHook’, ‘testLogic’);
        $hook_array[‘process_record’] = Array();
        $hook_array[‘process_record’][] = Array(1, ‘Description of Logic Hook’, ‘custom/modules/Cases/FirstLogicHook.php’, ‘FirstLogicHook’, ‘testLogic’);

        and added this to my FirstLogicHook.php

        class FirstLogicHook {
        function myFirstLogicFunction(&$focus, $event, $arguments) {
        // Move on only if the field is not empy, so that it’s not overwritten if set
        if (empty($focus->fetched_row[‘memberenddate’])) {
        $bean = BeanFactory::getBean(‘Aos_Contracts’, $focus->parent_id); // Module is your related module, such as ‘Accounts’
        $bean->fields->retrieve(); // Ref:
        $focus->memberenddate = $bean->end_date;

        If I rename the FirstLogicHook.php Cases will work again. Any idea what I did wrong. I never done Logichooks before.

Trackbacks and Pingbacks:

  1. SugarCRM Developer Blog » Blog Archive » HOWTO: Using the bean instead of SQL all the time. - March 23, 2012

    […] he was referring to a post we did where we did some raw SQL queries in the code example. I’ve updated the code examples […]

  2. relationship module fields in one layout - April 11, 2012

    […] […]

  3. SugarCRM Developer Blog » Blog Archive » HOWTO: Grab fields from a related record into your DetailView - April 11, 2012

    […] fields from different modules, or show these fields on a listview. For that reason, check out this post for a better way to do […]

  4. Want to Include two fields of another module - June 18, 2012

    […] […]

  5. Show grandparent field - July 6, 2012

    […] […]

  6. How to turn a Document field in a subpanel into a downloadable Link - July 9, 2012

    […] […]

  7. Details view values in the subpanel - August 3, 2012

    […] […]

  8. Add grandparent field to module views [CE 6.5.2] - August 30, 2012

    […] […]

  9. Change a field that shows up in a Relationship - October 9, 2012

    […] […]

  10. SugarCRM Developer Blog » Blog Archive » How to display related record’s Email Address (and have it work as an email field) - June 14, 2013

    […] Step 1: Create a custom vardef that points to the related record, as shown in previous post,  HOWTO: Grab fields from a related record without Sugar Logic […]

Leave a Reply

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

You are commenting using your 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