HOWTO: Create a Flex Relate for other modules

bsoremsugar —  May 16, 2011 — 23 Comments

You may recall seeing this field in the various activity modules in Sugar:

This is what’s known as the “Flex Relate” field, which allows you to relate a record to one in a different module that you specify. This allows you to create a relationship where the target entity is flexible, which allows you to represent all sorts of business logic clearly. A great example of this the various activity entities in the app ( Calls, Meetings, Tasks ), which make it so you can relate the activity to one of many different record types.

The only downside of this field, is there’s no good way to build it using Module Builder or Studio ( or least in a very useful way ). However, it’s a pretty easy code customization you can do which is upgrade-safe. Let’s look at how.

We’ll assume we made a new custom module via Module Builder named test_flexparent, which will have a flex relate field that can relate to either the Contacts or Leads module. To do this, we must define the fields and relationships which will be needed to make this happen; we can do this by adding the file custom/Extension/modules/test_flexparent/Ext/Vardefs/flexrelate.php with the following contents:

There are three parts to this file. The first part defines the three fields we have added to manage the relationship: parent_name, parent_id, and parent_type. We next add two link fields which will correspond to the relationships we’ll add for the links between this module and contacts and leads. Finally, we define the actual relationships themselves.

One more thing we need to do is add in the language files which specify the field’s string name as well as the dropdown. You can do this by adding a file named custom/Extension/application/Ext/Language/en_us.flexrelate.php.

Now after a Repair Relationships and a Quick Rebuild and Repair, the field will be available and you can add it to the module’s various views.

