Page 1 of 8 12345 ... LastLast
Results 1 to 10 of 71
Like Tree1Likes

Thread: Generic Autonumber Hook

  1. #1
    eli.lindner is offline Sugar Community Member
    Join Date
    Feb 2007
    Location
    Brisbane, Australia
    Posts
    34

    Default Generic Autonumber Hook

    Hi All,

    What I have here is a simple logic hook for adding auto sequencing codes to your custom field.

    There seems to be a lot of threads on this so I thought I'd share this generic generator that can be used in a variety of circumstances.

    It creates codes in the manner, PREFIX DATE_FORMAT SEPARATOR AUTO_NUMBER SUFFIX

    eg, in the attached image I've used the following LC 08 - 0001

    All the options are configurable in the hook in the constants section.

    Code:
    	CODE_PREFIX = "LC"; //prefix for the code
    	CODE_SUFFIX = ""; //suffix to add after the code
    	CODE_SEPARATOR = "-"; //separator to use
    	CODE_FIELD = "lead_code_c"; //custom field to be generated
    	CUSTOM_TABLE = "leads_cstm"; //table containing custom fields
    	ADD_DATE = "1"; //add the date 
    	DATE_FORMAT = "y"; //format for the year parameter
    	ZERO_PADDING = 4; //minimum amount of characters desired for the number. ie 4 = 0001, 3 = 001
    	FIRST_NUM = "0001"; //default to start with.
    I've tested and using it in Leads, Opportunities, Projects, and many custom modules.

    It is designed to be used as an after_save hook and is compatible with create records from a subpanel as it doesn't rely on the custom table entry already existing.

    Add a custom field to your module in studio, noting the name of the field. In my example I've use leads module and added the field lead_code which is called lead_code_c in the database.

    Extract and copy add_code_hook.php to custom/modules/<module>/

    In logic_hooks.php add the following

    Code:
    $hook_array['after_save'] = Array(); 
    $hook_array['after_save'][] = Array(1, 'add_code', 'custom/modules/<module>/add_code_hook.php','add_code', 'add_code');
    Configure your options in add_code_hook.php

    There is probably a couple of cases where this won't work as expected but for most generic needs it's quite adaptable.

    Let me know if you have any issues or need a custom code that's requires a special regex match.

    Regards,

    Eli
    Attached Images Attached Images  
    Attached Files Attached Files
    dkinzer likes this.

  2. #2
    SugarDev.net is offline Sugar Community Member
    Join Date
    Feb 2008
    Posts
    1,401

    Default Re: Generic Autonumber Hook

    This is great!! Thnx!

    Are you considering adding this to sugarforge?
    Developers go here
    Businesses go there (Dutch)

    Modules:
    SugarDev.net Developer Tools | Config | Dutch Language Pack
    "Nothing gets fixed unless there is a bug"

  3. #3
    anvaruz is offline Senior Member
    Join Date
    Jul 2008
    Location
    United Kingdom
    Posts
    26

    Default Re: Generic Autonumber Hook

    this is exactly what I was looking for, thanks!

    I found a minor issue:
    I tested it in Contacts module, it works very well
    Until I started changing CODE_PREFIX, then it starts entering default number and doesn't do incrementing any more

    it also overwrites old value (when I edit my contact) with the new default value.

    just wandering what will happened when the year is changed to 09 with ADD_DATE set to 1 ?

    basically I need this for assigning unique reference number to my contacts, and each year the prefix needs to be changed
    but also old prefixes should remain the same when the contact is edited

    I'm using Version 5.1.0b (Build 4905)
    XAMPP: PHP Version: 5.2.4 , MySQL 5.0.45

  4. #4
    JefD is offline Member
    Join Date
    Oct 2008
    Posts
    6

    Default Re: Generic Autonumber Hook

    First will i say thanks for the script, i tested on the contact module and it works very well

    But i have some problems when i change the date to ymd.

    These numbers are generate:
    IO081126
    IO08112681126
    IO0811268112681126


    I hope that someone can help me?!

    System Details:
    SugarCRM 5.1.0a
    Debian Linux 4.0
    PHP5.0

    JefD

    P.s sorry for my bad English
    Last edited by JefD; 2008-11-26 at 12:57 PM.

  5. #5
    swissvblade is offline Junior Member
    Join Date
    Dec 2008
    Posts
    1

    Default Re: Generic Autonumber Hook

    Nice script, I was looking for that for some time...

    I have one small problem though....I need the script to determine the last number in a the auto increment filed while skipping those records that are marked as deleted.
    I tried altering the query a bit, but I get a blank page...

  6. #6
    Superman's Avatar
    Superman is offline Sugar Community Member
    Join Date
    Oct 2005
    Location
    Kazakhstan
    Posts
    880

    Default Re: Generic Autonumber Hook

    At last! This is really what Sugar community needs.
    Farkhad Rakhimzhanov
    E-mail: farkhad@gmail.com

    SuperTimesheet timesheet tool for SugarCRM.

  7. #7
    domproblem is offline Junior Member
    Join Date
    Nov 2008
    Posts
    3

    Default Re: Generic Autonumber Hook

    Hi iam a new comer to sugar and love the program but im not so savey with the php and hooks side of things. i was wondering ?????
    we are having trouble with the hooks side of things and im starting to get confused is there any kind soul out there hou could send me any info on this i would be most great full thanks dom

  8. #8
    SugarDev.net is offline Sugar Community Member
    Join Date
    Feb 2008
    Posts
    1,401

    Default Re: Generic Autonumber Hook

    Have you seen the developer guide?
    Developers go here
    Businesses go there (Dutch)

    Modules:
    SugarDev.net Developer Tools | Config | Dutch Language Pack
    "Nothing gets fixed unless there is a bug"

  9. #9
    eli.lindner is offline Sugar Community Member
    Join Date
    Feb 2007
    Location
    Brisbane, Australia
    Posts
    34

    Default Re: Generic Autonumber Hook

    @domproblem please give me some more details so I can help you out

    @jefD if you are still having trouble this is what you need to change

    (self::ADD_DATE?"[0-9]{2}":"")

    to

    (self::ADD_DATE?"[0-9]{6}":"")

    That should fix your problem.

    @swissvblade

    change

    where (" . self::CODE_FIELD . " <> '' or " . self::CODE_FIELD . " is not null)

    to

    where ((" . self::CODE_FIELD . " <> '' or " . self::CODE_FIELD . " is not null) and deleted = 0)

  10. #10
    ybong is offline Member
    Join Date
    Feb 2009
    Posts
    10

    Default Re: Generic Autonumber Hook

    Hi,

    May I know where can i find logic_hooks.php file? I am using sugar CRM Community Version 5.2.0.
    Please advice. Thanks


    Regards,
    YB

Page 1 of 8 12345 ... LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. copy address logic hook
    By michamamo2 in forum Developer Help
    Replies: 1
    Last Post: 2008-11-20, 12:59 AM
  2. still struggling with logic hook.
    By ServerStorm in forum Developer Help
    Replies: 9
    Last Post: 2008-10-29, 03:12 PM
  3. How to open a popup within a logic hook?
    By fedepia in forum Help
    Replies: 2
    Last Post: 2008-10-16, 06:31 PM
  4. Adding Logic Hook to existing
    By Superman in forum Developer Help
    Replies: 2
    Last Post: 2006-12-18, 09:38 PM
  5. Logic Hook Question
    By swhitlow in forum Developer Help
    Replies: 1
    Last Post: 2006-06-04, 04:49 AM

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
  •