Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: Custom field auto increased by Logic Hook ?

  1. #1
    dekleinemedia is offline A Prolific Poster
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    241

    Default Custom field auto increased by Logic Hook ?

    Hi,
    I have the following problem;

    I've created a custom module called "Acc_Accounts"
    Now I want my field "acc_number" to be automatically increased when a new account is entered.

    I've read several posts on Sugar about a logic hook and now I found a solution which was shared on Sugar, but it still doesn't seems to work.

    I've saved the files "logic_hook.php" and "add_code_hook.php" in custom\modules\Acc_Accounts

    I hope someone can tell me what is wrong with my code?

    Thanksss

    logic_hook.php

    PHP Code:
    <?php
    class Acc_Accounts_class
    {
        function 
    Acc_Accounts_method(&$bean$event$arguments=null)
        {
            if (
    $event != 'after_save') return;
                
    $hook_array['after_save'] = Array(); 
                
    $hook_array['after_save'][] = Array(1'add_code''custom/modules/Acc_Accounts/add_code_hook.php','add_code''add_code');
        }
    }
    ?>
    add_code_hook.php

    PHP Code:
    <?php
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

    class 
    add_code {

        const 
    CODE_PREFIX "";
        const 
    CODE_SUFFIX "";
        const 
    CODE_SEPARATOR "-";
        const 
    CODE_FIELD "acc_number";
        const 
    CUSTOM_TABLE "acc_accounts";
        const 
    ADD_DATE "1";
        const 
    DATE_FORMAT "y";
        const 
    ZERO_PADDING 4//minimum amount of characters desired for the number. ie 4 = 0001, 3 = 001
        
    const FIRST_NUM "0001"//default to start with.

        
    function add_code(&$bean$event$arguments)
        {
            require_once(
    'log4php/LoggerManager.php');

            
    //pattern to detect valid code, designed to detect non padded numbering too.
            
    $pattern "/^" self::CODE_PREFIX . (self::ADD_DATE?"[0-9]{2}":"") . self::CODE_SEPARATOR "([0-9]+)" self::CODE_SUFFIX "$/";

            
    $GLOBALS['log']->debug("add_code function fired after save.");

            if(!
    preg_match($pattern$bean->fetched_row[self::CODE_FIELD])) {

                
    $db =  DBManagerFactory::getInstance();
                
    $query "select " self::CODE_FIELD " from " self::CUSTOM_TABLE 
                         
    " where (" self::CODE_FIELD " <> '' or " self::CODE_FIELD " is not null) order by right(" self::CODE_FIELD ", 4) desc limit 1";
        
                
    $result $db->query($querytrue'Error selecting most recent ' self::CODE_FIELD ' CODE');
        
                if (
    $row=$db->fetchByAssoc($result)) {
                    
    $last_code $row[self::CODE_FIELD];
                } else {
                    
    //no codes exist, generate default - PREFIX CURRENT_YEAR SEPARATOR FIRST_NUM
                    
    $last_code self::CODE_PREFIX . (self::ADD_DATE?date(self::DATE_FORMAT):"") . self::CODE_SEPARATOR self::FIRST_NUM self::SUFFIX;
                }
        
                
    $GLOBALS['log']->debug("Last Code: " $last_code);
        
                
    preg_match($pattern$last_code$matches);
                
    $num=$matches[1];
                
    $num++;
                
    $pads self::ZERO_PADDING strlen($num);
                
    $new_code self::CODE_PREFIX . (self::ADD_DATE?date(self::DATE_FORMAT):"") . self::CODE_SEPARATOR;
        
                
    //preform the lead padding
                
    for($i=0$i $pads$i++) {
                    
    $new_code .= "0";
                }

                
    $new_code .= $num self::CODE_SUFFIX;

                
    $GLOBALS['log']->debug("New Code: " $last_code);
                
                
    //write to database
                
    $query "INSERT INTO " self::CUSTOM_TABLE " (id_c, " self::CODE_FIELD ") VALUES ('$bean->id', '$new_code') ON DUPLICATE KEY UPDATE " self::CODE_FIELD "='$new_code'";
                
    $result $db->query($querytrue'Error adding ' self::CODE_PREFIX ' code');
            } else {
                
    $GLOBALS['log']->debug("Code Exists...Exiting " $bean->fetched_row[" . self::CODE_FIELD . "]);        
            }
        }
    }

    ?>
    Kind regards,

    De Kleine Media


    SugarCRM CE v.5.2.0h
    Windows platform
    MySQL v.5.1
    phpMyAdmin - 2.11.2.2
    Apache Server v.2.0

  2. #2
    crmbalah is offline A Prolific Poster
    Join Date
    Mar 2009
    Location
    chennai
    Posts
    418

    Default Re: Custom field auto increased by Logic Hook ?

    Hi
    try this code logic hook in before to save

  3. #3
    dekleinemedia is offline A Prolific Poster
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    241

    Default Re: Custom field auto increased by Logic Hook ?

    Quote Originally Posted by crmbalah View Post
    Hi
    try this code logic hook in before to save
    Hi, thanks for your reply.


    I've changed my code in logic hook;

    PHP Code:
    <?php
    // Do not store anything in this file that is not part of the array or the hook version.  This file will    
    // be automatically rebuilt in the future. 
     
    $hook_version 1
    $hook_array = Array(); 
    // position, file, function 
    $hook_array['before_save'] = Array(); 
    $hook_array['before_save'][] = Array(1'add_code''custom/modules/Acc_Accounts/add_code_hook.php','add_code''add_code'); 
    ?>
    ?>
    This is mu add_code_hook

    PHP Code:
    <?php
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

    class 
    add_code {

        const 
    CODE_PREFIX ""//prefix for the code
        
    const CODE_SUFFIX ""//suffix to add after the code
        
    const CODE_SEPARATOR ""//separator to use
        
    const CODE_FIELD "acc_number"//custom field to be generated
        
    const CUSTOM_TABLE "acc_accounts"//table containing custom fields
        
    const ADD_DATE "0"//add the date 
        
    const DATE_FORMAT "y"//format for the year parameter
        
    const ZERO_PADDING 4//minimum amount of characters desired for the number. ie 4 = 0001, 3 = 001
        
    const FIRST_NUM "0001"//default to start with.

        
    function add_code(&$bean$event$arguments)
        {
            require_once(
    'log4php/LoggerManager.php');

            
    //pattern to detect valid code, designed to detect non padded numbering too.
            
    $pattern "/^" self::CODE_PREFIX . (self::ADD_DATE?"[0-9]{2}":"") . self::CODE_SEPARATOR "([0-9]+)" self::CODE_SUFFIX "$/";

            
    $GLOBALS['log']->debug("add_code function fired after save.");

            if(!
    preg_match($pattern$bean->fetched_row[self::CODE_FIELD])) {

                
    $db =  DBManagerFactory::getInstance();
                
    $query "select " self::CODE_FIELD " from " self::CUSTOM_TABLE 
                         
    " where (" self::CODE_FIELD " <> '' or " self::CODE_FIELD " is not null) order by right(" self::CODE_FIELD ", 4) desc limit 1";
        
                
    $result $db->query($querytrue'Error selecting most recent ' self::CODE_FIELD ' CODE');
        
                if (
    $row=$db->fetchByAssoc($result)) {
                    
    $last_code $row[self::CODE_FIELD];
                } else {
                    
    //no codes exist, generate default - PREFIX CURRENT_YEAR SEPARATOR FIRST_NUM
                    
    $last_code self::CODE_PREFIX . (self::ADD_DATE?date(self::DATE_FORMAT):"") . self::CODE_SEPARATOR self::FIRST_NUM self::SUFFIX;
                }
        
                
    $GLOBALS['log']->debug("Last Code: " $last_code);
        
                
    preg_match($pattern$last_code$matches);
                
    $num=$matches[1];
                
    $num++;
                
    $pads self::ZERO_PADDING strlen($num);
                
    $new_code self::CODE_PREFIX . (self::ADD_DATE?date(self::DATE_FORMAT):"") . self::CODE_SEPARATOR;
        
                
    //preform the lead padding
                
    for($i=0$i $pads$i++) {
                    
    $new_code .= "0";
                }

                
    $new_code .= $num self::CODE_SUFFIX;

                
    $GLOBALS['log']->debug("New Code: " $last_code);
                
                
    //write to database
                
    $query "INSERT INTO " self::CUSTOM_TABLE " (id_c, " self::CODE_FIELD ") VALUES ('$bean->id', '$new_code') ON DUPLICATE KEY UPDATE " self::CODE_FIELD "='$new_code'";
                
    $result $db->query($querytrue'Error adding ' self::CODE_PREFIX ' code');
            } else {
                
    $GLOBALS['log']->debug("Code Exists...Exiting " $bean->fetched_row[" . self::CODE_FIELD . "]);        
            }
        }
    }

    ?>
    It still doesn't work... can someone tell me what i did wrong? I've also added a screenshot of my database... please help!
    Thanksssss
    Attached Images Attached Images  
    Last edited by dekleinemedia; 2009-06-30 at 03:03 PM.
    Kind regards,

    De Kleine Media


    SugarCRM CE v.5.2.0h
    Windows platform
    MySQL v.5.1
    phpMyAdmin - 2.11.2.2
    Apache Server v.2.0

  4. #4
    dekleinemedia is offline A Prolific Poster
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    241

    Question Re: Custom field auto increased by Logic Hook ?

    I hope someone can tell me what to do
    Kind regards,

    De Kleine Media


    SugarCRM CE v.5.2.0h
    Windows platform
    MySQL v.5.1
    phpMyAdmin - 2.11.2.2
    Apache Server v.2.0

  5. #5
    heta's Avatar
    heta is offline Sugar Community Member
    Join Date
    Nov 2007
    Location
    Ahmedabad,India
    Posts
    214

    Default Re: Custom field auto increased by Logic Hook ?

    hi Jordy,

    from the screenshot it seems the table is not of custom. so the query which is written to insert the code to DB i.e.

    $query = "INSERT INTO " . self::CUSTOM_TABLE . " (id_c, " . self::CODE_FIELD . ") VALUES ('$bean->id', '$new_code') ON DUPLICATE KEY UPDATE " . self::CODE_FIELD . "='$new_code'";

    $result = $db->query($query, true, 'Error adding ' . self::CODE_PREFIX . ' code');


    has wrong field name "
    id_c" it should be "id"

    i hope that works.
    Best Regards
    ---------------------------------------
    Heta Shah - iNET PROCESS
    heta.shah@inetprocess.com
    http://www.inetprocess.co.in
    Projects :
    iNETDocs -Project of the month(Dec-2008)
    iNETGoogleMap

  6. #6
    dekleinemedia is offline A Prolific Poster
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    241

    Default Re: Custom field auto increased by Logic Hook ?

    Quote Originally Posted by heta View Post
    hi Jordy,

    from the screenshot it seems the table is not of custom. so the query which is written to insert the code to DB i.e.

    $query = "INSERT INTO " . self::CUSTOM_TABLE . " (id_c, " . self::CODE_FIELD . ") VALUES ('$bean->id', '$new_code') ON DUPLICATE KEY UPDATE " . self::CODE_FIELD . "='$new_code'";

    $result = $db->query($query, true, 'Error adding ' . self::CODE_PREFIX . ' code');


    has wrong field name "
    id_c" it should be "id"

    i hope that works.
    Hi Heta,

    Thanks for your help. Unfortunately is still doesn't work. When i create a new account, it doesn't fill in auto acc_number. When I want to save it, i just gave me an error page with I think a programming error.

    Any more suggestions? I very appreciate your help.
    Kind regards,

    De Kleine Media


    SugarCRM CE v.5.2.0h
    Windows platform
    MySQL v.5.1
    phpMyAdmin - 2.11.2.2
    Apache Server v.2.0

  7. #7
    heta's Avatar
    heta is offline Sugar Community Member
    Join Date
    Nov 2007
    Location
    Ahmedabad,India
    Posts
    214

    Default Re: Custom field auto increased by Logic Hook ?

    try this.

    comment these lines

    $query = "INSERT INTO " . self::CUSTOM_TABLE . " (id_c, " . self::CODE_FIELD . ") VALUES ('$bean->id', '$new_code') ON DUPLICATE KEY UPDATE " . self::CODE_FIELD . "='$new_code'";
    $result = $db->query($query, true, 'Error adding ' . self::CODE_PREFIX . ' code');

    ----------

    add a line
    $this->bean->acc_number = $new_code;

    hope this works.

    IF NOT PLEASE POST THE ERROR LINE FROM sugarlog..
    Best Regards
    ---------------------------------------
    Heta Shah - iNET PROCESS
    heta.shah@inetprocess.com
    http://www.inetprocess.co.in
    Projects :
    iNETDocs -Project of the month(Dec-2008)
    iNETGoogleMap

  8. #8
    dekleinemedia is offline A Prolific Poster
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    241

    Default Re: Custom field auto increased by Logic Hook ?

    Quote Originally Posted by heta View Post
    try this.

    comment these lines

    $query = "INSERT INTO " . self::CUSTOM_TABLE . " (id_c, " . self::CODE_FIELD . ") VALUES ('$bean->id', '$new_code') ON DUPLICATE KEY UPDATE " . self::CODE_FIELD . "='$new_code'";
    $result = $db->query($query, true, 'Error adding ' . self::CODE_PREFIX . ' code');

    ----------

    add a line
    $this->bean->acc_number = $new_code;

    hope this works.

    IF NOT PLEASE POST THE ERROR LINE FROM sugarlog..
    Hi thanks again. Unfortunately still the same problem..

    DB is same as above (screenshot)
    logic_hooks.php (custom/modules/Acc_Accounts) is same as above
    add_code_hooks.php (custom/modules/Acc_Accounts)
    is now looking like this;

    PHP Code:
    $GLOBALS['log']->debug("New Code: " $last_code);
                
                
    //write to database
                /*$query = "INSERT INTO " . self::CUSTOM_TABLE . " (id, " . self::CODE_FIELD . ") VALUES ('$bean->id', '$new_code') ON DUPLICATE KEY UPDATE " . self::CODE_FIELD . "='$new_code'";
                $result = $db->query($query, true, 'Error adding ' . self::CODE_PREFIX . ' code');*/
                
    $this->bean->acc_number $new_code;
            } else {
                
    $GLOBALS['log']->debug("Code Exists...Exiting " $bean->fetched_row[" . self::CODE_FIELD . "]);        
            }
        }
    }

    ?> 
    Here is error log; Can't see an log error for today..?

    06/30/09 15:51:48 [3712][1][FATAL] HTTP Error: no data present after HTTP headers
    06/30/09 15:51:48 [3712][1][FATAL] HTTP/1.0 500 Internal Server Error
    Date: Tue, 30 Jun 2009 13:52:07 GMT
    Server: Apache/2.2.3 (CentOS)
    Set-Cookie: PHPSESSID=6luib88m17uhdqqd9jc13a24b3; path=/
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Pragma: no-cache
    Content-Length: 0
    Connection: close
    Content-Type: text/html; charset=UTF-8

    06/30/09 16:28:12 [3712][1][FATAL] MySQL error 1054: Unknown column 'id_c' in 'field list'
    Kind regards,

    De Kleine Media


    SugarCRM CE v.5.2.0h
    Windows platform
    MySQL v.5.1
    phpMyAdmin - 2.11.2.2
    Apache Server v.2.0

  9. #9
    heta's Avatar
    heta is offline Sugar Community Member
    Join Date
    Nov 2007
    Location
    Ahmedabad,India
    Posts
    214

    Default Re: Custom field auto increased by Logic Hook ?

    hi dekleinemedia,

    you have created the field of type integer & the code has characters!!
    Best Regards
    ---------------------------------------
    Heta Shah - iNET PROCESS
    heta.shah@inetprocess.com
    http://www.inetprocess.co.in
    Projects :
    iNETDocs -Project of the month(Dec-2008)
    iNETGoogleMap

  10. #10
    dekleinemedia is offline A Prolific Poster
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    241

    Question Re: Custom field auto increased by Logic Hook ?

    Quote Originally Posted by heta View Post
    hi dekleinemedia,

    you have created the field of type integer & the code has characters!!
    Mmm.. sorry that was my bad!

    Now i've changed this, but it still doesn't seem to work

    If I go to modules\Acc_Accounts I changed the code in "vardefs.php"

    PHP Code:
     'acc_number' => 
      array (
        
    'name' => 'acc_number',
        
    'vname' => 'LBL_ACC_NUMBER',
        
    'type' => 'id',
        
    'comments' => 'account number',
        
    'len' => 4,
        
    'reportable' => false,
      ), 
    My DB is as in screenshot now...

    Here is my error log;
    07/01/09 10:48:20 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:48:21 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:48:21 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:48:21 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:48:22 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:48:22 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:48:23 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:48:23 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:48:24 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:48:24 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:48:25 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:50:50 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:51:06 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:51:06 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:51:07 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:51:10 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:51:23 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:51:24 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:51:24 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:51:29 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:51:42 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:51:42 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:51:43 [2248][1][FATAL] UndeployedMetaDataImplementation->getFileName(): view type working is not recognized
    07/01/09 10:54:15 [2248][1][FATAL] HTTP Error: no data present after HTTP headers
    07/01/09 10:54:15 [2248][1][FATAL] HTTP/1.1 100 Continue

    HTTP/1.0 500 Internal Server Error
    Date: Wed, 01 Jul 2009 08:54:31 GMT
    Server: Apache/2.2.3 (CentOS)
    Set-Cookie: PHPSESSID=7srk1fupirmejhsbq1phajqbv4; path=/
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Pragma: no-cache
    Content-Length: 0
    Connection: close
    Content-Type: text/html; charset=UTF-8


    07/01/09 10:54:18 [2248][1][FATAL] HTTP Error: no data present after HTTP headers
    07/01/09 10:54:18 [2248][1][FATAL] HTTP/1.1 100 Continue

    HTTP/1.0 500 Internal Server Error
    Date: Wed, 01 Jul 2009 08:54:33 GMT
    Server: Apache/2.2.3 (CentOS)
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Pragma: no-cache
    Content-Length: 0
    Connection: close
    Content-Type: text/html; charset=UTF-8


    07/01/09 10:54:18 [2248][1][FATAL] HTTP Error: no data present after HTTP headers
    07/01/09 10:54:18 [2248][1][FATAL] HTTP/1.0 500 Internal Server Error
    Date: Wed, 01 Jul 2009 08:54:34 GMT
    Server: Apache/2.2.3 (CentOS)
    Set-Cookie: PHPSESSID=ph32mgk1lmk17livr0sv1u2ac7; path=/
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Pragma: no-cache
    Content-Length: 0
    Connection: close
    Content-Type: text/html; charset=UTF-8

    What am i doing wrong? I appreciate your help

    Thankssss
    Attached Images Attached Images  
    Kind regards,

    De Kleine Media


    SugarCRM CE v.5.2.0h
    Windows platform
    MySQL v.5.1
    phpMyAdmin - 2.11.2.2
    Apache Server v.2.0

Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Filling in HTML field thorough logic hook problem
    By ishaan in forum Developer Help
    Replies: 6
    Last Post: 2009-06-18, 12:02 PM
  2. How to create custom logic hook?
    By vishwasrao in forum Developer Help
    Replies: 1
    Last Post: 2009-04-15, 11:15 AM
  3. custom buisness logic hook questions
    By jjshoe in forum Developer Help
    Replies: 1
    Last Post: 2009-02-24, 07:20 PM
  4. Getting field name of relationship for logic hook
    By jhermiz in forum Developer Help
    Replies: 1
    Last Post: 2008-12-16, 03:44 PM
  5. Logic Hook! how to merge two field contents
    By kinshibuya in forum Help
    Replies: 4
    Last Post: 2008-09-19, 02:36 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
  •