Creating an alert

$alert = BeanFactory::newBean('alerts');
        $alert->name = 'My Alert';
        $alert->description = 'You have been assigned new lead';
        //$alert->url_redirect = 'index.php';
        $alert->target_module = 'Leads';
        $alert->assigned_user_id = $lead->user_id2_c;
        $alert->type = 'info';
        $alert->is_read = 0;
        $alert->save();
        $responseData = [
            'reload' => true,
        ];

i am using this code but is giving an error what is its problem

Which Suite version you are trying in? Please can you share what error messages are.

8.7
unexpected error while calling action

If you are trying to show notifications on saving leads, you can add an ‘after_save’ logic hook where you can add the code to create an alert.
Please refer

Go to dev mode in your app.env and see the text and full stack trace of that error in the browser’s dev tools ,network tab

I tried the following changes on my local suite8 system and was able to get notifications when leads were converted. The same error message was displayed when alert fields were not set. Set the values and there was no error. Please can you follow the steps given here.

  1. Add a ‘after_save’ logic hook to create an alert when a lead is converted.
    File: public\legacy\custom\modules\Leads\logic_hooks.php
$hook_array['after_save'] = Array(); 
$hook_array['after_save'][] = Array(2, 'alert when leads converted', 'custom/modules/Leads/AlertOnConvert.php','AlertOnConvert', 'alertOnConvert');
  1. Create a logic hook code file at public\legacy\custom\modules\Leads\AlertOnConvert.php and copy the following code in it.
<?php
if(!defined('sugarEntry') || !sugarEntry){
	die('Not a valid entry');
}

class AlertOnConvert{
	public function alertOnConvert(SugarBean $bean, $event,$args){
			global $current_user,$timedate;
			if($bean->status == 'Converted'){
				$alert = BeanFactory::newBean('Alerts');
				$alert->name = 'Lead Converted ('.$bean->name.')';
				$alert->description = $bean->description;
				$alert->url_redirect = 'index.php?action=DetailView&module=Leads&record='.$bean->id;
				$alert->target_module = 'Lead';
				$alert->assigned_user_id = $current_user->id;
				$alert->type = 'info';
				$alert->reminder_id='';
				$alert->snooze=$timedate->nowDb();
				$alert->date_start=$timedate->nowDb();
				$alert->is_read = 0;
				$alert->save();
			}
	}
}
  1. Run php bin/console cache:clear and reload the suite8 or login again if the alert icon not visible.
  2. Test if you get alert when you convert a lead. You might have to wait depending upon the snoozing time defined on system settings.

image

2 Likes