Question

How do I insert a data in a sub-table of a module? for example I have this module: MainModule it's table is mainmodule, and the module's sub-table is mainmodule_sub.

so I wanna know how to insert a data into mainmodule_sub using SugarBean.

For a clearer view:

the problem is most of the modules only have the main_tables and _cstm tables, and not all modules have one or two tables; so I just wanna know how to insert data into the third_table let's say for example the ProspectLists module and it has this 5 tables namely prospect_lists, prospect_lists_cstm, prospect_lists_prospect, prospect_lists_campaign, and so on..

how do I insert the data into prospect_lists_prospect?

Was it helpful?

Solution 2

As I have been through asking question from Stack to the Developers, and as of now it's either you create your own API for it to be done or make your own local call/insertion via SQL because only the main_tables and _cstm tables are connected to each ModuleAPI.

OTHER TIPS

I believe you mean a table for storing custom fields created through Studio and by default named as <module_name>_cstm. If so, one need to call $bean->custom_fields->retrieve(); to get access to. Then you can use ordinary $bean->save(); to store its values.

For example:

// Assume custom field 'account_custom_category_c' 
$bean->custom_fields->retrieve();
$bean->account_custom_category_c = 'Very Big Company';
$bean->save();

You CAN add new fields to _cstm tables manually, so not having to risk losing your existing data.

Simply add the fields via SQL and PHP using the following template (replace with whatever module you need the new field in:

ALTER TABLE <module>_cstm add COLUMN new_field_c varchar(255) NULL;

The _c at the end is important, fields in _cstm tables should end on _c.

Then, navigate to

{sugar_base_directory}/custom/Extension/modules/<module>/Ext/Vardefs

There in that directory, create a new file called sugarfield_new_field_c.php

In this newly created file, define the properties of the field that's intended to be added (note that the module name here in the $dictionary array should be singular, e.g. Prospect, NOT Prospects):

<?php
$dictionary['<module_singular>']['fields']['new_field_c']['name'] = 'new_field_c';
$dictionary['<module_singular>']['fields']['new_field_c']['vname'] = 'LBL_NEW_FIELD_C';
$dictionary['<module_singular>']['fields']['new_field_c']['type'] = 'varchar';
$dictionary['<module_singular>']['fields']['new_field_c']['enforced'] = '';
$dictionary['<module_singular>']['fields']['new_field_c']['dependency'] = '';
$dictionary['<module_singular>']['fields']['new_field_c']['required'] = false;
$dictionary['<module_singular>']['fields']['new_field_c']['massupdate'] = '0';
$dictionary['<module_singular>']['fields']['new_field_c']['default'] = '';
$dictionary['<module_singular>']['fields']['new_field_c']['no_default'] = false;
$dictionary['<module_singular>']['fields']['new_field_c']['comments'] = 'Example Vardef';
$dictionary['<module_singular>']['fields']['new_field_c']['help'] = '';
$dictionary['<module_singular>']['fields']['new_field_c']['importable'] = 'true';
$dictionary['<module_singular>']['fields']['new_field_c']['duplicate_merge'] = 'disabled';
$dictionary['<module_singular>']['fields']['new_field_c']['duplicate_merge_dom_value'] = 0;
$dictionary['<module_singular>']['fields']['new_field_c']['audited'] = false;
$dictionary['<module_singular>']['fields']['new_field_c']['reportable'] = true;
$dictionary['<module_singular>']['fields']['new_field_c']['unified_search'] = false;
$dictionary['<module_singular>']['fields']['new_field_c']['merge_filter'] = 'disabled';
$dictionary['<module_singular>']['fields']['new_field_c']['calculated'] = false;
$dictionary['<module_singular>']['fields']['new_field_c']['len'] = '255';
$dictionary['<module_singular>']['fields']['new_field_c']['size'] = '20';
$dictionary['<module_singular>']['fields']['new_field_c']['id'] = 'new_field_c';
$dictionary['<module_singular>']['fields']['new_field_c']['custom_module'] = '<module>';
$dictionary['<module_singular>']['fields']['new_field_c']['source'] = 'custom_fields';
?>

Then, insert a corresponding record in table fields_meta_data that will trigger the comparison process, i.e. make SugarCRM aware of this new field:

INSERT INTO fields_meta_data (id, name, vname, comments, custom_module, type, len, required, deleted, audited, massupdate, duplicate_merge, reportable, importable) VALUES ('<module>new_field_c', 'new_field_c', 'LBL_NEW_FIELD_C', 'Example Vardef', '<module>', 'varchar', 255, 0, 0, 0, 0, 0, 1, 'true');

Once that's in place, do a repair & rebuild, and your new field is ready to use.

This will make the field compatible with SugarBean:

$bean = BeanFactory::getBean('<module>');
$bean->new_field_c = 'abcd';
$bean->save();

will recognize that field and update it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top