I need to create notifications whenever a file is uploaded to an Account. I need this notification to be shown in the bell icon.
Thanks Satish
I need to create notifications whenever a file is uploaded to an Account. I need this notification to be shown in the bell icon.
Thanks Satish
Check the below code and edit it accordingly for your requirement.
trigger ContentDocumentLinkTrigger on ContentDocumentLink ( after insert ) {
Set < Id > setAccIds = new Set < Id >();
Map < Id, Id > mapAccIdContDocId = new Map < Id, Id >();
for ( ContentDocumentLink objCDL : trigger.new ) {
String strEntityId = objCDL.LinkedEntityId;
if ( String.isNotBlank( objCDL.LinkedEntityId ) && strEntityId.left( 3 ) == '001' ) {
setAccIds.add( objCDL.LinkedEntityId );
mapAccIdContDocId.put( objCDL.LinkedEntityId, objCDL.ContentDocumentId );
}
}
if ( setAccIds.size() > 0 ) {
CustomNotificationType notificationType = [SELECT Id FROM CustomNotificationType WHERE DeveloperName = 'Desktop'];
Messaging.CustomNotification notification = new Messaging.CustomNotification();
Set < String > recipientsIds = new Set < String >();
notification.setTitle( 'File Attached to your Account' );
notification.setNotificationTypeId( notificationType.Id );
for ( Account objAcc : [ SELECT Id, OwnerId FROM Account WHERE Id IN: setAccIds ] ) {
notification.setBody( 'File Id is ' + mapAccIdContDocId.get( objAcc.Id ) );
notification.setTargetId( objAcc.Id );
recipientsIds.add( objAcc.OwnerId );
notification.send( recipientsIds );
}
}
}
Let me know if you have additional questions.