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

Thread: Please check my logic hook code - Not Firing

  1. #1
    bstonehill is offline Sugar Community Member
    Join Date
    Nov 2006
    Location
    Atlanta, GA
    Posts
    98

    Default Please check my logic hook code - Not Firing

    Here's the situation:

    I have 3 modules (2 custom) with relationships:
    Accounts one-to-many with Items Module
    Accounts one-to-many with DVR Module
    DVR one-to-many with Items Module

    DVR is basically a container for Items.

    I need to change the account for each item related to DVR to match whenever the account is changed on the DVR using a logic_hook. I'm thinking this would best be done using after_save. I'm sure there is an easier way to do this by referencing the subpanel beans, but I can't figure out how to reference those. Here is the code I have so far. By the way I'm using 5.5.0 CE Stack Installer.

    Logic Hook:

    Code:
    <?php
      //Logic Hook to change item's account relationship when its related DVR account when relationship is changed
      
      $hook_version = 1;
      
      $hook_array['after_save'][] = Array(1, 'itemMover', 'custom/customCode/move_items.php', 'itemMover', 'moveItems');
    ?>
    PHP Code:

    Code:
    <?php
    //if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    
    
      class itemMover {
        
        function moveItems(&$bean, $event, $arguments){
          
          //Check if Account Field changed
          //if($bean->account_id != $bean->fetched_row['account_id']){} //Will implement when everything else is working     
          
          //Get items attached to DVR
          $i = 0;
          $item_array = Array();
          $id = $bean->id;
          $acct_id = $bean->account_id;
          
          $query = "SELECT * FROM inv_dvrs_inv_items WHERE inv_dvrs_i38aenv_dvrs_ida = '".$id."' AND deleted = 0";
          
          $results = $bean->db->query($query, true);
          while($row = $bean->db->fetchByAssoc($results)){
            $item_array[$i] = $row['inv_dvrs_iaf07v_items_idb'];
            $i++;
          }
          
          //Set Account for items to match DVR account
          foreach($item_array as $item){
             $query = "UPDATE inv_items_accounts_c SET inv_items_5455ccounts_ida = '".$acct_id."' WHERE inv_items_bf35v_items_idb = '".$item."'";
          }
          
        }     
      }
    ?>
    I've successfully implemented logic_hooks to format Account text fields to upper case and to format phone numbers before_save and I'm using these as a template, however my code in this case is much more complex. I'm also not sure how to see what is contained in the $bean array to troubleshoot. I've tried setting the description field to $bean->description = print_r($bean) to see the contents but no luck, not even if specify the value $bean->description = '1234'. Any help would be greatly appreciated.

    Also, not sure if this makes a difference here or not, but when opening a custom popup via a button (using Enhanced Studio) .htaccess would not allow it because /modules/ was forbidden and the path was custom/modules/. I had to move the popup file backwards into custom/. I don't really think this is the case here because my other logic_hooks execute but I figured it was worth mentioning.
    Last edited by bstonehill; 2010-01-21 at 02:04 PM.
    Sugar Version 5.1.0a
    PHP 5.2.5
    MySQL 5.0.37

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

    Default Re: Please check my logic hook code - Not Firing

    Is it not firing period? Try renaming the file to itemMover.php and then change the hook array to:

    PHP Code:
      $hook_array['after_save'][] = Array(1'moveItems''custom/customCode/itemMover.php''itemMover''moveItems'); 

  3. #3
    bstonehill is offline Sugar Community Member
    Join Date
    Nov 2006
    Location
    Atlanta, GA
    Posts
    98

    Default Re: Please check my logic hook code - Not Firing

    Sorry, my path to the move_items.php was incorrect in logic_hooks. It's firing now but I'm still having issues with the code. I found the get_linked_beans() property which returns all items related to a record. This should be a better way to accomplish this rather than querying the database directly, but the documentation is not real clear on how to use it. Do I load it into an array like
    Code:
     $items_array = get_linked_beans($field_name, $bean_name, $sort_array = array(), $begin_index=0, $end_index = -1, $deleted=0, $optional_where="");
    or just
    Code:
     get_linked_beans($field_name, $bean_name, $sort_array = array(), $begin_index=0, $end_index = -1, $deleted=0, $optional_where = "");
    and if so how do I reference the results? I'm also not clear on if I need to replace any of the variables or if they are pre-defined.

    Either one of these causes a white screen after saving.
    Sugar Version 5.1.0a
    PHP 5.2.5
    MySQL 5.0.37

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

    Default Re: Please check my logic hook code - Not Firing

    Try:

    PHP Code:
            $related_items $bean->get_related_list(new inv_dvrs_inv_items(),"inv_dvrs_inv_items");
            foreach(
    $related_items['list'] as $key=>$related_bean) {
                            echo 
    "related id: ".$related_bean->id."<br/>";
            } 
    You may need to tweak it to work for you though.

  5. #5
    bstonehill is offline Sugar Community Member
    Join Date
    Nov 2006
    Location
    Atlanta, GA
    Posts
    98

    Default Re: Please check my logic hook code - Not Firing

    Hmmm, I tried it but the first line causes me to just get a white screen after saving just like the others.

    Code:
    $related_items = $bean->get_related_list(new inv_dvrs_inv_items(),"inv_dvrs_inv_items");
    but if I comment out that line the page loads properly after saving.
    Sugar Version 5.1.0a
    PHP 5.2.5
    MySQL 5.0.37

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

    Default Re: Please check my logic hook code - Not Firing

    You may need to do a require_once to the path of that bean file. Make sure that the bean name is correct to for the new inv_dvrs_inv_items() part.

  7. #7
    bstonehill is offline Sugar Community Member
    Join Date
    Nov 2006
    Location
    Atlanta, GA
    Posts
    98

    Default Re: Please check my logic hook code - Not Firing

    Sorry, I"m sure this is a newbie question but I'm still stuck. Which file is the bean file? The module is called DVRs and because it is a custom module and part of a project it is prefixed with inv_. Would that be modules/inv_DVRs/inv_DVRs.php? That appears to be where the class is declared but that extends inv_DVRs_sugar.php. I tried it with both but still have the same white screen problem.

    Also, how do I get the bean name? inv_dvrs_inv_items is the relationship name. Should I use that or just the module inv_DVRs or something else entirely? new inv_DVRs ? I've tried that as well but still no luck.
    Sugar Version 5.1.0a
    PHP 5.2.5
    MySQL 5.0.37

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

    Default Re: Please check my logic hook code - Not Firing

    It looks like you've got that part. The bean name is the "class" name in inv_DVRs.php. Try that same name for the second parameter of the get_related_list function. I haven't tried this with a custom module but it should work.

    Anyone else have any further insight?

  9. #9
    bstonehill is offline Sugar Community Member
    Join Date
    Nov 2006
    Location
    Atlanta, GA
    Posts
    98

    Default Re: Please check my logic hook code - Not Firing

    Quote Originally Posted by eggsurplus View Post
    It looks like you've got that part. The bean name is the "class" name in inv_DVRs.php. Try that same name for the second parameter of the get_related_list function. I haven't tried this with a custom module but it should work.

    Anyone else have any further insight?
    Thanks for all your help, I've at least got a better understanding than I did going in.

    I've managed to at least find an example that doesn't cause the white screen problem, it's just not updating the related account field. I found an example that does exactly what I'm trying to do except with the contacts and opportunities modules instead of a custom one at http://www.jjwdesign.com/blog/2009/0...gn-logic-hook/. It appears at least in this case I needed to be using the child (inv_items) rather than inv_DVRs. I also noticed that in the class definition it was inv_Items rather than inv_items, not sure if this is case sensitive. In any case when I changed to the code below it no longer crashes, now I just need to get it to update properly.

    Code:
          $acct_id = $bean->account_id;
          
          require_once('modules/inv_items/inv_items.php');
          $items = $bean->get_linked_beans('inv_dvrs_inv_items', 'inv_Items');
          foreach ($items as $item){
            //$bean->description = $item->account_id;
            $item->account_id = $acct_id;
          }
    I'm trying to see what's in the $item array but when I do $bean->description = print_r($item); all I get is the number 1. I changed the hook to before_save rather than after_save in order to see data in the description field by the way. I also double-checked that account_id was a valid field in the items module.
    Last edited by bstonehill; 2010-01-21 at 07:41 PM.
    Sugar Version 5.1.0a
    PHP 5.2.5
    MySQL 5.0.37

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

    Default Re: Please check my logic hook code - Not Firing

    To dump it as a string try:
    $bean->description = print_r($item,true);

    Looks like you're getting closer. After doing $item->account_id = $acct_id; make sure to save it so that it saves the new account id: $item->save();

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. Logic Hook
    By Aespejo in forum Español
    Replies: 11
    Last Post: 2012-03-05, 11:27 AM
  2. Logic Hook - Firing just once
    By leowolff in forum Help
    Replies: 2
    Last Post: 2010-01-20, 07:40 PM
  3. Replies: 3
    Last Post: 2009-08-15, 03:00 AM
  4. Logic Hook
    By sacramentojoe in forum Help
    Replies: 15
    Last Post: 2007-07-31, 11:30 PM
  5. Check and restore original code
    By george_bbch in forum Help
    Replies: 0
    Last Post: 2006-07-27, 01:38 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
  •