function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Archie PangalimanArchie Pangaliman 

How to trigger the bell notification when a file is attached to a task record

How to trigger the bell notification when a file is attached to a task record. We only want the bell notification to trigger when the task is owned by specific role. Thanks
HarshHarsh (Salesforce Developers) 
Hi Archie,
Please follow the following stack exchange link has the same kind of requirement.
https://salesforce.stackexchange.com/questions/331436/bell-notification-when-file-is-attached-to-an-account 

Related links

https://developer.salesforce.com/forums/?id=9062I000000IQBnQAO 

Please mark it as Best Answer if the above information was helpful.

Thanks.
 
Archie PangalimanArchie Pangaliman
Hi Harsh, 

I already saw that discussion. I don't know how to modify the code shared to meet our requiement. Thanks
CharuDuttCharuDutt
Hii Archie 
Try Below Code
trigger taskTrigger on task ( after insert ) {
    set<Id> OwnerId = new set<Id>();
    Map < string, String > mapAccIdTasks = new Map < string, String >();
    CustomNotificationType notificationType = [SELECT Id FROM CustomNotificationType WHERE DeveloperName = 'Desktop'];
    for ( task oTask : trigger.new ) {
 
        OwnerId.add(oTask.ownerid);
        mapAccIdTasks.put(oTask.WhatId,oTask.Subject);

    }

    map<Id,user> userMap = new map<Id,user>([SELECT Id,UserRole FROM User WHERE Id In :OwnerId]);


    for ( task oTask : trigger.new ) {

    if(userMap.ContainsKey(oTask.ownerid)){
    	user oUser = userMap.get(oTask.ownerid);
    	if(oUser.userRole == 'Operations'){

    	

         Messaging.CustomNotification notification = new Messaging.CustomNotification();
        Set < String > recipientsIds = new Set < String >();
        notification.setTitle( 'File Attached to your Account' );
        notification.setNotificationTypeId( notificationType.Id );
    
        
        
            notification.setBody( 'File Id is ' + oTask.Subject );
            notification.setTargetId( oTask.Id );
            recipientsIds.add( oTask.OwnerId );
            notification.send( recipientsIds );
            }
    	}
    }
}



Please Mark It As Best Answer If It Helps
Thank You!