SugarCRM Developer Tutorial on creating many-to-many relationship in Product Catalogue Module
SugarCRM Module Relationships
SugarCRM has few developer tools which makes the admin user’s life simpler. Sugar has one of the most powerful developer tools called studio which helps admin users create the below things
- And modify the layouts.
As a standard feature admin can create 3 types of relationships between two modules namely,
- One to One relationship
- One to Many relationship
- Many to Many relationship
Sugar has explained in a detailed manner about SugarCRM Standard Relationships Options
The changes we can do using these tools are called configuration changes to the Sugar application. Any trained admin or a tech-savvy business user can do these kinds of changes with good training.
Note: SugarCRM offers regular training sessions for administrators. You can access the upcoming programs in Sugarclub.
Standard Product Catalog Module Relationships
We can create Many to Many relationships among most of the modules using Studio. But for the Product Catalog module studio does not allow admins to create Many to Many relationship from studio
Please see the below screenshot. We don’t have a mechanism to create Many to Many relationships through the studio in the Product Catalog module. In the screenshot, we can see that there is no Many to Many relationship options available.
Product Bundles and Parent-Child Products Customisations
There are often business cases where we need to create bundled products and child products in SugarCRM. For creating the As part of this use case, we should be able to create one to Many / Many to Many relationship with the product catalog module with other modules.
Step by Step Guide to create “many-to-many” relationship in Product Catalog Module
Let us learn step by step process on how to create Many to Many relationship from the studio by adding the small snippets of code in the custom folder in an upgrade safe manner.
In order to achieve this, we have to do the below 4 steps.
Step – 1
In this step let us extend the core defaults.php file to the custom path. This code helps to create the subpanel under the Product Catalog module.
Please add the below snippet of code in the mentioned path.
<?php $module_name = 'ProductTemplates'; $subpanel_layout = array( 'top_buttons' => array( array( 'widget_class' => 'SubPanelTopCreateButton' ) , array( 'widget_class' => 'SubPanelTopSelectButton', 'popup_module' => $module_name ) , ) , 'where' => '', 'list_fields' => array( 'name' => array( 'vname' => 'LBL_NAME', 'widget_class' => 'SubPanelDetailViewLink', 'width' => '45%', ) , 'date_modified' => array( 'vname' => 'LBL_DATE_MODIFIED', 'width' => '45%', ) , 'edit_button' => array( 'vname' => 'LBL_EDIT_BUTTON', 'widget_class' => 'SubPanelEditButton', 'module' => $module_name, 'width' => '4%', ) , 'remove_button' => array( 'vname' => 'LBL_REMOVE', 'widget_class' => 'SubPanelRemoveButton', 'module' => $module_name, 'width' => '5%', ) , ) , ); ?>
Step – 2
In this step let us extend the subpanel-list to the custom path. This file is used to define the default fields that appear by default when the subpanel is created.
<?php $viewdefs['ProductTemplates']['base']['view']['subpanel-list'] = array( 'panels' => array( array( 'name' => 'panel_header', 'label' => 'LBL_PANEL_1', 'fields' => array( array( 'label' => 'LBL_NAME', 'enabled' => true, 'default' => true, 'name' => 'name', 'link' => true, ) , array( 'label' => 'LBL_DATE_MODIFIED', 'enabled' => true, 'default' => true, 'name' => 'date_modified', ) , ) , ) , ) , 'orderBy' => array( 'field' => 'date_modified', 'direction' => 'desc', ) , );
Step – 3
In this step let us add the code in the layouts path. This file is used to render the actual subpanel widget.
<?php $viewdefs['ProductTemplates']['base']['layout']['subpanels'] = array( 'components' => array(), );
After adding these 3 files in the paths mentioned, please give and make sure that your SugarCRM root directory has proper ownership and permissions. Once it is done, please go to the admin section and perform the Quick Repair and Rebuild.
Once the Repair is completely done we can go to the studio and check whether you are able to see the Many to Many relationship option in the Product Catalog module or not.
Hurray !!! Now you will be able to see the ability to create Many to Many relationship in the Product Catalog module with any other stock/custom module. Please see the below screenshot.
We are not done yet. Now we can try creating the Many to Many Relationship from the Product Catalog module with any module. You will be able to create it.
Now go to the related module which module you have selected as a secondary module while creating the M-M relationship. Try arranging the layout for the Product Catalog module under the Subpanels section of the related module.
The user can easily drag and drop the fields from the Hidden section to the Default section in the layout and do Save & Deploy.
Even after Save & Deploy you will not be able to see the fields in the layout. Don’t worry here, we need to do one more small thing. When we have done the layout arrangement it creates a file in the below path with the relationship name.
Step – 4
When we are creating the relationship between two modules it creates the file in the below-mentioned path. The relationship name varies based on the secondary module we have selected.
The code in this path is used to render the fields in the actual subpanel.
Please add the below code after this parameter ‘type’ => ‘subpanel-list’,
'panels' => array ( 0 => array ( 'name' => 'panel_header', 'label' => 'LBL_PANEL_1', 'fields' => array ( 0 => array ( 'label' => 'LBL_NAME', 'enabled' => true, 'default' => true, 'name' => 'name', 'link' => true, ), ), ), ), 'orderBy' => array ( 'field' => 'date_modified', 'direction' => 'desc', ),
Once added to this code, give ownership and permissions to the Sugar Root Folder. Go to the Admin section and perform the Quick and Repair Rebuild. Once done we are all set. Go to the studio and arrange the Product Catalog module layout under the related module as per your needs.
That’s all we are done!!!!!!!!!