Use Case

Let us assume there are two modules – Company and FoodOrder. There are many types of companies:

  • Restaurant
  • Payment Gateway
  • Delivery Company

A FoodOrder has the following associations:

  1. Supplied by Restaurant
  2. Paid using Payment Gateway
  3. Delivered by Delivery Company

This is a classic case of multiple One-to-Many relationships between two modules. Sugar Module Builder does not support this inherently. However, we will start by using the Module Builder and then manually change the files.

Step 1: Create Modules using Module Builder

croppercapture12.jpg

Step 2: Add CompanyType field to Company using Module Builder

Add a custom field – CompanyType to the company. Define this as a DropDown  list with values Restaurant, Payment Gateway, and Delivery Company.

croppercapture14.jpg

Step 3: Create a One-to-Many  Relationship using Module Builder

Add a relationship to the company.

croppercapture13.jpg

Step 4: Publish the Module Files

This will create a zip file with the following hierarchy.

croppercapture15.jpg

Unzip all the files within a directory <root>.

Step 5: Fix the Relationship Just Created

Please follow the excellent tutorial by Sander Marechal at http://www.jejik.com/articles/2008/10/fixing_one-to-many_relationships_in_sugarcrm_5_1/.

In brief, please do the following:

  1. Add ‘LBL_RESTAURANT ‘ => ‘Restaurant’ in <root>/SugarModules/modules/sws_FoodOrder/language/en_us.php
  2. Add ‘label’ => ‘LBL_RESTAURANT’ in <root>/SugarModules/modules/sws_FoodOrder/metadata/detailviewdefs.php and <root>/SugarModules/modules/sws_FoodOrder/metadata/editviewdefs.php

Step 6: Rename Current Relationship

Edit <root>/SugarModules/relationships/relationships/sws_company_sws_foodorderMetaData.php

1. Rename sws_company_sws_foodorder to sws_restaurant_sws_foodorder.

2. Add relationship_role_column and relationship_role_column_value in sws_restaurant_sws_foodorder definition.

croppercapture16.jpg

3.  Add field sws_company_rc_company_type in the table description sws_company_foodorder_c.

croppercapture38.jpg

4. Add index sws_company_sws_foodorder_idb on column sws_companbbd4odorder_idb, and index sws_company_sws_foodorder_company_type on new column sws_company_rc_company_type.

croppercapture18.jpg

5. Change “alternate” index to include both columns for company & foodorder. This essentially tells the system that there could only be one entry between ine instance of company and one instance of food order.

croppercapture19.jpg

6. Rename relationsip in <root>/SugarModules/relationships/vardefs/sws_Company.php

croppercapture20.jpg

7. Also rename in <root>/SugarModules/relationships/vardefs/sws_FoodOrder.php

croppercapture22.jpg

8. Rename in <root>/SugarModules/relationships/layoutdefs/sws_Company.php

croppercapture24.jpg

9. Rename in <root>/SugarModules/relationships/language/sws_Company.php and <root>/SugarModules/relationships/language/sws_FoodOrder.

croppercapture27.jpg

croppercapture28.jpg

Step 7: Add New Relationships

Now we are ready to add new relationships. We will again edit the same files as in previous step.

  1. Add new relationship in <root>/SugarModules/relationships/relationships/sws_company_sws_foodorderMetaData.php

croppercapture40.jpg

2. Edit <root>/SugarModules/relationships/vardefs/sws_Company.php

croppercapture30.jpg

3. Edit <root>/SugarModules/relationships/vardefs/sws_FoodOrder.php

croppercapture31.jpg

croppercapture32.jpg

4. Edit Edit <root>/SugarModules/relationships/layoutdefs/sws_Company.php.

croppercapture33.jpg

5. Finally, edit the Edit <root>/SugarModules/relationships/language /sws_Company.php and <root>/SugarModules/relationships/language /sws_FoodOrder.php files

croppercapture34.jpg

croppercapture35.jpg

Step 8: Display Relationship in Edit / Detail Panels

Now that we have created the relationships, we need to make sure that we can display these in the edit / details panels. Specifically, we need to show the “company” name three times when editing / viewing FoodOrder record.

1. Edit <root>/SugarModules/modules/sws_FoodOrder/language/en_us.lang.php to add the two labels for new relations:

croppercapture36.jpg

2. Edit <root>/SugarModules/modules/sws_FoodOrder/metadata/detailviewdef.php and <root>/SugarModules/modules/sws_FoodOrder/metadata/editviewdef.php to add the two labels for new relations:
croppercapture37.jpg

Step 9: Zip Modules and Install

Simply zip up the module directories starting at <root>. Then install using SugarCRM Module Loader

The FoodOrder edit / create form looks like this:

croppercapture39.jpg

Unresolved Issues

The following are unresolved issues. There should be standard ways to deal with these issues. If somebody can point me to the right direction, I would be happy to updater this tutorial.

1.       A Company record will have three relationship lists. It would be optimal to only show the relationships that are associated with that company type.
2.       While editing a FoodOrder record, it would be optimal if only the list of “Restaurant” type companies show up in the “Restaurant” field. The same is true for “payment Gateway” and “delivery Company” field.