id
stringlengths
14
16
text
stringlengths
33
5.27k
source
stringlengths
105
270
e4c2a35f86d2-8
} callback(null, fields, errors); }, _doValidateEmail: function(fields, errors, callback) { //validate email requirements if (_.isEmpty(this.model.get('email'))) { errors['email'] = errors['email'] || {}; errors['email'].required = true; } callback(null, fields, errors); }, }) Once the files are in place, navigate to Admin > Repair > Quick Repair and Rebuild. More information on displaying custom error messages can be found in the Error Messages section. Method 2: Overriding the RecordView and CreateView Layouts
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html
e4c2a35f86d2-9
Method 2: Overriding the RecordView and CreateView Layouts Another method for defining your own custom validation is to override a module's record and create layouts to append a new view with your logic. The benefits of this method are that you can use the single view to house the validation logic, however, this means that you will have to override the layout. When overriding a layout, verify that the layout has not changed when upgrading your instance. Once the layouts are overridden, define the validation check and use the model.addValidationTask method to append the function to the save validation.  First, create the custom view. For the accounts example, create the view validate-account: ./custom/modules/Accounts/clients/base/views/validate-account/validate-account.js  ({ className:"hidden", _render: function() {
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html
e4c2a35f86d2-10
({ className:"hidden", _render: function() { //No-op, Do nothing here }, bindDataChange: function() { //add validation tasks this.model.addValidationTask('check_account_type', _.bind(this._doValidateCheckType, this)); this.model.addValidationTask('check_email', _.bind(this._doValidateEmail, this)); }, _doValidateCheckType: function(fields, errors, callback) { //validate type requirements if (this.model.get('account_type') == 'Customer' && _.isEmpty(this.model.get('phone_office'))) { errors['phone_office'] = errors['phone_office'] || {};
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html
e4c2a35f86d2-11
errors['phone_office'] = errors['phone_office'] || {}; errors['phone_office'].required = true; } callback(null, fields, errors); }, _doValidateEmail: function(fields, errors, callback) { //validate email requirements if (_.isEmpty(this.model.get('email'))) { errors['email'] = errors['email'] || {}; errors['email'].required = true; } callback(null, fields, errors); }, })
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html
e4c2a35f86d2-12
} callback(null, fields, errors); }, }) More information on displaying custom error messages can be found in the Error Messages section. Next, depending on the selected module, duplicate its create layout to the modules custom folder to handle record creation. In our Accounts example, we have an existing ./modules/Accounts/clients/base/layouts/create/create.php file so we need to duplicate this file to ./custom/modules/Accounts/clients/base/layouts/create/create.php. After this has been completed, append the new custom view to the components. append: array( 'view' => 'validate-account', ), As shown below: ./custom/modules/Accounts/clients/base/layouts/create/create.php <?php
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html
e4c2a35f86d2-13
<?php $viewdefs['Accounts']['base']['layout']['create'] = array( 'components' => array( array( 'layout' => array( 'components' => array( array( 'layout' => array( 'components' => array( array( 'view' => 'create', ), array( 'view' => 'validate-account', ), ), 'type' => 'simple', 'name' => 'main-pane', 'span' => 8, ), ), array(
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html
e4c2a35f86d2-14
), ), array( 'layout' => array( 'components' => array(), 'type' => 'simple', 'name' => 'side-pane', 'span' => 4, ), ), array( 'layout' => array( 'components' => array( array( 'view' => array ( 'name' => 'dnb-account-create', 'label' => 'DNB Account Create', ), 'width' => 12, ), ),
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html
e4c2a35f86d2-15
'width' => 12, ), ), 'type' => 'simple', 'name' => 'dashboard-pane', 'span' => 4, ), ), array( 'layout' => array( 'components' => array( array( 'layout' => 'preview', ), ), 'type' => 'simple', 'name' => 'preview-pane', 'span' => 8, ), ), ), 'type' => 'default', 'name' => 'sidebar', 'span' => 12,
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html
e4c2a35f86d2-16
'name' => 'sidebar', 'span' => 12, ), ), ), 'type' => 'simple', 'name' => 'base', 'span' => 12, ); Lastly, depending on the selected module, duplicate its record layout to the modules custom folder to handle editing a record. In the accounts example, we do not have an existing ./modules/Accounts/clients/base/layouts/record/record.php file so we duplicated the core ./clients/base/layouts/record/record.php to ./custom/modules/Accounts/clients/base/layouts/record/record.php. Since we are copying from the ./clients/ core directory, modify: $viewdefs['base']['layout']['record'] = array( ... ); To:
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html
e4c2a35f86d2-17
... ); To: $viewdefs['Accounts']['base']['layout']['record'] = array( ... ); After this has been completed, append the new custom view to the components: array( 'view' => 'validate-account', ), The resulting file is shown below: ./custom/modules/Accounts/clients/base/layouts/record/record.php <?php $viewdefs['Accounts']['base']['layout']['record'] = array( 'components' => array( array( 'layout' => array( 'components' => array( array( 'layout' => array( 'components' => array( array( 'view' => 'record',
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html
e4c2a35f86d2-18
array( 'view' => 'record', 'primary' => true, ), array( 'view' => 'validate-account', ), array( 'layout' => 'extra-info', ), array( 'layout' => array( 'name' => 'filterpanel', 'span' => 12, 'last_state' => array( 'id' => 'record-filterpanel', 'defaults' => array( 'toggle-view' => 'subpanels', ), ), 'availableToggles' => array( array( 'name' => 'subpanels',
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html
e4c2a35f86d2-19
array( 'name' => 'subpanels', 'icon' => 'icon-table', 'label' => 'LBL_DATA_VIEW', ), array( 'name' => 'list', 'icon' => 'icon-table', 'label' => 'LBL_LISTVIEW', ), array( 'name' => 'activitystream', 'icon' => 'icon-th-list', 'label' => 'LBL_ACTIVITY_STREAM', ), ), 'components' => array( array( 'layout' => 'filter',
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html
e4c2a35f86d2-20
array( 'layout' => 'filter', 'targetEl' => '.filter', 'position' => 'prepend' ), array( 'view' => 'filter-rows', "targetEl" => '.filter-options' ), array( 'view' => 'filter-actions', "targetEl" => '.filter-options' ), array( 'layout' => 'activitystream', 'context' => array( 'module' => 'Activities', ), ), array( 'layout' => 'subpanels', ),
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html
e4c2a35f86d2-21
'layout' => 'subpanels', ), ), ), ), ), 'type' => 'simple', 'name' => 'main-pane', 'span' => 8, ), ), array( 'layout' => array( 'components' => array( array( 'layout' => 'sidebar', ), ), 'type' => 'simple', 'name' => 'side-pane', 'span' => 4, ), ), array( 'layout' => array( 'components' => array( array( 'layout' => array(
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html
e4c2a35f86d2-22
array( 'layout' => array( 'type' => 'dashboard', 'last_state' => array( 'id' => 'last-visit', ) ), 'context' => array( 'forceNew' => true, 'module' => 'Home', ), ), ), 'type' => 'simple', 'name' => 'dashboard-pane', 'span' => 4, ), ), array( 'layout' => array( 'components' => array( array( 'layout' => 'preview', ), ), 'type' => 'simple',
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html
e4c2a35f86d2-23
), ), 'type' => 'simple', 'name' => 'preview-pane', 'span' => 8, ), ), ), 'type' => 'default', 'name' => 'sidebar', 'span' => 12, ), ), ), 'type' => 'simple', 'name' => 'base', 'span' => 12, ); Once the files are in place, navigate to Admin > Repair > Quick Repair and Rebuild.  Last modified: 2023-02-03 21:04:03
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Adding_Field_Validation_to_the_Record_View/index.html
4bb124b8b1c5-0
Logic Hooks These pages demonstrate some common examples of working with logic hooks in Sugar. TopicsComparing Bean Properties Between Logic HooksHow to compare the properties of a bean between the before_save and after_save logic hooksPreventing Infinite Loops with Logic HooksHow to add a check to a logic hook to eliminate perpetual looping Last modified: 2023-02-03 21:04:03
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Logic_Hooks/index.html
14dfe0ae794e-0
Preventing Infinite Loops with Logic Hooks Overview Infinite loops often happen when a logic hook calls save on a bean in a scenario that triggers the same hook again. This example shows how to add a check to a logic hook to eliminate perpetual looping. Saving in an After Save Hook Infinite loops can sometimes happen when you have a need to update a record after it has been run through the workflow process in the after_save hook. Here is an example of a looping hook: ./custom/modules/Accounts/logic_hooks.php <?php $hook_version = 1; $hook_array = Array(); $hook_array['after_save'] = Array(); $hook_array['after_save'][] = Array( 1, 'Update Account Record', 'custom/modules/Accounts/Accounts_Hook.php',
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Logic_Hooks/Preventing_Infinite_Loops_with_Logic_Hooks/index.html
14dfe0ae794e-1
'custom/modules/Accounts/Accounts_Hook.php', 'Accounts_Hook', 'update_self' ); ./custom/modules/Accounts/Accounts_Hook.php <?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class Accounts_Hook { function update_self($bean, $event, $arguments) { //generic condition if ($bean->account_type == 'Analyst') { //update $bean->industry = 'Banking'; $bean->save(); } } }
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Logic_Hooks/Preventing_Infinite_Loops_with_Logic_Hooks/index.html
14dfe0ae794e-2
$bean->save(); } } } In the example above, there is a condition that, when met, will trigger the update of a field on the record. This will cause an infinite loop because calling the save() method will trigger once during the before_save hook and then again during the after_save hook. The solution to this problem is to add a new property on the bean to ignore any unneeded saves. For this example, we will name the property "ignore_update_c" and add a check to the logic hook to eliminate the perpetual loop. An example of this is shown below: ./custom/modules/Accounts/Accounts_Hook.php <?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class Accounts_Hook { function update_self($bean, $event, $arguments) {
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Logic_Hooks/Preventing_Infinite_Loops_with_Logic_Hooks/index.html
14dfe0ae794e-3
function update_self($bean, $event, $arguments) { //loop prevention check if (!isset($bean->ignore_update_c) || $bean->ignore_update_c === false) { //generic condition if ($bean->account_type == 'Analyst') { //update $bean->ignore_update_c = true; $bean->industry = 'Banking'; $bean->save(); } } } } Related Record Save Loops Sometimes logic hooks on two separate but related modules can cause an infinite loop. The problem arises when the two modules have logic hooks that update one another. This is often done when wanting to keep a field in sync between two modules. The example below will demonstrate this behavior on the accounts:contacts relationship:
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Logic_Hooks/Preventing_Infinite_Loops_with_Logic_Hooks/index.html
14dfe0ae794e-4
./custom/modules/Accounts/logic_hooks.php <?php $hook_version = 1; $hook_array = Array(); $hook_array['before_save'] = Array(); $hook_array['before_save'][] = Array( 1, 'Handling associated Contacts records', 'custom/modules/Accounts/Accounts_Hook.php', 'Accounts_Hook', 'update_contacts' ); ./custom/modules/Accounts/Accounts_Hook.php <?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class Accounts_Hook { function update_contacts($bean, $event, $arguments) { //if relationship is loaded
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Logic_Hooks/Preventing_Infinite_Loops_with_Logic_Hooks/index.html
14dfe0ae794e-5
{ //if relationship is loaded if ($bean->load_relationship('contacts')) { //fetch related beans $relatedContacts = $bean->contacts->getBeans(); foreach ($relatedContacts as $relatedContact) { //perform any other contact logic $relatedContact->save(); } } } } The above example will loop through all contacts related to the account and save them. At this point, the save will then trigger our contacts logic hook shown below: ./custom/modules/Contacts/logic_hooks.php <?php $hook_version = 1; $hook_array = Array(); $hook_array['before_save'] = Array(); $hook_array['before_save'][] = Array( 1,
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Logic_Hooks/Preventing_Infinite_Loops_with_Logic_Hooks/index.html
14dfe0ae794e-6
$hook_array['before_save'][] = Array( 1, 'Handling associated Accounts records', 'custom/modules/Contacts/Contacts_Hook.php', 'Contacts_Hook', 'update_account' ); ./custom/modules/Contacts/Contacts_Hook.php <?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class Contacts_Hook { function update_account($bean, $event, $arguments) { //if relationship is loaded if ($bean->load_relationship('accounts')) { //fetch related beans $relatedAccounts = $bean->accounts->getBeans(); $parentAccount = false;
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Logic_Hooks/Preventing_Infinite_Loops_with_Logic_Hooks/index.html
14dfe0ae794e-7
$parentAccount = false; if (!empty($relatedAccounts)) { //order the results reset($relatedAccounts); //first record in the list is the parent $parentAccount = current($relatedAccounts); } if ($parentAccount !== false && is_object($parentAccount)) { //perform any other account logic $parentAccount->save(); } } } }
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Logic_Hooks/Preventing_Infinite_Loops_with_Logic_Hooks/index.html
14dfe0ae794e-8
$parentAccount->save(); } } } } These two logic hooks will continuously trigger one another in this scenario until you run into a max_execution or memory_limit error. The solution to this problem is to add a new property on the bean to ignore any unneeded saves. In our example, we will name this property "ignore_update_c" and add a check to our logic hook to eliminate the perpetual loop. The following code snippets are copies of the two classes with the ignore_update_c property added. ./custom/modules/Accounts/Accounts_Hook.php <?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class Accounts_Hook { function update_contacts($bean, $event, $arguments) { //if relationship is loaded
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Logic_Hooks/Preventing_Infinite_Loops_with_Logic_Hooks/index.html
14dfe0ae794e-9
{ //if relationship is loaded if ($bean->load_relationship('contacts')) { //fetch related beans $relatedContacts = $bean->contacts->getBeans(); foreach ($relatedContacts as $relatedContact) { //check if the bean's ignore_update_c attribute is not set if (!isset($bean->ignore_update_c) || $bean->ignore_update_c === false) { //set the ignore update attribute $relatedContact->ignore_update_c = true; //perform any other contact logic $relatedContact->save(); } } } } } ./custom/modules/Contacts/Contacts_Hook.php <?php
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Logic_Hooks/Preventing_Infinite_Loops_with_Logic_Hooks/index.html
14dfe0ae794e-10
./custom/modules/Contacts/Contacts_Hook.php <?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class Contacts_Hook { function update_account($bean, $event, $arguments) { //if relationship is loaded if ($bean->load_relationship('accounts')) { //fetch related beans $relatedAccounts = $bean->accounts->getBeans(); $parentAccount = false; if (!empty($relatedAccounts)) { //order the results reset($relatedAccounts); //first record in the list is the parent $parentAccount = current($relatedAccounts); } if ($parentAccount !== false && is_object($parentAccount)) {
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Logic_Hooks/Preventing_Infinite_Loops_with_Logic_Hooks/index.html
14dfe0ae794e-11
if ($parentAccount !== false && is_object($parentAccount)) { //check if the bean's ignore_update_c element is set if (!isset($bean->ignore_update_c) || $bean->ignore_update_c === false) { //set the ignore update attribute $parentAccount->ignore_update_c = true; //perform any other account logic $parentAccount->save(); } } } } } Last modified: 2023-02-03 21:04:03
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Logic_Hooks/Preventing_Infinite_Loops_with_Logic_Hooks/index.html
3960d1f49d3a-0
Comparing Bean Properties Between Logic Hooks Overview How to compare the properties of a bean between the before_save and after_save logic hooks Storing and Comparing Values When working with a bean in the after_save logic hook, you may notice that the after_save fetched rows no longer match the before_save fetched rows. If your after_save logic needs to be able to compare values that were in the before_save, you can use the following example to help you store and use the values. ./custom/modules/<module>/logic_hooks.php <?php $hook_version = 1; $hook_array = Array(); $hook_array['before_save'] = Array(); $hook_array['before_save'][] = Array( 1, 'Store values',
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Logic_Hooks/Comparing_Bean_Properties_Between_Logic_Hooks/index.html
3960d1f49d3a-1
1, 'Store values', 'custom/modules/<module>/My_Logic_Hooks.php', 'My_Logic_Hooks', 'before_save_method' ); $hook_array['after_save'] = Array(); $hook_array['after_save'][] = Array( 1, 'Retrieve and compare values', 'custom/modules/<module>/My_Logic_Hooks.php', 'My_Logic_Hooks', 'after_save_method' ); ?> ./custom/modules/<module>/My_Logic_Hooks.php <?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class My_Logic_Hooks {
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Logic_Hooks/Comparing_Bean_Properties_Between_Logic_Hooks/index.html
3960d1f49d3a-2
class My_Logic_Hooks { function before_save_method($bean, $event, $arguments) { //store as a new bean property $bean->stored_fetched_row_c = $bean->fetched_row; } function after_save_method($bean, $event, $arguments) { //check if a fields value has changed if ( isset($bean->stored_fetched_row_c) && $bean->stored_fetched_row_c['field'] != $bean->field ) { //execute logic } } } ?> Last modified: 2023-02-03 21:04:03
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Logic_Hooks/Comparing_Bean_Properties_Between_Logic_Hooks/index.html
ff8e8d8e08db-0
Creating Custom Field Types Overview In this example, we create a custom field type called "Highlightfield", which will mimic the base text field type with the added feature that the displayed text for the field will be highlighted in a color chosen when the field is created in Studio. This example requires the following steps, which are covered in the sections and subsections below: Creating the JavaScript Controller Defining the Handlebar Templates Adding the Field Type to Studio Enabling Search and Filtering Note: Custom field types are not currently functional for use in Reports and will break Report Wizard. Naming a Custom Field Type in Sugar When choosing a name for a custom field type, keep in mind the following rules: The first letter of a custom field type's name must be capitalized. All subsequent letters in the field type's name must be lowercase.
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-1
All subsequent letters in the field type's name must be lowercase. A field type's name cannot contain non-letter characters such as 0-9, hyphens, or dashes. Therefore, in this example, the field type "Highlightfield" cannot be called HighLightField or highlightfield. Creating the JavaScript Controller First, create a controller file. Since we are starting from scratch, we need to extend the base field template. To accomplish this, create ./custom/clients/base/fields/Highlightfield/Highlightfield.js. This file will contain the JavaScript needed to render the field and format the values. By default, all fields extend the base template and do not require you to add the extendsFrom property. An example template is shown below:
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-2
./custom/clients/base/fields/Highlightfield/Highlightfield.js ({ /** * Called when initializing the field * @param options */ initialize: function(options) { this._super('initialize', [options]); }, /** * Called when rendering the field * @private */ _render: function() { this._super('_render'); }, /** * Called when formatting the value for display * @param value */ format: function(value) { return this._super('format', [value]); }, /** * Called when unformatting the value for storage * @param value */ unformat: function(value) { return this._super('unformat', [value]);
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-3
return this._super('unformat', [value]); } }) Note: This controller file example contains methods for initialize, _render, format, and unformat. These are shown for your ease of use but are not necessarily needed for this customization. For example, you could choose to create an empty controller consisting of nothing other than ({}) and the field would render as expected in this example. Defining the Handlebar Templates Next, define the handlebar templates. The templates will nearly match the base template found in ./clients/base/fields/base/ with the minor difference of an additional attribute of style="background:{{def.backcolor}}; color:{{def.textcolor}}" for the detail and list templates. Detail View
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-4
Detail View The first template to define is the Sidecar detail view. This template handles the display of data on the record view. ./custom/clients/base/fields/Highlightfield/detail.hbs {{! The data for field colors are passed into the handlebars template through the def array. For this example, the def.backcolor and def.textcolor properties. These indexes are defined in: ./custom/modules/DynamicFields/templates/Fields/TemplateHighlightfield.php }} {{#if value}} <div class="ellipsis_inline" data-placement="bottom" style="background:{{def.backcolor}}; color:{{def.textcolor}}"> {{value}} </div> {{/if}} Edit View
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-5
{{value}} </div> {{/if}} Edit View Now define the Sidecar edit view. This template handles the display of the field in the edit view. ./custom/clients/base/fields/Highlightfield/edit.hbs. {{! We have not made any edits to this file that differ from stock, however, we could add styling here just as we did for the detail and list templates. }} <input type="text" name="{{name}}" value="{{value}}" {{#if def.len}}maxlength="{{def.len}}"{{/if}} {{#if def.placeholder}}placeholder="{{str def.placeholder this.model.module}}"{{/if}} class="inherit-width"> <p class="help-block"> List View
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-6
<p class="help-block"> List View Finally, define the list view. This template handles the display of the custom field in list views. ./custom/clients/base/fields/Highlightfield/list.hbs  {{! The data for field colors are passed into the handlebars template through the def array. Our case being the def.backcolor and def.textcolor properties. These indexes are defined in: ./custom/modules/DynamicFields/templates/Fields/TemplateHighlightfield.php }} <div class="ellipsis_inline" data-placement="bottom" data-original-title="{{#unless value}} {{#if def.placeholder}}{{str def.placeholder this.model.module}}{{/if}}{{/unless}}{{value}}"
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-7
style="background:{{def.backcolor}}; color:{{def.textcolor}}"> {{#if def.link}} <a href="{{#if def.events}}javascript:void(0);{{else}}{{href}}{{/if}}">{{value}}</a>{{else}}{{value}} {{/if}} </div> Adding the Field Type to Studio To enable the new field type for use in Studio, define the Studio field template. This will also allow us to map any additional properties we need for the templates. For this example, map the ext1 and ext2 fields from the fields_meta_data table to the backcolor and textcolor definitions. Also, set the dbfield definition to varchar so that the correct database field type is created.
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-8
Note: We are setting "reportable" to false to avoid issues with Report Wizard. ./custom/modules/DynamicFields/templates/Fields/TemplateHighlightfield.php <?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); require_once 'modules/DynamicFields/templates/Fields/TemplateField.php'; class TemplateHighlightfield extends TemplateField { var $type = 'varchar'; var $supports_unified_search = true; /** * TemplateAutoincrement Constructor: Map the ext attribute fields to the relevant color properties * * References: get_field_def function below * * @returns field_type */ function __construct() {
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-9
*/ function __construct() { $this->vardef_map['ext1'] = 'backcolor'; $this->vardef_map['ext2'] = 'textcolor'; $this->vardef_map['backcolor'] = 'ext1'; $this->vardef_map['textcolor'] = 'ext2'; } //BEGIN BACKWARD COMPATIBILITY // AS 7.x does not have EditViews and DetailViews anymore these are here // for any modules in backwards compatibility mode. function get_xtpl_edit() { $name = $this->name; $returnXTPL = array(); if (!empty($this->help)) {
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-10
$returnXTPL = array(); if (!empty($this->help)) { $returnXTPL[strtoupper($this->name . '_help')] = translate($this->help, $this->bean->module_dir); } if (isset($this->bean->$name)) { $returnXTPL[$this->name] = $this->bean->$name; } else { if (empty($this->bean->id)) { $returnXTPL[$this->name] = $this->default_value; } } return $returnXTPL; } function get_xtpl_search() { if (!empty($_REQUEST[$this->name])) { return $_REQUEST[$this->name]; } }
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-11
return $_REQUEST[$this->name]; } } function get_xtpl_detail() { $name = $this->name; if (isset($this->bean->$name)) { return $this->bean->$name; } return ''; } //END BACKWARD COMPATIBILITY /** * Function: get_field_def * Description: Get the field definition attributes that are required for the Highlightfield Field * the primary reason this function is here is to set the dbType to 'varchar', * otherwise 'Highlightfield' would be used by default. * References: __construct function above * * @return Field Definition */ function get_field_def() {
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-12
*/ function get_field_def() { $def = parent::get_field_def(); //set our fields database type $def['dbType'] = 'varchar'; //set our fields to false to avoid issues with Report Wizard. $def['reportable'] = false; //set our field as custom type $def['custom_type'] = 'varchar'; //map our extension fields for colorizing the field $def['backcolor'] = !empty($this->backcolor) ? $this->backcolor : $this->ext1; $def['textcolor'] = !empty($this->textcolor) ? $this->textcolor : $this->ext2; return $def; } }
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-13
return $def; } } Note: For the custom field type, the ext1, ext2, ext3, and ext4 database fields in the fields_meta_data table offer additional property storage. Creating the Form Controller Next, set up the field's form controller. This controller will handle the field's form template in Admin > Studio > Fields and allow us to assign color values to the Smarty template. ./custom/modules/DynamicFields/templates/Fields/Forms/Highlightfield.php <?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); require_once 'custom/modules/DynamicFields/templates/Fields/TemplateHighlightfield.php'; /** * Implement get_body function to correctly populate the template for the ModuleBuilder/Studio
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-14
/** * Implement get_body function to correctly populate the template for the ModuleBuilder/Studio * Add field page. * * @param Sugar_Smarty $ss * @param array $vardef * */ function get_body(&$ss, $vardef) { global $app_list_strings, $mod_strings; $vars = $ss->get_template_vars(); $fields = $vars['module']->mbvardefs->vardefs['fields']; $fieldOptions = array(); foreach ($fields as $id => $def) { $fieldOptions[$id] = $def['name']; } $ss->assign('fieldOpts', $fieldOptions); //If there are no colors defined, use black text on // a white background
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-15
//If there are no colors defined, use black text on // a white background if (isset($vardef['backcolor'])) { $backcolor = $vardef['backcolor']; } else { $backcolor = '#ffffff'; } if (isset($vardef['textcolor'])) { $textcolor = $vardef['textcolor']; } else { $textcolor = '#000000'; } $ss->assign('BACKCOLOR', $backcolor); $ss->assign('TEXTCOLOR', $textcolor); $colorArray = $app_list_strings['highlightColors']; asort($colorArray); $ss->assign('highlightColors', $colorArray);
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-16
$ss->assign('highlightColors', $colorArray); $ss->assign('textColors', $colorArray); $ss->assign('BACKCOLORNAME', $app_list_strings['highlightColors'][$backcolor]); $ss->assign('TEXTCOLORNAME', $app_list_strings['highlightColors'][$textcolor]); return $ss->fetch('custom/modules/DynamicFields/templates/Fields/Forms/Highlightfield.tpl'); } ?> Creating the Smarty Template
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-17
} ?> Creating the Smarty Template Once the form controller is in place, create the Smarty template. The .tpl below will define the center content of the field's Studio edit view. The template includes a coreTop.tpl and coreBottom.tpl file. These files add the base field properties such as "Name" and "Display Label" that are common across all field types. This allows us to focus on the fields that are specific to our new field type. ./custom/modules/DynamicFields/templates/Fields/Forms/Highlightfield.tpl {include file="modules/DynamicFields/templates/Fields/Forms/coreTop.tpl"} <tr>
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-18
<tr> <td class='mbLBL'>{sugar_translate module="DynamicFields" label="COLUMN_TITLE_DEFAULT_VALUE"}:</td> <td> {if $hideLevel < 5} <input type='text' name='default' id='default' value='{$vardef.default}' maxlength='{$vardef.len|default:50}'> {else} <input type='hidden' id='default' name='default' value='{$vardef.default}'>{$vardef.default} {/if} </td> </tr> <tr> <td class='mbLBL'>{sugar_translate module="DynamicFields" label="COLUMN_TITLE_MAX_SIZE"}:</td>
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-19
<td> {if $hideLevel < 5} <input type='text' name='len' id='field_len' value='{$vardef.len|default:25}' onchange="forceRange(this,1,255);changeMaxLength(document.getElementById('default'),this.value);"> <input type='hidden' id="orig_len" name='orig_len' value='{$vardef.len}'> {if $action=="saveSugarField"} <input type='hidden' name='customTypeValidate' id='customTypeValidate' value='{$vardef.len|default:25}'
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-20
onchange="if (parseInt(document.getElementById('field_len').value) < parseInt(document.getElementById('orig_len').value)) return confirm(SUGAR.language.get('ModuleBuilder', 'LBL_CONFIRM_LOWER_LENGTH')); return true;"> {/if} {literal} <script> function forceRange(field, min, max) { field.value = parseInt(field.value); if (field.value == 'NaN')field.value = max; if (field.value > max) field.value = max; if (field.value < min) field.value = min; } function changeMaxLength(field, length) { field.maxLength = parseInt(length);
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-21
field.maxLength = parseInt(length); field.value = field.value.substr(0, field.maxLength); } </script> {/literal} {else} <input type='hidden' name='len' value='{$vardef.len}'>{$vardef.len} {/if} </td> </tr> <tr> <td class='mbLBL'>{sugar_translate module="DynamicFields" label="LBL_HIGHLIGHTFIELD_BACKCOLOR"}:</td> <td> {if $hideLevel < 5} {html_options name="ext1" id="ext1" selected=$BACKCOLOR options=$highlightColors} {else}
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-22
{else} <input type='hidden' id='ext1' name='ext1' value='{$BACKCOLOR}'> {$BACKCOLORNAME} {/if} </td> </tr> <tr> <td class='mbLBL'>{sugar_translate module="DynamicFields" label="LBL_HIGHLIGHTFIELD_TEXTCOLOR"}:</td> <td> {if $hideLevel < 5} {html_options name="ext2" id="ext2" selected=$TEXTCOLOR options=$highlightColors} {else} <input type='hidden' id='ext2' name='ext2' value='{$TEXTCOLOR}'> {$TEXTCOLORNAME} {/if}
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-23
{$TEXTCOLORNAME} {/if} </td> </tr> {include file="modules/DynamicFields/templates/Fields/Forms/coreBottom.tpl"} Registering the Field Type Once you have defined the Studio template, register the field as a valid field type. For this example, the field will inherit the base field type as it is a text field. In doing this, we are able to override the core functions. The most used override is the save function shown below. ./custom/include/SugarFields/Fields/Highlightfield/SugarFieldHighlightfield.php <?php require_once 'include/SugarFields/Fields/Base/SugarFieldBase.php';
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-24
require_once 'data/SugarBean.php'; class SugarFieldHighlightfield extends SugarFieldBase { //this function is called to format the field before saving. For example we could put code in here // to check spelling or to change the case of all the letters public function save(&$bean, $params, $field, $properties, $prefix = '') { $GLOBALS['log']->debug("SugarFieldHighlightfield::save() function called."); parent::save($bean, $params, $field, $properties, $prefix); } } ?> Creating Language Definitions For the new field, you must define several language extensions to ensure everything is displayed correctly. This section includes three steps: Adding the Custom Field to the Type List Creating Labels for Studio
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-25
Adding the Custom Field to the Type List Creating Labels for Studio Defining Dropdown Controls Adding the Custom Field to the Type List Define the new field type in the field types list. This will allow an Administrator to select the Highlightfield field type in Studio when creating a new field. ./custom/Extension/modules/ModuleBuilder/Ext/Language/en_us.Highlightfield.php <?php $mod_strings['fieldTypes']['Highlightfield'] = 'Highlighted Text'; Creating Labels for Studio Once the field type is defined, add the labels for the Studio template. ./custom/Extension/modules/DynamicFields/Ext/Language/en_us.Highlightfield.php <?php $mod_strings['LBL_HIGHLIGHTFIELD'] = 'Highlighted Text';
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-26
$mod_strings['LBL_HIGHLIGHTFIELD_FORMAT_HELP'] = ''; $mod_strings['LBL_HIGHLIGHTFIELD_BACKCOLOR'] = 'Background Color'; $mod_strings['LBL_HIGHLIGHTFIELD_TEXTCOLOR'] = 'Text Color'; Defining Dropdown Controls For this example, we must define dropdown lists, which will be available in Studio for an administrator to add or remove color options for the field type. ./custom/Extension/application/Ext/Language/en_us.Highlightfield.php <?php $app_strings['LBL_HIGHLIGHTFIELD_OPERATOR_CONTAINS'] = 'contains'; $app_strings['LBL_HIGHLIGHTFIELD_OPERATOR_NOT_CONTAINS'] = 'does not contain';
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-27
$app_strings['LBL_HIGHLIGHTFIELD_OPERATOR_STARTS_WITH'] = 'starts with'; $app_list_strings['highlightColors'] = array( '#0000FF' => 'Blue', '#00ffff' => 'Aqua', '#FF00FF' => 'Fuchsia', '#808080' => 'Gray', '#ffff00' => 'Olive', '#000000' => 'Black', '#800000' => 'Maroon', '#ff0000' => 'Red', '#ffA500' => 'Orange', '#ffff00' => 'Yellow', '#800080' => 'Purple', '#ffffff' => 'White', '#00ff00' => 'Lime',
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-28
'#00ff00' => 'Lime', '#008000' => 'Green', '#008080' => 'Teal', '#c0c0c0' => 'Silver', '#000080' => 'Navy' ); Enabling Search and Filtering To enable Highlightfield for searching and filtering, define the filter operators and the Sugar widget. Defining the Filter Operators The filter operators, defined in ./custom/clients/base/filters/operators/operators.php,  allow the custom field be used for searching in Sidecar listviews.  ./custom/clients/base/filters/operators/operators.php <?php require 'clients/base/filters/operators/operators.php';
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-29
require 'clients/base/filters/operators/operators.php'; $viewdefs['base']['filter']['operators']['Highlightfield'] = array( '$contains' => 'LBL_HIGHLIGHTFIELD_OPERATOR_CONTAINS', '$not_contains' => 'LBL_HIGHLIGHTFIELD_OPERATOR_NOT_CONTAINS', '$starts' => 'LBL_HIGHLIGHTFIELD_OPERATOR_STARTS_WITH', );
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-30
); Note: The labels for the filters in this example are defined in  ./custom/Extension/application/Ext/Language/en_us.Highlightfield.php. For the full list of filters in the system, you can look at ./clients/base/filters/operators/operators.php. Defining the Sugar Widget Finally, define the Sugar widget. The widget will help our field for display on Reports and subpanels in backward compatibility. It also controls how search filters are applied to our field. ./custom/include/generic/SugarWidgets/SugarWidgetFieldHighlightfield.php <?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); require_once 'include/generic/SugarWidgets/SugarWidgetFieldvarchar.php';
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-31
class SugarWidgetFieldHighlightfield extends SugarWidgetFieldVarchar { function SugarWidgetFieldText(&$layout_manager) { parent::SugarWidgetFieldVarchar($layout_manager); } function queryFilterEquals($layout_def) { return $this->reporter->db->convert($this->_get_column_select($layout_def), "text2char") . " = " . $this->reporter->db->quoted($layout_def['input_name0']); } function queryFilterNot_Equals_Str($layout_def) { $column = $this->_get_column_select($layout_def);
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-32
$column = $this->_get_column_select($layout_def); return "($column IS NULL OR " . $this->reporter->db->convert($column, "text2char") . " != " . $this->reporter->db->quoted($layout_def['input_name0']) . ")"; } function queryFilterNot_Empty($layout_def) { $column = $this->_get_column_select($layout_def); return "($column IS NOT NULL AND " . $this->reporter->db->convert($column, "length") . " > 0)"; } function queryFilterEmpty($layout_def) { $column = $this->_get_column_select($layout_def);
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
ff8e8d8e08db-33
$column = $this->_get_column_select($layout_def); return "($column IS NULL OR " . $this->reporter->db->convert($column, "length") . " = 0)"; } function displayList($layout_def) { return nl2br(parent::displayListPlain($layout_def)); } } ?> Once the files are in place, navigate to Admin > Repair > Quick Repair and Rebuild. It is also best practice to clear your browser cache before using the new field type in Studio. Last modified: 2023-03-09 22:43:32
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Creating_Custom_Fields/index.html
c9398acc7a9f-0
Modifying Layouts to Display Additional Columns Overview By default, the record view layout for each module displays two columns of fields. The number of columns to display can be customized on a per-module basis with the following steps. Resolution First, you will want to ensure your layouts are deployed in the custom directory. If you have not previously customized your layouts via Studio, go to Admin > Studio > {Module Name} > Layouts. From there, select each layout you wish to add additional columns to and click 'Save & Deploy'. This action will create a corresponding layout file under the ./custom/modules/{Module Name}/clients/base/views/record/ directory. The file will be named record.php. In your custom record.php file, locate the maxColumns value under templateMeta array, and change it to the number of columns you would like to have on screen:
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Modifying_Layouts_to_Display_Additional_Columns/index.html
c9398acc7a9f-1
'maxColumns' => '3', Once that is updated, locate the widths array to define the spacing for your each column(s). You should have a label and field entry for each column in your layout: 'widths' => array ( 0 => array ( 'label' => '10', 'field' => '30', ), 1 => array ( 'label' => '10', 'field' => '30', ), 2 => array ( 'label' => '10', 'field' => '30', ), ), Next, under the existing panels array, each entry will already have a columns property, with a value of 2. Update each of these to the number of columns you set for maxColumns : 'columns' => 3,
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Modifying_Layouts_to_Display_Additional_Columns/index.html
c9398acc7a9f-2
'columns' => 3, Once the file has been modified, you can navigate to Studio and add fields to your new column in the layout. For any rows that already contain two fields, the second field will automatically span the second and third column. Simply click the minus (-) icon to contract the field to one column and expose the new column space: After you have added the desired fields in Studio, click 'Save & Deploy' to finalize your changes Column Widths Studio does not support expanding a field beyond two column-widths. If you want a field to span the full layout (all three columns), you would need to manually update this in the custom record.php like so: array ( 'name' => 'description', 'span' => 12, )
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Modifying_Layouts_to_Display_Additional_Columns/index.html
c9398acc7a9f-3
'name' => 'description', 'span' => 12, ) Changing the span value from 8 to 12. After saving the changes on the file, you would then run a Quick Repair and Rebuild. However, any updates in Studio will revert the span back to 8.   Last modified: 2023-02-03 21:04:03
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Modifying_Layouts_to_Display_Additional_Columns/index.html
c7f14063ac9a-0
Module Loadable Packages Examples working with the module loadable packages.
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/index.html
c7f14063ac9a-1
TopicsCreating an Installable Package for a Logic HookThese examples will demonstrate how to create module loadable packages for logic hooks based on different scenarios.Creating an Installable Package That Copies FilesThis is an overview of how to create a module loadable package that will copy files into your instance of Sugar. This is most helpful when your instance is hosted in Sugar's cloud environment or by a third party. For more details on the $manifest or $installdef options, you can visit the Introduction to the Manifest. This example package can be downloaded here.Creating an Installable Package that Creates New FieldsThis is an overview of how to create a Module Loader package that will install custom fields to a module. This example will install a set of fields to the accounts module. The full package is downloadable here for your reference. For more details on the $manifest or $installdef options, you can visit the Introduction to the Manifest File.Removing Files with
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/index.html
c7f14063ac9a-2
options, you can visit the Introduction to the Manifest File.Removing Files with an Installable PackageThis is an overview of how to create a module loadable package that will remove files from the custom directory that may have been left over from a previous package installation or legacy versions of your code customization.
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/index.html
c7f14063ac9a-3
Last modified: 2023-02-03 21:04:03
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/index.html
f2c71ef3616e-0
Creating an Installable Package for a Logic Hook Overview These examples will demonstrate how to create module loadable packages for logic hooks based on different scenarios. Logic Hook File vs. Logic Hook Definition No matter the deployment method, all logic hooks require being registered (meaning they are attached to the $hook_array array), defining the logic hook event, module, file to load, and class and function to run for that logic hook event. For all deployment options, the logic hook definition points to a file where the class and function to run is located, and this file must be copied to the instance via the copy installdefs of the manifest. The bulk of this article will focus on the various options for registering the logic hook definition via the package manifest. But all deployment strategies will assume the same logic hook definition, the same logic hook file, and will use the same copy array to move the logic hook file into the instance.
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_for_a_Logic_Hook/index.html
f2c71ef3616e-1
Note: More information on the $manifest or $installdef can be found in the Introduction to the Manifest . The Logic Hook This logic hook will install a before_save hook to the accounts module that will default the account name to 'My New Account Name ({time stamp})'. This various ways of installing this hook will be shown in the following sections. Logic Hook Definition <basepath>/Files/custom/Extension/Accounts/Ext/LogicHooks/account_name_hook.php <?php $hook_array['before_save'][] = Array( 99, 'Example Logic Hook - Updates account name', 'custom/modules/Accounts/accounts_save.php', 'Accounts_Save', 'updateAccountName' ); Logic Hook File
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_for_a_Logic_Hook/index.html
f2c71ef3616e-2
'updateAccountName' ); Logic Hook File <basepath>/Files/custom/modules/Accounts/accounts_save.php <?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class Accounts_Save { function updateAccountName($bean, $event, $arguments) { $bean->name = "My New Account Name (" . time() . ")"; } } Distributed Plugin
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_for_a_Logic_Hook/index.html
f2c71ef3616e-3
} } Distributed Plugin If the package being built is intended to be distributed to various Sugar instances as part of a distributed plugin, your logic hook definition should be created as part of the extension framework using the LogicHooks Extension, which can be done using $installdefs['logic_hooks'] in the package manifest. When using this extension, the logic hook will be installed and uninstalled along with the plugin. Note: More information on the $installdefs['logic_hooks'] can be found in LogicHooks Extensions. Package Example The following files are relative to the root of the .zip archive which is referred to at the basepath. This example package can be downloaded here. <basepath>/manifest.php <?php $manifest = array(
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_for_a_Logic_Hook/index.html
f2c71ef3616e-4
<?php $manifest = array( 'acceptable_sugar_flavors' => array( 'PRO', 'ENT', 'ULT' ), 'acceptable_sugar_versions' => array( 'exact_matches' => array(), 'regex_matches' => array( '13.0.* '), ), 'author' => 'SugarCRM', 'description' => 'Installs a sample logic hook - using extension framework', 'icon' => '', 'is_uninstallable' => true, 'name' => 'Example Logic Hook Installer - Using Extension Framework', 'published_date' => '2018-01-01 00:00:00', 'type' => 'module',
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_for_a_Logic_Hook/index.html
f2c71ef3616e-5
'type' => 'module', 'version' => '1.0.0', ); $installdefs = array( 'id' => 'package_123', 'copy' => array( array( 'from' => '<basepath>/Files/custom/modules/Accounts/accounts_save.php', 'to' => 'custom/modules/Accounts/accounts_save.php', ), ), 'hookdefs' => array( array( 'from' => '<basepath>/Files/custom/Extension/Accounts/Ext/LogicHooks/account_name_hook.php', 'to_module' => 'Accounts', ) ), );
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_for_a_Logic_Hook/index.html
f2c71ef3616e-6
) ), ); <basepath>/Files/custom/Extension/Accounts/Ext/LogicHooks/account_name_hook.php <?php $hook_array['before_save'][] = Array( 99, 'Example Logic Hook - Updates account name', 'custom/modules/Accounts/accounts_save.php', 'Accounts_Save', 'updateAccountName' ); <basepath>/Files/custom/modules/Accounts/accounts_save.php <?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class Accounts_Save { function updateAccountName($bean, $event, $arguments) {
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_for_a_Logic_Hook/index.html
f2c71ef3616e-7
function updateAccountName($bean, $event, $arguments) { $bean->name = "My New Account Name (" . time() . ")"; } } Promotion Process If the package being built is intended to be part of a code promotion process, you are welcome to use the LogicHooks Extension as mentioned above or you may find it easier to update the ./custom/modules/<module>/logic_hooks.php file, which can be done using the $installdefs['logic_hooks'] in the package manifest. Package Example The following files are relative to the root of the .zip archive which is referred to at the basepath. This example package can be downloaded here. <basepath>/manifest.php <?php $manifest = array(
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_for_a_Logic_Hook/index.html
f2c71ef3616e-8
<?php $manifest = array( 'acceptable_sugar_flavors' => array( 'PRO', 'ENT', 'ULT' ), 'acceptable_sugar_versions' => array( 'exact_matches' => array(), 'regex_matches' => array( '13.0.* '), ), 'author' => 'SugarCRM', 'description' => 'Installs a sample logic hook - using logic_hooks installdefs', 'icon' => '', 'is_uninstallable' => true, 'name' => 'Example Logic Hook Installer - Using logic_hooks installdefs', 'published_date' => '2018-01-01 00:00:00',
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_for_a_Logic_Hook/index.html
f2c71ef3616e-9
'published_date' => '2018-01-01 00:00:00', 'type' => 'module', 'version' => '1.0.0', ); $installdefs = array( 'id' => 'package_456', 'copy' => array( array( 'from' => '<basepath>/Files/custom/modules/Accounts/accounts_save.php', 'to' => 'custom/modules/Accounts/accounts_save.php', ), ), 'logic_hooks' => array( array( 'module' => 'Accounts', 'hook' => 'before_save', 'order' => 99,
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_for_a_Logic_Hook/index.html
f2c71ef3616e-10
'order' => 99, 'description' => 'Example Logic Hook - Updates account name', 'file' => 'custom/modules/Accounts/accounts_save.php', 'class' => 'Accounts_Save', 'function' => 'updateAccountName', ), ), ); <basepath>/Files/custom/modules/Accounts/accounts_save.php <?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class Accounts_Save { function updateAccountName($bean, $event, $arguments) { $bean->name = "My New Account Name (" . time() . ")"; } } Conditionally Add or Remove Logic Hook via post_execute script
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_for_a_Logic_Hook/index.html
f2c71ef3616e-11
} } Conditionally Add or Remove Logic Hook via post_execute script A developer may want to be able to trigger the addition or removal of logic hooks from a customization. This can be done using a post execute script with the check_logic_hook_file and remove_logic_hook_file functions. It is recommended to be cautious when doing this and limit any updates to off-hours as not to affect system behavior for users. Note: remove_logic_hook_file only removes hooks defined in the given module's logic_hooks.php file. check_logic_hook_file only checks hooks defined in the given module's logic_hooks.php file. Neither check or remove hooks defined via the Logic Hook Extension framework. Thus this deployment option should be considered a subset of the Promotion Process deployment option listed earlier. Package Example
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_for_a_Logic_Hook/index.html
f2c71ef3616e-12
Package Example Note: For demonstration purposes, in the post_execute script, the Accounts_Save logic hook from the earlier example package is unregistered (if it exists) and a second logic hook Accounts_Save_Enterprise is registered. The second logic hook points to a different logic hook file, which is moved to the instance using copy in the manifest. The following files are relative to the root of the .zip archive which is referred to at the basepath. This example package can be downloaded here. <basepath>/manifest.php <?php $manifest = array( 'acceptable_sugar_flavors' => array( 'PRO', 'ENT', 'ULT' ), 'acceptable_sugar_versions' => array( 'exact_matches' => array(),
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_for_a_Logic_Hook/index.html
f2c71ef3616e-13
'exact_matches' => array(), 'regex_matches' => array( '13.0.* '), ), 'author' => 'SugarCRM', 'description' => 'Installs a sample logic hook - using post_execute check_logic_hook_file', 'icon' => '', 'is_uninstallable' => true, 'name' => 'Example Logic Hook Installer - Using post_execute check_logic_hook_file', 'published_date' => '2018-01-01 00:00:00', 'type' => 'module', 'version' => '1.0.0', ); $installdefs = array( 'id' => 'package_789', 'copy' => array(
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_for_a_Logic_Hook/index.html
f2c71ef3616e-14
'id' => 'package_789', 'copy' => array( array( 'from' => '<basepath>/Files/custom/modules/Accounts/accounts_save_enterprise.php', 'to' => 'custom/modules/Accounts/accounts_save_enterprise.php', ), ), 'post_execute' => array( '<basepath>/change_hooks.php', ), ); <basepath>/Files/custom/modules/Accounts/accounts_save_enterprise.php <?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class Accounts_Save_Enterprise { function updateAccountName($bean, $event, $arguments)
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_for_a_Logic_Hook/index.html
f2c71ef3616e-15
{ function updateAccountName($bean, $event, $arguments) { $bean->name = "My New Account Name (" . time() . ") -- Enterprise"; } } <basepath>/change_hooks.php <?php if(!defined('sugarEntry')) define('sugarEntry', true); require_once 'include/entryPoint.php'; $accounts_hook_pro = array( 99, 'Example Logic Hook - Updates account name', 'custom/modules/Accounts/accounts_save.php', 'Accounts_Save', 'updateAccountName' ); $accounts_hook_ent = array( 99, 'Example Logic Hook - Updates account name - Enterprise',
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_for_a_Logic_Hook/index.html
f2c71ef3616e-16
99, 'Example Logic Hook - Updates account name - Enterprise', 'custom/modules/Accounts/accounts_save_enterprise.php', 'Accounts_Save_Enterprise', 'updateAccountName' ); if( $GLOBALS['sugar_flavor'] == 'ENT' ) { //unregister Accounts_Save logichook remove_logic_hook("Accounts", "before_save", $accounts_hook_pro); //register Accounts_Save_Enterprise logichook check_logic_hook_file("Accounts", "before_save", $accounts_hook_ent); } Last modified: 2023-02-03 21:04:03
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_for_a_Logic_Hook/index.html
77c11bb579ab-0
Creating an Installable Package That Copies Files Overview This is an overview of how to create a module loadable package that will copy files into your instance of Sugar. This is most helpful when your instance is hosted in Sugar's cloud environment or by a third party. For more details on the $manifest or $installdef options, you can visit the Introduction to the Manifest. This example package can be downloaded here. Manifest Example <?php $manifest = array( 'acceptable_sugar_flavors' => array('PRO','ENT','ULT'), 'acceptable_sugar_versions' => array( 'exact_matches' => array(), 'regex_matches' => array('(.*?)\\.(.*?)\\.(.*?)$'), ), 'author' => 'SugarCRM',
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_That_Copies_Files/index.html
77c11bb579ab-1
), 'author' => 'SugarCRM', 'description' => 'Copies my files to custom/src/', 'icon' => '', 'is_uninstallable' => true, 'name' => 'Example File Installer', 'published_date' => '2018-06-29 00:00:00', 'type' => 'module', 'version' => '1.0.0', ); $installdefs = array( 'id' => 'package_1530305622', 'copy' => array( 0 => array( 'from' => '<basepath>/Files/custom/src/MyLibrary/MyCustomClass.php', 'to' => 'custom/src/MyLibrary/MyCustomClass.php', ),
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_That_Copies_Files/index.html
77c11bb579ab-2
), ), ); Copied File Example This example copies a file to  ./custom/src/MyLibrary/MyCustomClass.php, which adds the custom namespaced class Sugarcrm\Sugarcrm\custom\MyLibrary\MyCustomClass to the application. <?php namespace Sugarcrm\Sugarcrm\custom\MyLibrary; use \Sugarcrm\Sugarcrm\Logger\Factory; class MyCustomClass { public function MyCustomMethod($message = 'relax') { $logger = Factory::getLogger('default'); $logger->info('MyCustomClass says' . "'{$message}'"); } } Last modified: 2023-02-03 21:04:03
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_That_Copies_Files/index.html
f2d1a0c96987-0
Removing Files with an Installable Package Overview This is an overview of how to create a module loadable package that will remove files from the custom directory that may have been left over from a previous package installation or legacy versions of your code customization.  Note: For SugarCloud customers, this method will not work because removing files is prohibited by Package Scanner. To have files removed, you can submit a case to Sugar Support with a list of files to be removed or request a package approval and provide your module loadable package.
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Removing_Files_with_an_Installable_Package/index.html
f2d1a0c96987-1
This example will install a custom file and use a pre-execute script to remove files from a previously installed customization. Typically, uninstalling a package would remove the files of the customization, however, in some scenarios, it is desired to install a new package on top of a previous version, and removing files with the new package might be necessary. The full package for this example is downloadable here for your reference. For more details on the $manifest or $installdef options, you can visit the Introduction to the Manifest documentation.  Manifest Example <basepath>/manifest.php <?php $manifest = array( 'acceptable_sugar_flavors' => array( 'PRO', 'ENT', 'ULT' ), 'acceptable_sugar_versions' => array(
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Removing_Files_with_an_Installable_Package/index.html
f2d1a0c96987-2
), 'acceptable_sugar_versions' => array( 'exact_matches' => array(), 'regex_matches' => array( '13.0.* '), ), 'author' => 'SugarCRM', 'description' => 'Installs a custom class file', 'icon' => '', 'is_uninstallable' => true, 'name' => 'Example Removing File Installer', 'published_date' => '2018-12-06 00:00:00', 'type' => 'module', 'version' => '1.2.0' ); $installdefs = array( 'id' => 'package_1341607504', 'copy' => array( array(
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Removing_Files_with_an_Installable_Package/index.html
f2d1a0c96987-3
'copy' => array( array( 'from' => '<basepath>/Files/custom/src/CustomClass.php', 'to' => 'custom/src/CustomClass.php', ), ), 'pre_execute' => array( '<basepath>/removeLegacyFiles.php', ), 'remove_files' => array( 'custom/include/CustomClass.php' ) ); <basepath>/removeLegacyFiles.php  <?php if (isset($this->installdefs['remove_files'])) { foreach($this->installdefs['remove_files'] as $relpath){ if (SugarAutoloader::fileExists($relpath)) {
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Removing_Files_with_an_Installable_Package/index.html
f2d1a0c96987-4
if (SugarAutoloader::fileExists($relpath)) { SugarAutoloader::unlink($relpath); } } } The full package is downloadable here for your reference. Last modified: 2023-02-03 21:04:03
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Removing_Files_with_an_Installable_Package/index.html
40b215ff6a68-0
Creating an Installable Package that Creates New Fields Overview This is an overview of how to create a Module Loader package that will install custom fields to a module. This example will install a set of fields to the accounts module. The full package is downloadable here for your reference. For more details on the $manifest or $installdef options, you can visit the Introduction to the Manifest File. Note: Sugar Sell Essentials customers do not have the ability to upload custom file packages to Sugar using Module Loader. Manifest Example <basepath>/manifest.php <?php $manifest = array( 'acceptable_sugar_flavors' => array('CE', 'PRO', 'CORP', 'ENT', 'ULT'), 'acceptable_sugar_versions' => array( 'exact_matches' => array(),
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_13.0/Cookbook/Module_Loadable_Packages/Creating_an_Installable_Package_that_Creates_New_Fields/index.html