Sugar Notifications in Action

Here is a guest post from Hatim Alam from BHEA, an Elite SugarCRM Partner, and is a certified Sugar Developer Specialist. Hatim shares some ideas for getting creative use out of Sugar Notifications.  Share your own ideas in comments below!

In this post, we are going to look into different scenarios where Sugar Notifications can be of great help. For example, It can be used to notify user when new post or comment has been created on the Activity Stream or on execution of custom job or on triggering of API endpoint or implementing approval/review workflow.

We will create Sugar Notification for each of these scenarios and learn how easy it is to use and implement.



What are Sugar Notifications?

The Notifications indicator is available to all Sugar users and is located on the upper right of the screen next to global search.

The number displayed in the notifications box indicates the number of messages with a status of "Unread". A "0" will appear if there are no unread notifications.

Open the Notifications dropdown and click "View Notifications" in order to see all notifications in a list view.



Create Sugar Notifications on new posts or comments in the Activity Stream

Editor's Note:  Pedro Bazan had published popular a logic hook for adding notifications whenever a user is mentioned in an Activity Stream.

The following steps will create new before_save logic hook on Activities bean within Activity Stream module. Through this logic hook, we will create a new notification whenever a user posts a new comment.

Step 1: Create before_save Logic Hook extension definition

custom/Extension/modules/ActivityStream/Activities/Ext/LogicHooks/create_notification_save.php

create_notification_save.php

<?php
$hook_version = 1;
$hook_array = array();
$hook_array['before_save'] = array();
$hook_array['before_save'][] = array(
1,
'Create Sugar Notification on any new post or comment',
'custom/modules/ActivityStream/Activities/customLogicHook.php',
'SugarNotifyUser',
'notify_user_on_post_comment'
);
?>

Step 2: Create the Logic Hook action method

As defined in our logic hook extension above, now we need to create a file where we can define our logic hook function.  In this action, we will check if any new post or comment has been posted to Activity Stream.

Usually, there are multiple types of activities in Activity Stream - link, unlink, create, update and post. We will be checking if the activity type is a post.  The post activity type is associated with new posts or comments within an Activity Stream.custom/modules/ActivityStream/Activities/customLogicHook.php

customLogicHooks.php

<?php

/**
* Desc: Before save logic hook to create Sugar Notification if new post or comment in Activity Stream
* Written by: Hatim Alam
* Dated: 21st Feb 2016
*/

class SugarNotifyUser {
function notify_user_on_post_comment($bean, $event, $arguments) {
//if activity type is a post
if($bean->activity_type=="post") {
//get the parent bean
$parent_bean = BeanFactory::getBean($bean->parent_type, $bean->parent_id);
//initialize notification bean
$notification_bean = BeanFactory::getBean("Notifications");
//check if comment or a post
$notification_bean->name = ($bean->activity_type=="post" && $bean->comment_count==0) ? "New post on {$bean->parent_type}" : "New comment on {$bean->parent_type}";
$notification_bean->description = "New update has been posted on <a href='#{$bean->parent_type}/{$parent_bean->id}'>{$parent_bean->name}</a>";
//assigned user should be record assigned user
$notification_bean->assigned_user_id = $parent_bean->assigned_user_id;
$notification_bean->parent_id = $bean->parent_id;
$notification_bean->parent_type = $bean->parent_type;
$notification_bean->created_by = $bean->created_by;
//set is_read to no
$notification_bean->is_read = 0;
//set the level of severity
$notification_bean->severity = "information";
$notification_bean->save();
  }
}
}

?>

Create Sugar Notifications to update status of data synchronization

We have many situations where we integrate Sugar with external applications and expose our custom APIs to update data in Sugar as external changes happen. We can use notifications to increase user awareness of these changes. For eg; we can update contact information and notify the assigned user using Sugar Notifications.

Step 1: Create custom endpoint in Sugar

custom/modules/Contacts/clients/base/api/TestContactApi.php

TestContactApi.php

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

class TestContactAPi extends SugarApi
{
public function registerApiRest()
{
return array(
'testContactUpdate' => array(
//request type
'reqType' => 'POST',
//endpoint path
'path' => array('Contacts', 'testContactUpdate'),
//endpoint variables
'pathVars' => array(),
//method to call
'method' => 'testContactUpdate',
//short help string to be displayed in the help documentation
'shortHelp' => 'This is test endpoint to update contact',
//long help to be displayed in the help documentation
'longHelp' => '',
            ),
  );
}

public function testContactUpdate($api, $args)
{
//logic to update your contact info based on arguments received goes here

//if contact updated successfully, notify user
$notification_bean = BeanFactory::getBean("Notifications");
$notification_bean->name = "Contact updated successfully.";
$notification_bean->message = "Details of the update can go here.";
$notification_bean->parent_type = "Contacts";
$notification_bean->parent_id = "{$contact_id}"; //where $contact_id will come as an argument to api
$notification_bean->assigned_user_id = "{$contact_assigned_user_id}"; //where $contact_assigned_user_id is assigned user id of contact
$notification_bean->is_read = 0;
//set the level of severity
$notification_bean->severity = "information";
$notification_bean->save();
}
}

?>

Create Sugar Notifications to implement approval or review workflows

Sugar Notifications can be very handy to implement approval/review workflow and notify a user or a user's manager if any specific condition satisfies. For eg: the assigned user's Manager gets a notification when an opportunity is created with a discount > 25%.

Step 1: Create before_save logic hook extension definition

custom/Extension/modules/Opportunities/Ext/LogicHooks/check_opp_discount.php

check_opp_discount.php

<?php
$hook_version = 1;
$hook_array = array();
$hook_array['before_save'] = array();
$hook_array['before_save'][] = array(
1,
'Check discount percentage on opp save',
'custom/modules/Opportunities/customLogicHooks.php',
'CheckOppDiscount',
'check_opp_discount'
);
?>

Step 2: Create the logic hook action method

Here, we will check if opportunity discount percentage is >25%, we will notify assigned user's Manager using Sugar Notificationscustom/modules/Opportunities/customLogicHooks.php

customOppLogicHooks.php

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

class CheckOppDiscount {
function check_opp_discount($bean, $event, $arguments) {
//check if discount percentage is >25%
if($bean->opp_discount_c > 25) {
//get assigned user bean
$assigned_user_bean = BeanFactory::getBean("Users", $bean->assigned_user_id);
//fetch assigned user's manager
$manager_id = $assigned_user_bean->reports_to_id;
//create sugar notification
$notification_bean = BeanFactory::getBean("Notifications");
$notification_bean->name = 'Opportunity - {$bean->name} is in danger';
$notification_bean->description = 'Discount percentage is more than 25%.';
$notification_bean->parent_id = $bean->id;
$notification_bean->parent_type = 'Opportunities';
$notification_bean->assigned_user_id = $manager_id;
$notification_bean->severity = "warning";
$notification_bean->is_read = 0;
$notification_bean->save();
  }
}
}

?>

Se how easy it was to create Sugar Notifications? Sugar Notifications are a great tool for various other actions as well.  You can use them with custom APIs, logic hooks, Sugar Jobs, etc, in order to notify users of record assignment, new record creation, record update, job status, etc.Try out some ideas on your own and let us know what you come up with in the comments below!



Quick Tip: Check out this SugarCRM Knowledge Base article for adjusting Sugar Notification Delay - Setting Sugar's Notification Delay

Parents Comment Children
No Data