Showing fields from related module in detail view

I have a related field called “Target Company” on my target record. I would like to show some fields from the related target company record on the detail view of the target record. For example, the Target Company for John Doe is “ACME Corp”. I would like to show the website field of ACME Corp, on the detail view of the target John Doe.

Hi JMarble,

I believe this can be easily done with Logic Hooks.

Here are some references I’ve used to do exactly what you want:

The following article works for standard fields but not for custom ones. Refer to my comment at the end of the article to see a way to make it work with custom fields using Logic Hooks:
http://developer.sugarcrm.com/2012/03/13/howto-grab-fields-from-a-related-record-without-sugar-logic/comment-page-1/#comment-31637

The following link refers to an article that explains how to do that but in a non-upgrade safe manner (not recommended):
http://developer.sugarcrm.com/2011/04/26/howto-add-a-field-from-a-related-module-into-the-listview/

I’ve implemented this using the first technique shown above, in my case in the Meetings module. Which is a very interesting situation because under a Meeting you have a “flex-relate” field that may be linked to different modules, but you can even test the “type” of the related record and then retrieve the fields accordingly.

I hope this helps you.

Cheers, Celso.

1 Like

Thank you! Looks like that should do the trick.

You’re welcome!
Please get back to me if you need some code snippets from my implementation.
Regards, Celso.

Hi JMarble,

You can customize detail view in file “custom/modules/[module name]/views/view.detail.php”.

function display() {
$related_module_field = [Field Value];
$this->ss->assign(“related_module_field”, $related_module_field);
}

Then add custom code in file “custom/modules/metadata/detailviewdefs.php” as following.

1 =>
array (
‘name’ => ‘[Field Name]’,
‘customCode’ => ‘{$related_module_field}’,
),

Hope this helps… :slight_smile:

1 Like

Hi biztech,
Do you believe this solution will work for custom fields. As far as I know, it doesn’t.
On the other hand, this is a more elegant solution than using Logic Hooks, so I appreciate if you can tell me if it works for custom fields.
Thanks, Celso.

Yes of course,

It will work for custom fields.

Is it possible to do this for listview?

Hi Jay,

I know a way to make it possible for listview but I do not think it’s upgrade safe way.

You can customize the module class file and write a function get_list_view_data() as following.

function get_list_view_data() {

$the_array = parent::get_list_view_data();

$the_array[YOUR_FIELD_NAME] = RELATE_FIELD_VALUE ;

return $the_array;
}

Where is the module class file?

I tried modules/MODULENAME/metadata/listviewdefs.php but list view wont load after that

Hi, Celso, I followed your logic hooks steps but could not get it work. I would like to make sure my pre-condition is met for executing your code:
My scenario:

Pre-condition:
In my module A, I have a relate field for Module B (I do NOT create relationship between Module A and Module B, it is just a related field inserted in Module A)

What I try to do:
I try to auto fill/auto update a Drop Down field in Module A (of course, in Module B, there is an exact same drop down field.) So when I choose a record from Module B I would like to relate (using the relate field), I would like Drop down in Module A to display whatever is already saved in drop down value from Module B.

I am curious would data type matter, or affect the logic hooks? Does it work with drop down? Also, Can I match a currency data type to a integer datatype?

I attached the screen shots.

Hi Bruce!

Inside your grabinfo function, I believe you could just access your fields directly from the $focus object, which corresponds do the SugarBean for the current record being processed under the Logic hook. Considering “quantity_of_X” is a custom variable of your Module A record, you could just do this:

if (empty($focus->quantity_of_X)) {
...
}

When you do: “$bean = BeanFactory::getBean(‘JOBS_Jobs’, $focus->parent_id);”, what do you want to do? This will try to retrieve a Bean of module named JOBS_Jobs with record ID “$focus->parent_id”. But remember, “parent_id” must be a field from your Module A. This will happen whenever you have two modules with a formal relationship stablished. You “save” the record_id of a record on Module B into a field on a record on Module A.

I don’t see a way to relate two modules without really linking them using a relationship. But I’ve never did anything like this with custom modules.

Anyway, please try to explain me better what you want to do. Give details of your custom modules, the list of fields, etc., maybe we can try a solution.

Regards, Celso.

1 Like

Hi again!

I’ve just found an eBook which seems to be very good. There’s a free sample which discusses exactly about you needs. Please check it here:

https://leanpub.com/suitecrmfordevelopers

I’m really impressed about the quality of the text, the author did an excellent job! I’ll grab my copy ASAP!

I’m quite sure you’ll be able to achieve what you want just using Logic Hooks and the Bean explanations of this book.

Regards, Celso.

Thank you so much, Celso!
You are right, without the formal relationship built between Module A & B, the method won’t work.

As you suggested, I added the relationship, modified the code, got rid of the fetch_row and replaced the parent_id with the field name I try to retrieve and it works!

BTW, how could I grab two text fields data from Module B and store it in one field in Module A?

Also, I tried your method on retrieving related field from Module B, but it does not work. What should I do?

Hi Bruce!

You can do almost anything you want, so I believe it’s pretty easy to grab those fields from your Module B into a Module A record.

Since you have a record from module A related to module B, you could just try this in your LogicHook for ModuleA:

if (empty($focus->quantity_of_X)) {
  // this will retrieve a record from ModuleB into $bean corresponding to the ID of the desired record in ModuleB
  $bean = BeanFactory::getBean('ModuleB', $focus->variable_containing_id_of_moduleB); // $module, $record_id
  if ($bean) { // test if $bean was loaded successfully
    // this is only necessary if you'll need custom fields from ModuleB
    $bean->custom_fields->retrieve();

    // now set some variables of current record on ModuleA to values retrieved from the related record on ModuleB
    $focus->variable1 = $bean->variable1;
    $focus->variable2 = $bean->variable2;

  }
}

Hope this helps!

1 Like

Hello, Celso, Thank you again, just got the chance to test your latest code, it seems only working under if Module B is a “PARENT” of Module A, I mean, for example, module B has a one-to-many relationship w/ Module A, because, currently, I am trying to grab something from A and display to B, it is not working.

It’s working now!
Thank you again, Celso. I looked carefully through the PDF as you suggested and did some more research online.
Finally, I got it work, fetching the child record into a parent, so excited!
I need to add
reset() and current() two method to make it work. This is where I found the related info at https://community.sugarcrm.com/thread/18189

Hi Bruce!
I’m so glad you could solve your problem, congratulations!
These tricks like reset() and current() are similar to other situations like the need to “load custom fields”, when working with standard modules.
Those forums from SugarCRM are usually a good starting point for these tricks.
Anyway, the e-Book I’ve recommended you has so many useful information that you’d waste several days to learn yourself, so I strongly suggest you buy that book.
Please don’t hesitate in contacting me if you need further assistance.
Sorry for not answering before.
Regards, Celso.

1 Like

Thanks again, Celso. I truly wish I grabbed the ebook earlier, buying one now

Celso, actually, I am still thinking to grab the field content from the related field module (not related module). I am wondering if I can find the related field id and put it in the beanFactory::getBean(‘related field module name’,‘related field id’), after loading the bean, then i can access any field at that related field module and retrieve it. Will it work? I try to find the related field id in vardef, but could not locate it. :huh:

On the other hand, If that is not possible, going back to my previous success scenario, with the formal relationship built between my module A & B (many to many), but now A has 3 B records related to it, and I need to grab field content out of B records, put it back to A, if I use load_relationship method, and later, how to specify the different B in the code, which related records I am grabbing? :dry: