One of the many challenges in creating SugarCRM add-ons meant for mass distribution is adding new fields to a given installation's layouts. Simply instructing folks to go to Studio and add your key fields to each view causes so many issues. Any time you rely 100% on someone following an installation guide there is a much greater chance that...
- They never knew about the install guide
- They installed the solution, can't see where the field is, and now reached out for support
- They just insta-quit their trial and move on
- They not only quit, but leave scathing, rage-filled reviews on how nothing works
So why even take a chance and instead add those fields to the UI for them? This is another important, and fairly easy, thing that you can do on behalf of the user. Much like adding scheduler jobs, doing Repair and Rebuilds, and redirecting appropriately after installation is complete. Each manual step that you remove for a user means a higher chance of success for them and for you.
How do you add fields to a layout in SugarCRM automatically? Thankfully, the fine folks at Epicom shared a clever way to do this on installation. We've expanded on it so that it works in SugarCRM 7 and that the cache is rebuilt automatically.
I am purposely just posting the code without covering the details on what it does or why as it can be pretty intense. The key piece is the call to addField2View. Notice that this works on the old 6 version along with 7. What it is doing is adding a new panel just for your module's fields. It's just easier to do than try to position it exactly in some existing location as each installation has the chance that it has been reconfigured differently.
<?php | |
if (! defined('sugarEntry') || ! sugarEntry) die('Not A Valid Entry Point'); | |
?> | |
Please be patient as the installer finishes. | |
<br/><br/> | |
You will be automatically redirected once it has completed. | |
<?php | |
function post_install() | |
{ | |
global $sugar_version, $db, $dictionary, $current_user; | |
require_once('include/utils.php'); | |
require_once('include/utils/file_utils.php'); | |
require_once('config.php'); | |
require_once('include/MVC/Controller/SugarController.php'); | |
require_once('modules/ModuleBuilder/controller.php'); | |
require_once('modules/ModuleBuilder/parsers/ParserFactory.php'); | |
//define fields to add to the UI | |
$prospect_lists = array( | |
'mailchimp_list_name_c' => array( | |
'name' => 'mailchimp_list_name_c', | |
'label' => 'LBL_MAILCHIMP_LIST_NAME', | |
), | |
'mailchimp_default_module_c' => array( | |
'name' => 'mailchimp_default_module_c', | |
'label' => 'LBL_MAILCHIMP_DEFAULT_MODULE', | |
), | |
); | |
$layoutFields = array(); | |
foreach ($prospect_lists as $key => $field) { | |
$layoutFields[$key] = $field; | |
} | |
if(preg_match( "/^6.*/", $sugar_version)) | |
{ | |
addField2View('ProspectLists', $layoutFields, 'detailview'); | |
addField2View('ProspectLists', $layoutFields, 'editview'); | |
} | |
else | |
{ | |
addField2View('ProspectLists', $layoutFields, 'recordview'); | |
} | |
//add to database as we are using Vardefs Ext instead of custom_fields in manifest due to: https://community.sugarcrm.com/sugarcrm/topics/custom_utils_in_sugarcrm_7_1_5_not_loading | |
$field_defs = array(); | |
/** | |
Have to redefine the field here for the repair to work | |
*/ | |
$field_defs['mailchimp_list_name_c'] = array( | |
'name' => 'mailchimp_list_name_c', | |
'label' => 'LBL_MAILCHIMP_LIST_NAME', | |
'type' => 'enum', | |
'function' => 'getMailChimpLists', | |
'function_bean' => 'SugarChimp', | |
'len' => 255, | |
); | |
/** | |
if you edit this definition, also change /extensions/custom/modules/ProspectLists/Vardefs/SugarChimp.php | |
*/ | |
$field_defs['mailchimp_default_module_c'] = array( | |
'name' => 'mailchimp_default_module_c', | |
'label' => 'LBL_MAILCHIMP_DEFAULT_MODULE', | |
'type' => 'enum', | |
'function' => 'getMailChimpDefaultModules', | |
'function_bean' => 'SugarChimp', | |
'len' => 255, | |
); | |
$options = $db->getOptions(); | |
$options['skip_index_rebuild'] = true; | |
$db->setOptions($options); | |
$db->repairTableParams('prospect_lists', $field_defs, null, true); | |
if(preg_match( "/^6.*/", $sugar_version)) | |
{ | |
// add code here to repair/rebuild automatically or have the user do so manually | |
} | |
else | |
{ | |
//now repair the ServiceDictionary.rest.php | |
$old_user = $GLOBALS['current_user']; | |
$user = new User(); | |
$GLOBALS['current_user'] = $user->getSystemUser(); | |
$_REQUEST['repair_silent']=1; | |
$rc = new RepairAndClear(); | |
$actions = array( | |
'clearAll', | |
'clearMetadataAPICache', | |
); | |
$rc->repairAndClearAll($actions,array('ProspectLists'),true,false); | |
$rc->clearAdditionalCaches(); | |
$GLOBALS['current_user'] = $old_user; | |
} | |
if(preg_match( "/^6.*/", $sugar_version)) { | |
echo " | |
<script> | |
document.location = 'index.php?module=SugarChimp&action=license'; | |
</script>" | |
; | |
} else { | |
echo " | |
<script> | |
var app = window.parent.SUGAR.App; | |
app.router.navigate('#bwc/index.php?module=SugarChimp&action=license', {trigger:true}); | |
</script>" | |
; | |
} | |
} | |
function addField2View ($module, $layoutFields, $view) { | |
echo "<br /><b> Updating module $module view $view with custom fields.</b><br />"; | |
$parser = ParserFactory::getParser($view, $module); | |
if (!$parser) { | |
$GLOBALS['log']->fatal("No parser found for $module | $view"); | |
} | |
$row = $column = 0; | |
foreach ($layoutFields as $field => $field_def) { | |
$parser->_viewdefs['panels']['LBL_SUGARCHIMP'][$row][$column] = $field_def; | |
echo ("<strong>$field</strong> field added to $module $view View.<br />"); | |
$column = ($column > 0) ? 0 : 1; | |
if($column === 0) $row++; | |
} | |
$parser->handleSave(false); | |
} |
You can even have fields removed during uninstall. In the example below we just remove the newly created panel that is meant just for this module.
<?php | |
if (! defined('sugarEntry') || ! sugarEntry) die('Not A Valid Entry Point'); | |
?> | |
Cleaning up some configurations... | |
<?php | |
post_uninstall(); //not called automatically | |
function post_uninstall() | |
{ | |
global $db, $sugar_version, $current_user; | |
require_once('include/utils.php'); | |
require_once('include/utils/file_utils.php'); | |
require_once('config.php'); | |
require_once('include/MVC/Controller/SugarController.php'); | |
require_once('modules/ModuleBuilder/controller.php'); | |
require_once('modules/ModuleBuilder/parsers/ParserFactory.php'); | |
//remove sugarchimp panel from views | |
if(preg_match( "/^6.*/", $sugar_version)) | |
{ | |
removeViewPanel('ProspectLists','detailview'); | |
removeViewPanel('ProspectLists','editview'); | |
} | |
else | |
{ | |
removeViewPanel('ProspectLists','recordview'); | |
} | |
if(preg_match( "/^6.*/", $sugar_version)) | |
{ | |
// add code here to repair/rebuild automatically or have the user do so manually | |
} | |
else | |
{ | |
//now repair the ServiceDictionary.rest.php | |
$old_user = $GLOBALS['current_user']; | |
$user = new User(); | |
$GLOBALS['current_user'] = $user->getSystemUser(); | |
$_REQUEST['repair_silent']=1; | |
$rc = new RepairAndClear(); | |
$actions = array( | |
'clearAll', | |
//'rebuildExtensions', //if not doing a clearAll | |
'clearMetadataAPICache', | |
//'rebuildExtensions' | |
); | |
$rc->repairAndClearAll($actions,array('ProspectLists'),true,false); | |
$rc->clearAdditionalCaches(); | |
$GLOBALS['current_user'] = $old_user; | |
} | |
echo "SugarChimp configurations have been removed."; | |
} | |
function removeViewPanel ($module, $view) { | |
echo "<br /><b> Removing module $module view $view with custom fields.</b><br />"; | |
$parser = ParserFactory::getParser($view, $module); | |
if (!$parser) { | |
$GLOBALS['log']->fatal("No parser found for $module | $view"); | |
} | |
unset($parser->_viewdefs['panels']['LBL_SUGARCHIMP']); | |
$parser->handleSave(false); | |
} |
With a little extra work you can save both your users and yourself a ton of time. Not only that, your sales are much, much more likely to convert.
-
Scoring / Rating field module
**It has never been easier to Score Your Data** Add rating fields to any SugarCRM module right from within Studio. No coding required ! Choose the maximum number of stars to display, allow (or not) ha... -
NetSuite Integration - SYNC by Commercient
FEATUREDYour ERP Accounting data is integrated with your SugarCRM when you use Commercient’s SYNC app. SYNC Accounts, Contacts, Invoices, Sales Orders, Products, Pricing, Inventory, and more! -
Messaging for Sugar (SMS) by Faye
FEATUREDMessaging for Sugar (SMS) by Faye is a unique module for Sugar that allows users to send and receive SMS Messages directly from their CRM platform. Messaging for Sugar you can send and receive texts, ... - Show more addons