Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: after save logic hook going in nested infinit loop

  1. #1
    tomjack is offline Junior Member
    Join Date
    Jul 2010
    Posts
    1

    Default after save logic hook going in nested infinit loop

    I have problem with after save logic hook.
    basically i have, relate fields with contact in opporuntiy and accounts

    on account edit, when i click save,
    following code logic hook will be used to save contact_id_c, contact_id1_c, contact_id2_c of opporunity

    Code:
    function set_contacts_details() {
    		
    			$this->opportunityObj->contact_id_c = $_REQUEST['contact_id_c'];
    			$this->opportunityObj->contact_id1_c = $_REQUEST['contact_id1_c'];
    			$this->opportunityObj->contact_id2_c = $_REQUEST['contact_id2_c'];
    			$this->opportunityObj->save();
    	}
    simillarly on opporuntiy edit, when i click save,
    following code logic hook will be used to save contact_id_c, contact_id1_c, contact_id2_c of acocunt

    Code:
    function set_contacts_details() {
    		
    		if (!empty($this->accountObj)) {	
    			$this->accountObj->contact_id_c = $_REQUEST['contact_id_c'];
    			$this->accountObj->contact_id1_c = $_REQUEST['contact_id1_c'];
    			$this->accountObj->contact_id2_c = $_REQUEST['contact_id2_c'];
    			$this->accountObj->save();
    		}
    	}
    But when I do save on any of account of opporunity save, this goes in infinite loop i,e, more than 100 nested calls and apache crashes / goes down

    any one knows similar issue...what exactly to be done

  2. #2
    eggsurplus's Avatar
    eggsurplus is offline Sugar Community Member
    Join Date
    Dec 2005
    Location
    Minnesota
    Posts
    2,343

    Default Re: after save logic hook going in nested infinit loop

    Check the REQUEST or even the SESSION vars to figure out what triggered the original logic hook. If it's not the originator return so that it doesn't run the logic hook for the child.

  3. #3
    kuske's Avatar
    kuske is offline Sugar Community Member
    Join Date
    Oct 2007
    Location
    Germany
    Posts
    2,597

    Default Re: after save logic hook going in nested infinit loop

    1st LAW of all Sugar hackers:

    YOU MUST NOT call a save() in an after safe logic hook !!!

    If you call a save(), logic hooks for before_safe and after_safe are called.

    So you will come in an endless loop.
    Harald Kuske
    Pre-Sales Engineer Central Europe

    SUGARCRM Deutschland GmbH
    Erika-Mann-Str. 53, 80636 Munich, Germany
    Email: hkuske@sugarcrm.com
    Home: http://www.sugarcrm.com


  4. #4
    eggsurplus's Avatar
    eggsurplus is offline Sugar Community Member
    Join Date
    Dec 2005
    Location
    Minnesota
    Posts
    2,343

    Default Re: after save logic hook going in nested infinit loop

    No way. I disagree. However, you have to be careful to know what's going on behind the scenes. The reason I disagree is that if you don't call the bean's save you'll have to manually mimic any logic hooks for that bean that you need to fire. For example, if you have a hook that attaches a team to a record based on it's parent and you are an account save hook creating a contact automatically you'd want that contact to call the logic hook to attach that team to the contact.

    You can prevent loops but you have to know what to look for.

  5. #5
    abhax's Avatar
    abhax is offline Senior Member
    Join Date
    Jun 2010
    Location
    India
    Posts
    65

    Default Re: after save logic hook going in nested infinit loop

    Quote Originally Posted by kuske View Post
    1st LAW of all Sugar hackers:

    YOU MUST NOT call a save() in an after safe logic hook !!!

    If you call a save(), logic hooks for before_safe and after_safe are called.

    So you will come in an endless loop.


    Hi,

    But whats the solution to stop it from going into loop.
    If sum1 is using the after_save and before_save logic hooks on a custom module?
    If It Is To Be, It Is Up To Me

  6. #6
    eitrix's Avatar
    eitrix is offline Sugar Community Member
    Join Date
    Aug 2010
    Location
    Serbia
    Posts
    396

    Default Re: after save logic hook going in nested infinit loop

    why are you using after_save logic hook?

    better maybe use before_save logic hook, and throw out save function, because you will assign values to fields before everything is saved, and then leave sugar to save it to db

    Haven't ever preferred after_save logic hook, and why would you save something, after it has been saved?
    CRM Software Engineer
    Eontek - www.eontek.rs

  7. #7
    AlexAv's Avatar
    AlexAv is offline Sugar Community Member
    Join Date
    Oct 2009
    Location
    Ukraine
    Posts
    922

    Default Re: after save logic hook going in nested infinit loop

    Hello

    Use SESSION or GLOBALS variables
    Letrium ltd. - Only high quality service
    http://letrium.com

  8. #8
    marklark is offline Sugar Community Member
    Join Date
    Jan 2010
    Location
    Colorado Springs, USA
    Posts
    29

    Default Re: after save logic hook going in nested infinit loop

    More information about using SESSION and GLOBAL variables to enable the use of logic hooks that may cause other logic hooks to fire would be nice.

    Thanks!

  9. #9
    AlexAv's Avatar
    AlexAv is offline Sugar Community Member
    Join Date
    Oct 2009
    Location
    Ukraine
    Posts
    922

    Default Re: after save logic hook going in nested infinit loop

    Something like this

    PHP Code:
    function logic_hook_func($bean$event$arguments) {

      if((isset(
    $_SESSION['check_hook'])) && ($_SESSION['check_hook']=='true'))
      {
         unset(
    $_SESSION['check_hook']);
         
    //not save
       
    }else{
         .....
         
    $_SESSION['check_hook'] = 'true';
         
    $bean->save();
         
    //save data
      
    }



    Last edited by AlexAv; 2011-04-06 at 03:08 PM.
    Letrium ltd. - Only high quality service
    http://letrium.com

  10. #10
    marklark is offline Sugar Community Member
    Join Date
    Jan 2010
    Location
    Colorado Springs, USA
    Posts
    29

    Default Re: after save logic hook going in nested infinit loop

    Hmmmm... Any idea why the following fails when I call $acct_bean->save() ?

    In my Account logic hook (before_save), I have

    if ( $_SESSION['inhook'] == 1 ) {
    $_SESSION['inhook'] = 0; # un-set the flag
    return;
    }
    else if ( $bean->bank_c != $bean->fetched_row['bank_c'] ) {
    # do stuff
    }


    In my Contact logic hook (also before_save), I have

    if ( $give_credit ) {
    $_SESSION['inhook'] = 1;

    $acct_bean = new Account( );
    $acct_bean->retrieve( $bean->account_id );

    $acct_bean->bank_c = $acct_bean->fetched_row['bank_c'] + 1;

    $acct_bean->save();
    }

    thanks again!
    Last edited by marklark; 2011-04-06 at 05:58 PM. Reason: fixing an error

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. save function in logic hook
    By dtronolone in forum Developer Help
    Replies: 0
    Last Post: 2010-06-17, 07:23 PM
  2. logic hook before save syntax
    By tellusmore in forum Help
    Replies: 1
    Last Post: 2009-12-21, 11:18 PM
  3. Notes - pre-save logic hook
    By monicaDC in forum Developer Help
    Replies: 0
    Last Post: 2009-08-23, 10:21 PM
  4. Logic Hook - before save problem
    By kinshibuya in forum Help
    Replies: 7
    Last Post: 2009-07-21, 07:00 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
  •