SugarCRM Developer Tutorial on creating many-to-many relationship in Product Catalogue Module

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

  • Fields
  • Relationships
  • 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

Product Catalog Studio for creating relationship

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

But in order to have that flexibility to create Many to Many relationships with any other module from the Product Catalog module, we need to do some code-level tweaking. Sugar facilitates an extensive developer manual to customize the platform to suit the needs of customers. These changes are called Sugar Customisations. Any developer who has basic knowledge of PHP, SQL, and Javascript can easily understand how to implement customizations in SugarCRM after practicing the cookbook exercises in the SugarCRM Developer guide.

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.

Record View of the Product Catalog

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.

./custom/modules/ProductTemplates/metadata/subpanels/default.php

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

./custom/modules/ProductTemplates/clients/base/views/subpanel-list/subpanel-list.php

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

./custom/modules/ProductTemplates/clients/base/layouts/subpanels/subpanels.php

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

Product Catalog Subpanel

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.

./custom/modules/ProductTemplates/clients/base/views/subpanel-for-relationshipname/relationshipname.php

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!!!!!!!!!

Bhea is an Advanced Partner for SugarCRM and provides services like SugarCRM Training, Consultation, Installation, Maintenance, and Deployment. Our Admin Training covers extensive sessions on Studio, Module Builder, Role Management, Team Management, and all admin basics required for SugarCRM administrator.