Sugar Notifications in Action

hatimalam —  March 14, 2016 — 8 Comments

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.

new_comment_in_activity_stream

New comment in Activity Stream

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.

sugar_notification_panel

Activity Stream notification in Notifications drop down

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

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

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

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

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 Notifications

custom/modules/Opportunities/customLogicHooks.php

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!

notification_detail_view

Record view for new Activity Stream notification

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

hatimalam

Posts

Open source web developer and nothing much ;)

8 responses to Sugar Notifications in Action

  1. 

    It helped me alot! Thank you so much..

    1 quesiton, as I have implemented your code I can see the notification in notification module. But, Why I didn’t get the Alert? Even the Number of Notifications at top right corner still remains (0).

    • 

      Hi Hammad,

      Glad to know it helped you 🙂
      Please make sure you are not setting is_read=1 or true (by default, it is false or 0).
      Let me know if this works.

  2. 
    Shijin Krishna March 19, 2016 at 9:52 am

    Great Post Hatim!

    Here is another scenario where we can make use of Sugar Notifications.

    Create Sugar Notifications to update the Admin user If any of the Scheduler Job Fails.

    Step1: Job failure Logic Hook Extension Definition

    custom/Extension/modules/SchedulersJobs/Ext/LogicHooks/set_notification_hook.php

    Step 2: Create the logic hook action method
    name = $bean->name.” – failed in cron run”;
    $notification_bean->description = “Oops!! Job (“.$bean->name.”) failed in cron run!;
    $notification_bean->assigned_user_id = ‘1’; //notification will be shown to Admin user
    $notification_bean->severity = “warning”;
    $notification_bean->is_read = 0;
    $notification_bean->save();
    }
    }

    //you can add conditons based on $bean->scheduler_id to set notifications only if a specific job fails.

    Thanks!

  3. 

    Looks like beginning of my previous comment is trimmed off.

    Anyway below is the sugar documentation on how to add a job queue logic hook. You can just add the notification create logic to hook method.

    http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.6/Logic_Hooks/Job_Queue_Hooks/job_failure/

    Thanks!

  4. 

    Hi, I have noticed that you refer to “custom/modules/Opportunities/customLogicHook.php” in “custom/Extension/modules/Opportunities/Ext/LogicHooks/check_opp_discount.php” but the name of the referred file is “custom/modules/Opportunities/customLogicHooks.php” with a ‘s’ at the end of the filename.

    • 

      Hi Artur,

      Thank you for pointing out that typo. I have fixed it now. Logic hook filename should be exactly same as what we declare in logic hook “custom/Extension/modules/Opportunities/Ext/LogicHooks/check_opp_discount.php”.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s