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:
- Supplied by Restaurant
- Paid using Payment Gateway
- 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
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.
Step 3: Create a One-to-Many Relationship using Module Builder
Add a relationship to the company.
Step 4: Publish the Module Files
This will create a zip file with the following hierarchy.
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:
- Add ‘LBL_RESTAURANT ‘ => ‘Restaurant’ in <root>/SugarModules/modules/sws_FoodOrder/language/en_us.php
- 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.
3. Add field sws_company_rc_company_type in the table description sws_company_foodorder_c.
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.
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.
6. Rename relationsip in <root>/SugarModules/relationships/vardefs/sws_Company.php
7. Also rename in <root>/SugarModules/relationships/vardefs/sws_FoodOrder.php
8. Rename in <root>/SugarModules/relationships/layoutdefs/sws_Company.php
9. Rename in <root>/SugarModules/relationships/language/sws_Company.php and <root>/SugarModules/relationships/language/sws_FoodOrder.
Step 7: Add New Relationships
Now we are ready to add new relationships. We will again edit the same files as in previous step.
- Add new relationship in <root>/SugarModules/relationships/relationships/sws_company_sws_foodorderMetaData.php
2. Edit <root>/SugarModules/relationships/vardefs/sws_Company.php
3. Edit <root>/SugarModules/relationships/vardefs/sws_FoodOrder.php
4. Edit Edit <root>/SugarModules/relationships/layoutdefs/sws_Company.php.
5. Finally, edit the Edit <root>/SugarModules/relationships/language /sws_Company.php and <root>/SugarModules/relationships/language /sws_FoodOrder.php files
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:
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:

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:
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.

























