The "utils" option allows you to define custom global utility functions. One use of a custom utility function is for populating a dropdown from the database. For example, you may wish to have a custom field on a contract that tracks what product is associated to it. There are two main steps to do this; define the custom function and then tell the custom field to pull from the function.
Using the module manifest organization mentioned in a previous post (https://www.sugaroutfitters.com/blog/organizing-files-in-a-module-zip) we first create the utils function in contract_products.php:
/extensions/application/utils/contract_products_utils.php
// /extensions/application/utils/contract_products_utils.php | |
function getProducts(){ | |
static $products = null; | |
if(!$products){ | |
global $db; | |
$query = "SELECT id, name FROM products where deleted = 0 order by name asc "; | |
$result = $db->query($query, false); | |
$products = array(); | |
$products[''] = ''; | |
while (($row = $db->fetchByAssoc($result)) != null) { | |
$products[$row['id']] = $row['name']; | |
} | |
} | |
return $products; | |
} |
Now we tell our custom field to pull from this new function:
/extensions/modules/Contracts/vardefs/product_dropdown_vardefs.php
// /extensions/modules/Contracts/vardefs/product_dropdown_vardefs.php | |
$dictionary['Contract']['fields']['product_c'] = array ( | |
'name' => 'product_c', | |
'vname' => 'LBL_PRODUCT', | |
'function' => 'getProducts', | |
'type' => 'enum', | |
'len' => '100', | |
); |
Finally, we add these to the manifest so that it will get installed via the Module Installer:
//manifest.php | |
//............... | |
'vardefs' => | |
array ( | |
//copies to custom/Extension/modules/Contracts/Ext/Vardefs | |
array ( | |
'from' => '<basepath>/extensions/modules/Contracts/vardefs/product_dropdown_vardefs.php', | |
'to_module' => 'Contracts', | |
), | |
), | |
'utils' => | |
array ( | |
//copies to custom/Extension/application/Ext/Utils/ | |
array ( | |
'from' => '<basepath>/extensions/application/utils/contract_products_utils.php', | |
), | |
), | |
//.......... |
What other purposes have you used custom utils for?
-
Proximity Search
Proximity searches are becoming more essential to businesses every day. Sales and marketing teams use them to organize field visits and campaigns. The real estate industry use them to offer better alt... -
SalesRoller
Link **Xero Cloud Accounting** and **SugarCRM** for the full 360 degree view of your customer information. -
Boxxstep
FEATUREDSelling is hard, buying is harder. Boxxstep integrates with Sugar to helps sales teams in complex and enterprise sales be more buyer-centric to close more deals. - Show more addons