Results 1 to 4 of 4

Thread: Autonumber hook

  1. #1
    swaroops1 is offline Senior Member
    Join Date
    Jul 2010
    Posts
    43

    Default Autonumber hook

    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($querytrue);
            
    $row $db->fetchByAssoc($result);
            
            
    // Increment the highest code by 1 and pad if necessary
            
    $code $row[self::CUSTOM_FIELD] + 1;
            
    $code str_pad($codeself::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_querytrue);
        }
    }

    ?>

  2. #2
    davidboris is offline Sugar Community Member
    Join Date
    May 2010
    Posts
    1,113

    Default Re: Autonumber hook

    Hi,
    Can you please clarify your needs ?
    Sorry, but It is little bit confusing.
    Thumbs up.

    Skype ID - david__boris

    SugarForge Projects:

    WYSIWYG now in studio!(Version 1.1 is out now!)

    Sugar Feeds on your personalized home pages like iGoogle, My Yahoo!, etc.

    Fab Tools! > Dashlet Not Followed Opportunities for past six Months

  3. #3
    swaroops1 is offline Senior Member
    Join Date
    Jul 2010
    Posts
    43

    Exclamation Re: Autonumber hook

    Hi..

    Here is my problem..

    I need a autonumber field for varchar field. I have not created any custom field for this module.

    And I have a relationship one-one between Opportunity and custom module.

    So please help me to achieve this.. As the relation is one-one the number generated in custom module would automatically reflect in Opportunity.

    Hope I am clear with my requirement.

  4. #4
    shailendra001's Avatar
    shailendra001 is offline A Prolific Poster
    Join Date
    Oct 2008
    Location
    Bangalore India
    Posts
    255

    Default Re: Autonumber hook

    Quote Originally Posted by swaroops1 View Post
    Hi..

    Here is my problem..

    I need a autonumber field for varchar field. I have not created any custom field for this module.

    And I have a relationship one-one between Opportunity and custom module.

    So please help me to achieve this.. As the relation is one-one the number generated in custom module would automatically reflect in Opportunity.

    Hope I am clear with my requirement.

    Hi !

    Since there is one to one relationship between Opportunities modules and Custom modules and want to generate id in custom modules follow these steps

    1) create a custom field in your custom modules say ' NEW_ID ' create same custom filed in Opportunities modules
    2) Apply that code with modification for your custom modules , because you want to generate that id in Custom modules
    3 ) override view.edit.php , in csutom/modules/Opportunities/views/view.edit.php so that on select event of Opportunities will bring the NEW_ID from your custom modules

    Kind Regards
    Shailendra

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Generic Autonumber Hook
    By eli.lindner in forum Downloads
    Replies: 57
    Last Post: 2012-04-15, 02:48 PM
  2. Replies: 10
    Last Post: 2010-11-16, 02:41 AM
  3. How to Create autonumber in Opportunities
    By jhermiz in forum Developer Help
    Replies: 3
    Last Post: 2008-12-08, 01:57 PM
  4. Adding Autonumber Field
    By phillipdean in forum Developer Help
    Replies: 2
    Last Post: 2008-12-07, 03:11 AM
  5. Autonumber - Customer Number
    By hubcrm in forum Developer Help
    Replies: 1
    Last Post: 2008-01-03, 12:54 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •