As we all know out of the box Sugar has the facility to add a file upload field only in few of the core modules and custom modules of module template type ‘file’. This article will explain you how we can add a file upload field to any of the core modules or any custom module which is not a file type module template.
Our first step is to create a new file type field for our module. Follow the below steps to achieve that.
<?php
$GLOBALS['dictionary']['{YOUR_MODULE_KEY}']['fields']['filename'] = array (
'name' => 'filename',
'vname' => 'LBL_FILENAME',
'type' => 'file',
'dbType' => 'varchar',
'len' => '255',
'reportable' => true,
'comment' => 'File name associated with the note (attachment)',
'importable' => false,
);
$GLOBALS['dictionary']['{YOUR_MODULE_KEY}']['fields']['file_mime_type'] = array(
'name' => 'file_mime_type',
'vname' => 'LBL_FILE_MIME_TYPE',
'type' => 'varchar',
'len' => '100',
'comment' => 'Attachment MIME type',
'importable' => false,
);
$GLOBALS['dictionary']['{YOUR_MODULE_KEY}']['fields']['file_url'] = array (
'name' => 'file_url',
'vname' => 'LBL_FILE_URL',
'type' => 'varchar',
'source' => 'non-db',
'reportable' => false,
'comment' => 'Path to file (can be URL)',
'importable' => false,
);Note: To find {YOUR_MODULE_KEY} navigate to
cache/modules/{YOUR_MODULE}/{YOUR_MODULE}vardefs.php and look for the key being used for $dictionary array. <?php
$mod_strings['LBL_FILE_MIME_TYPE'] = 'File Mime Type';
$mod_strings['LBL_FILE_URL'] = 'File URL';
$mod_strings['LBL_FILENAME'] = '{YOUR DISPLAY LABEL}';if (method_exists($bean, 'deleteAttachment')) {
if (!$bean->deleteAttachment()) {
// @TODO Localize this exception message
throw new SugarApiExceptionRequestMethodFailure('Removal of attachment failed.');
}
} else {
// @TODO Localize this exception message
throw new SugarApiExceptionNoMethod('No method found to remove attachment.');
}So our next step is to add the ‘deleteAttachment()’ method to your module bean.
As we can’t modify the core bean file. We are going to extend the bean class of our module.
<?php
$beanList['{YOUR_MODULE_NAME}'] = 'Custom{YOUR_MODULE_KEY}'
$beanFiles['Custom{YOUR_MODULE_KEY}'] = 'custom/modules/{YOUR_MODULE}/Custom{YOUR_MODULE_KEY}.php';Note: For core modules you can find the {YOUR_MODULE_KEY} in Include/modules.php file If your Module is Accounts the above two lines will look like below:
<?php
$beanList['Accounts'] = 'CustomAccount';
$beanFiles['CustomAccount'] = 'custom/modules/Accounts/CustomAccount.php';<?php
/*
*The Above code is extending the Core Account bean class add adding deleteAttachment the function.
*Replace Accounts with your module name
*/
require_once 'modules/Accounts/Account.php';
class CustomAccount extends Account
{
function deleteAttachment($isduplicate = "false")
{
if (!$this->ACLAccess('edit')) {
return false;
}
if ($isduplicate == "true") {
return true;
}
// Only attempt to delete the file if there isn't an upload_id. When there is an upload_id, we just clear the
// file metadata from the record. When there isn't an upload_id, we attempt to delete the file and clear the
// file metadata.
if (empty($this->upload_id) && !UploadFile::unlink_file($this->id)) {
return false;
}
$this->filename = '';
$this->file_ext = '';
$this->file_mime_type = '';
$this->file = '';
$this->file_size = '';
$this->file_source = '';
$this->upload_id = '';
$this->save();
return true;
}
}We are done!
This customization is purely upgrade safe and On-demand compatible.
Get started customizing your Sugar 11.x instance today! Let’s discuss about how we can help you achieve your goal of adding file upload fields to your core or custom modules. Contact us now!
Trade Shows and Exhibition Participation Manufacturing companies often generate leads and business through trade shows…
Exporting data from SugarCRM is an essential task for businesses that need to share CRM…
A CRM that works from Google Workspace - SugarCRM integration with Google Workspace Enhance relationships…
Sugar has introduced exciting features in the latest upgrade for all on-site customers. Let's deep…
In today’s competitive world, understanding and building strong customer relationships is crucial. Acquiring new customers…
Introduction In the ever-evolving world of Customer Relationship Management (CRM) software, staying current is essential.…