23 responses to HOWTO: Create a Flex Relate for other modules

  1. 

    Sweet.  It is blog postings like this that are very useful in developing with Sugar.   It would be even better if this type of functionality could be hard baked into the core.  Cheers, ~david

  2. 

    Very nice – and clear, thank you

    One question: Using the example above, what happens if I use the Flex Relate field to select a Lead, then later on I go back and use the Flex Relate Field and select a Contact.

    Does the second selection (of a Contact) set the delete field to 1 for the first selection (Lead) relationship or do I need to find a way to add script to delete the first relationship

    If I need to add script, do you have a suggestion for how to do this

    Ramblin

  3. 

    Question answered – I think …
    Well,

    I am not sure if this is the correct way to do it or not, but I did find a way to make it work.

    When I set up the additional modules, I created a one-to-many relationship to Contacts for each Module (eg, each Contact can belong to one Facilitator / each Facilitator can have many Contacts = many Contacts to one Facilitators).  This way, the Contact name appears in the Subpanel of the other Module (eg Facilitators) once the relationship is established.

    This left me with 4 Relate Fields showing on the Contact Editview and Detailview.

    But I only wanted the user to be able to connect each Contact to ONE of Accounts, Facilitators, Venues or Caterers.

    So, I hid the 4 relate fields and added one Flex_Relate field with the 4 Modules (Accounts, Facilitators, Venues, Caterers) as the Parent_type options.

    I also added a field called Company Type (contact_co_type) so I could later list, sort and search based on Company Type, using descriptors the user would understand.

    To prevent the possibility that a user could create one relationship and then go back later and create another, without removing the first, I added a logic hook that triggered the following code

    [CODE]
    parent_type == “”) && ($focus->account_name != “”) )
                {
                $focus->parent_type = “Accounts”;
                $focus->parent_name = $focus->account_name;
                $focus->parent_id = $focus->account_id;
                $focus->contact_co_type = “Account”;
                }
                elseif ($focus->parent_type == “Accounts”)
                    {
                    // Set this Contact relationship
                    $focus->contact_co_type = “Account”;
                    $focus->account_name = $focus->parent_name;
                    $focus->account_id = $focus->parent_id;
                    // Undo the other Contact Relationships
                    // (this is as a precaution in case another was previously set)
                    $focus->evmgr_fac_contacts_name = “”;
                    $focus->evmgr_fac7b_fac_ida = “”;
                    $focus->evmgr_ven_contacts_name = “”;
                    $focus->evmgr_vc9ba_ven_ida = “”;
                    $focus->evmgr_cat_contacts_name = “”;
                    $focus->evmgr_ce069_cat_ida = “”;
                    }
                elseif ($focus->parent_type == “EvMgr_Fac”)
                    {
                    // Set this Contact relationship
                    $focus->contact_co_type = “Facilitator”;
                    $focus->evmgr_fac_contacts_name = $focus->parent_name;
                    $focus->evmgr_fac7b_fac_ida = $focus->parent_id;
                    // Undo the other Contact Relationships
                    // (this is as a precaution in case another was previously set)
                    $focus->account_name = “”;
                    $focus->account_id = “”;
                    $focus->evmgr_ven_contacts_name = “”;
                    $focus->evmgr_vc9ba_ven_ida = “”;
                    $focus->evmgr_cat_contacts_name = “”;
                    $focus->evmgr_ce069_cat_ida = “”;
                    }
                elseif ($focus->parent_type == “EvMgr_Ven”)
                    {
                    // Set this Contact relationship
                    $focus->contact_co_type = “Venue”;
                    $focus->evmgr_ven_contacts_name = $focus->parent_name;
                    $focus->evmgr_vc9ba_ven_ida = $focus->parent_id;
                    // Undo the other Contact Relationships
                    // (this is as a precaution in case another was previously set)
                    $focus->account_name = “”;
                    $focus->account_id = “”;
                    $focus->evmgr_fac_contacts_name = “”;
                    $focus->evmgr_fac7b_fac_ida = “”;
                    $focus->evmgr_cat_contacts_name = “”;
                    $focus->evmgr_ce069_cat_ida = “”;
                    }
                elseif ($focus->parent_type == “EvMgr_Cat”)
                    {
                    // Set this Contact relationship
                    $focus->contact_co_type = “Caterer”;
                    $focus->evmgr_cat_contacts_name = $focus->parent_name;
                    $focus->evmgr_ce069_cat_ida = $focus->parent_id;
                    // Undo the other Contact Relationships
                    // (this is as a precaution in case another was previously set)
                    $focus->account_name = “”;
                    $focus->account_id = “”;
                    $focus->evmgr_fac_contacts_name = “”;
                    $focus->evmgr_fac7b_fac_ida = “”;
                    $focus->evmgr_ven_contacts_name = “”;
                    $focus->evmgr_vc9ba_ven_ida = “”;
                    }

            }

        }
    ?>
    [/CODE]

    The convoluted field names )liek evmgr_fac7b_fac_ida) are the system generated (Module Builder) record id fields for the relationship table, which I got by looking in the vardefs file created to establish the relationship.

    Note that I also added a check to see if the Account type was already created – like would be the case or a Lead converted to an Account – and set the parameters accordingly if that was the case.

    It works,

    By setting the id field to “”, it seems to set the Deleted filed in the relationship to 1.  As I said, I am not sure this is how you are supposed to do it, but it works.

    If anyone else has a more elegant (read abides by the SugarCRM programming convention rules that I do not know about) way to do this, please let me know.

  4. 

    I just wish I could make 2 flex relates (2 relations with 2 different group of modules) :(

  5. 

    Are the link fields and the relationships necessary? I was able to accomplish this with only adding the first three fields.

  6. 

    John, Thank you so very much for this post. The SugarCRM Developer Blog has really provided some great information. I’m going to try this technique shortly with the JJWDesign Google Maps package. I wanted to limit the Maps flex related field to certain modules, but could never find a custom safe solution. I’m looking forward to using this. Thanks, Jeff Walters

      • 

        Hello John, how are you?
        I have an issue when I try create a relationship or a flex relate in sugarcrm or suitecrm and I wonder if you can help me.
        I made a relationship between two modules and I had to re-deploy my package. I have understood that when deploying a package again, all changes and code in . /modules, ./custom/modules and ./custom/extension/modules are removed. But, you have an idea or solution so this does not occurs?
        I know I can make changes to my package using study, but need to create a new module and deploy again without losing the changes that I have made in my package.

        thank you for your post and your answer

Trackbacks and Pingbacks:

  1. Sugar Developer Blog » Blog Archive » HOWTO: Add a subpanel using code - May 18, 2011

    [...] thought I would build upon this week’s earlier example of how to add a flex relate by showing how to add the related subpanels as well. If you remember, we’ve added a new [...]

  2. Tasks. Related to. - SugarCRM Forums - August 30, 2011

    [...] [...]

  3. how to create this kind of field - SugarCRM Forums - September 13, 2011

    [...] [...]

  4. Flex Relate: script to remove first rel'n if change rel'n - SugarCRM Forums - October 29, 2011

    [...] [...]

  5. Custom Module - One-to-many Notes - Relation Problem Upon Note Creation - April 11, 2012

    [...] [...]

  6. DetailView of Custom Module not showing Notes - August 2, 2012

    [...] [...]

  7. Create one-to-many relationship without additional table. - August 10, 2012

    [...] [...]

  8. SugarCRM Developer Blog » Blog Archive » Create one-to-many relationship in code, no middle table required - August 15, 2012

    [...] this is pretty easy to do, and very similar to the technique used for creating flex relate fields. Let’s look at the code you need to add in a new file [...]

  9. Related to (Parent Type) field - Add/Include Module - November 9, 2012

    [...] [...]

  10. How can I adjust the list of module When I add "Flex Rlate" field? - March 13, 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