Dear Members,
Starting from this thread below,
http://www.sugarcrm.com/forums/showt...nerated+fields
I have used the hook but the problem here is can this hook be used for custom module default field that is name which is varchar.
And I have related that to opportunity on one-one relationship
And I have not used that field in Quickcreate.
Is there any problem in achieving my criteria..
Any help is appreciated..
Thanks in advance...
Here is the code below
PHP Code:<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/**
* add_code - Generic Autonumbering Hook
* ---------------------------------------------------------------
* The purpose of this hook is to generate generic codes for
* numbering entries that go into SugarCRM (e.g. new Leads, etc).
* This hook is intended to allow for greater flexibility and is
* fired after_save.
* ---------------------------------------------------------------
* Original Concept by Eli Lindner on SugarCRM.com Forums
* Revamp by Kris Tremblay - Silence IT
* August 10th, 2009
*/
class add_code
{
const CODE_PREFIX = "CF"; // Prefix (ie. LD represents the Leads prefix)
const CODE_SEPARATOR = "-"; // Character(s) separating the prefix and the code
const MIN_CODE_LENGTH = 6; // e.g. 0001, 0002, etc; used to dictate padding
const DATE_FORMAT = "y"; // Date format string for part of the prefix (e.g. "y" = 09 in LD09-)
const CUSTOM_FIELD = "name"; // Custom field to store the code in
const CUSTOM_TABLE = "css_sales_survey"; // Custom table where the custom field is located
function add_code(&$bean, $event, $arguments)
{
$db = DBManagerFactory::getInstance();
// Create complete prefix for the code (e.g. LD09-)
$prefix = self::CODE_PREFIX.date(self::DATE_FORMAT).self::CODE_SEPARATOR;
// Get the starting position for the SUBSTR call in the query
$prefix_len = strlen($prefix) + 1;
$query = "SELECT CAST(SUBSTR(".self::CUSTOM_FIELD.", $prefix_len) AS UNSIGNED) as ".self::CUSTOM_FIELD." FROM ".self::CUSTOM_TABLE."
WHERE (".self::CUSTOM_FIELD." <> '' OR ".self::CUSTOM_FIELD." IS NOT NULL)
ORDER BY CAST(SUBSTR(".self::CUSTOM_FIELD.", $prefix_len) AS UNSIGNED)
DESC
LIMIT 1";
$result = $db->query($query, true);
$row = $db->fetchByAssoc($result);
// Increment the highest code by 1 and pad if necessary
$code = $row[self::CUSTOM_FIELD] + 1;
$code = str_pad($code, self::MIN_CODE_LENGTH, "0", STR_PAD_LEFT);
// Put it all together
$new_code = $prefix.$code;
// Update the record that was just saved
$update_query = "UPDATE ".self::CUSTOM_TABLE." SET ".self::CUSTOM_FIELD." = '$new_code'
WHERE id_c = '{$bean->id}' AND (".self::CUSTOM_FIELD." = '' or ".self::CUSTOM_FIELD." IS NULL)";
$db->query($update_query, true);
}
}
?>


LinkBack URL
About LinkBacks



Reply With Quote

Bookmarks