logic hooks popup alert

Hello,

Please help me solution to display popup alert by logic hooks

This logic hooks after save of Case, when Case set to Closed but Task in subpanel is not Completed, the logic hooks will popup alert and restrict save.

Thanks 

  • Hi Bhavesh Patel,

    My logic hooks is working but this code dose not prevent Case save, I received error message: PHP Notice:  Undefined variable: extension

    public function CheckTask(SugarBean $bean, $event, $args)
        {
        global $sugar_config, $db,$current_user;
           //check Task status
           $id = $bean->id;
           $status =  $bean->status;
           $GLOBALS['log']->test('check: '.$status);
           $query = "SELECT status FROM tasks WHERE parent_id = '$id' AND deleted = '0'";
           $result = $db->query($query,true);
           $rows = $db->fetchByAssoc($result);
           $check = 0;
           foreach ($rows as $row) {
              if ($row['status'] != 'Completed') {
              $check = 1;
              $GLOBALS['log']->fatal('check: '.$status);
              break;
             } 
           }
           if ($check == 1 && $status = 'Closed') {
             throw new SugarApiExceptionInvalidParameter(string_format(
                            $GLOBALS['app_strings']['LBL_UPLOAD_IMAGE_FILE_NOT_SUPPORTED'],
                            array($extension)
                        ));
           }
        }
  • Hi Bao,

    Firstly, you've got a mistake '

           foreach ($row as $rows) {

    It should be:

           foreach ($rows as $row) {

    Secondly what is your logging output? Also, try putting in  $GLOBALS['log']->test(print_r($rows, true));  and let me know what the output of the rows are. Try putting in logging everywhere and try and solve it that way.   

  • Hi Bao Tran Hoang,

    That is not PHP Notic. You need to remove that extension variable.

    throw new SugarApiExceptionInvalidParameter(string_format(
         $GLOBALS['app_strings']['SET_YOUR_CUSTOM_MESSAGE'],''));

    This way you can prevent the save.and got red alert pop-up.

    Below is the second way that you can use.

    http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.6/UI_Model/Views/Examples/Adding_Field… 

  • Thanks Bhavesh Patel

    I updated my code and add string LBL_CASE_NOT_CLOSED to lang, I received message in error PHP Notice:  Undefined index: LBL_CASE_NOT_CLOSED. Anh receive red alert  A parameter in your request was invalid.

    public function CheckTask(SugarBean $bean, $event, $args)
        {
        global $sugar_config, $db,$current_user;
           //check Task status
           $id = $bean->id;
           $status =  $bean->status;
           $GLOBALS['log']->test('check: '.$status);
           $query = "SELECT status FROM tasks WHERE parent_id = '$id' AND deleted = '0'";
           $result = $db->query($query,true);
           $rows = $db->fetchByAssoc($result);
           $check = 0;
           foreach ($rows as $row) {
              if ($row['status'] != 'Completed') {
              $check = 1;
              $GLOBALS['log']->fatal('check: '.$status);
              break;
              }
           }
           if ($check == 1 && $status = 'Closed') {
             throw new SugarApiExceptionInvalidParameter(string_format(
                  $GLOBALS['app_strings']['LBL_CASE_NOT_CLOSED'],''));
           }
        }
  • Thank you very much, I updated my code below but it dose not work

  • I'm not sure about this 'throw new SugarApiExceptionInvalidParameter'. I'd use a sugar_die instead (I've mentioned this before). For example, using the code above:

    public function CheckTask(SugarBean $bean, $event, $args)
        {
        global $sugar_config, $db,$current_user;
           //check Task status
           $id = $bean->id;
           $status =  $bean->status;
           $GLOBALS['log']->test('check: '.$status);
           $query = "SELECT status FROM tasks WHERE parent_id = '$id' AND deleted = '0'";
           $result = $db->query($query,true);
           $rows = $db->fetchByAssoc($result);
           $check = 0;
           foreach ($rows as $row) {
              if ($row['status'] != 'Completed') {
              $check = 1;
              $GLOBALS['log']->fatal('check: '.$status);
              break;
              }
           }
           if ($check == 1 && $status = 'Closed') {
              sugar_die('Case Not Closed');
           }
        }

    If you want to carry on using SugarApiExceptionInvalidParameter, I'd recommend creating a new label for LBL_CASE_NOT_CLOSED.

  • hi Alan Apter

    I used your mentioned but I received red alert There was an error while connecting to the server. Please try again.

  • You could check the apache error logs. Or you're going to have to take things out line-by-line and figure out what's causing the problem. First take out the sugar_die line. Then 

           if ($check == 1 && $status = 'Closed') {        
           }

    Etc etc.

  • Hi Alan Apter,

    The apache is no error logs. If I take out the sugar_die is no red alert. 

    Thank for your help

  • Hi Alan Apter,

    The status is dropdown so I can not get value setected by $bean

    But I did not know to use $_POST in logic hooks

    Please help me