AnsweredAssumed Answered

Before Save logic hook inserting two records instead of one

Question asked by Mukta Chourishi on Feb 28, 2015
Latest reply on Mar 2, 2015 by Esteban Schmirman
I want to insert a record in a custom table whenever any record is assigned to some user.

I have created a script in custom/modules/InsertNotifications.php and called it using before_save

My code in custom/modules/InsertNotifications.php is

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

class InsertNotification
{
    function InsertNotification(&$bean,$event, $arguments)
    {
       
        if ((!isset($_REQUEST['sugar_body_only']) || $_REQUEST['sugar_body_only'] != true) && $_REQUEST['action'] != 'modulelistmenu' && $_REQUEST['action'] != "favorites" && $_REQUEST['action'] != 'Popup' && empty($_REQUEST['to_pdf']) && (!empty($_REQUEST['module']) && $_REQUEST['module'] != 'ModuleBuilder') && empty($_REQUEST['to_csv']) && $_REQUEST['action'] != 'Login' && $_REQUEST['module'] != 'Timesheets')
        {
            global $current_user, $db;
            $assigned_user_id = $bean->assigned_user_id;
            $bean_id = $bean->id;
            $bean_type = $_REQUEST['module'];
            $id = create_guid();
            $timestamp = TimeDate::getInstance()->nowDb();
           
           
            if (($bean->fetched_row['assigned_user_id'] != $bean->assigned_user_id) && ($current_user->id != $bean->assigned_user_id))
            {
                $sql = "INSERT INTO notification_waiting
                            (id,
                             userid,
                             bean_id,
                             bean_type,
                             date_time,
                             is_notify)
                        VALUES ('$id',
                                '$assigned_user_id',
                                '$bean_id',
                                '$bean_type',
                                '$timestamp',
                                0)";        
               $db->query($sql);
            }
        }
    }
}       


And my custom/modules/logic_hooks.php

<?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();

$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(1, 'Insert into Notification Table', 'custom/modules/InsertNotification.php','InsertNotification', 'InsertNotification');
?>



The problem is with every record assigned the code is inserting two records one has the correct bean id but the other has wrong bean id.

Why is it inserting two records ?

Outcomes