0

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

3
  • 1
    Satish, can you share the code you have developed so far? Trigger should be on ContentDocumentLink object. Jan 15 at 19:35
  • If it via trigger, can you pl. share the sample code
    – user91936
    Jan 15 at 21:27
  • 1
    welcome to SFSE, please see How to Ask on getting the most from the Community. You'll need to show what you have tried (use edit )
    – cropredy
    Jan 15 at 22:14
0

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.

2
  • Thank you Duraipandian, I have tried the code sample, but when we are uploading doc getting error(Can not upload)
    – user91936
    Jan 18 at 5:24
  • Set some debug log and see whether the error is from the trigger. Deactivate the Trigger and see whether the issue is happening. Jan 18 at 21:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.