HOWTO: Get related bean record details from the vardef.

bsoremsugar —  April 23, 2012 — 2 Comments

We had this question asked internally here at Sugar the other day from one of our PS team members:

I am working on Smarty function to get related beans for MultienumField. Relationship Name, module and RHS field name is required for smarty function. Can I set these fields in vardef?

David Wheeler chimed in with a great solution to the problem, which should be helpful for any of us needing to leverage the vardefs to get more information for a related field.

You can’t directly get the relationship name or module from the vardef for relate fields in all cases. The Link field is what you need rather than the relationship name since that is how relationships are accessed.

If you have a relate field that a vardef with:
    “rname” => “description”
    “link” => “accounts”

you can then pull the related accounts description field with something similar to:

$value = "";
$link = $bean->field_defs[$field]['link'];
$rname = $bean->field_defs[$field]['rname'];
$bean->load_relationship($link);
$related = $bean->$link->getBeans();
if (!empty ($related) && isset($related[0]->$rname)
    $value = $related[0]->$rname;

This assumes that $bean is the current working bean and $field is the name of the relate field.

2 responses to HOWTO: Get related bean record details from the vardef.

  1. 

    This doesn’t work.

Trackbacks and Pingbacks:

  1. How to create and save relationships in code - October 3, 2012

    […] […]

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