attachmentAV for Salesforce: Developer

You can integrate attachmentAV for Salesforce into your custom processes to scan files on-demand or process scan results with custom logic.

Submit file scans on-demand (#)

For ContentVersion, use the following Apex code:

new attachmentAV.AttachmentAVScanner().submitContentVersionScan(contentVersionId);

For Attachment, use the following Apex code:

new attachmentAV.AttachmentAVScanner().submitAttachmentScan(attachmentId);

What is happening under the hood:

  1. The logic submits the ContentVersion/Attachment for scanning.
  2. The scan is happening asynchronously. When the scan is completed, the attachmentAV__ScanResult__c record linked to the Attachment/ContentVersion object is updated with the scan result.
  3. Scanning mitigation actions and notifications are executed.

If any error occurs, it will be logged in the attachmentAV__ScanResult__c.ProcessingError__c field and the status of the attachmentAV__ScanResult__c will be changed to Failed. attachmentAV__ScanResult__c records are linked to ContentVersion objects indirectly via the attachmentAV__ContentDocumentId__c field and to Attachments via the attachmentav__AttachmentId__c field.

Additionally, there is a field ContentVersion.attachmentAV__ScanResult__c on the ContentVersion object which points to the scan result.

The following code snippet shows an APEX trigger to scan only files attached to Case objects via a certain Digital Experience site.

Here is how to create the trigger.

  1. Setup > Developer Console (or quick search Developer Console)
  2. File > New > Apex Trigger
  3. Enter name: ScanCaseFileAttachments
  4. Select sObject: ContentDocumentLink
  5. Paste the trigger code and click Save.
trigger ScanCaseFileAttachments on ContentDocumentLink (after insert) {
    // Replace with your Digital Experience site's Network Id
    // Find it via: SELECT Id, Name FROM Network
    Id experienceSiteNetworkId = '0DB...';

    Set<Id> contentDocumentIds = new Set<Id>();

    for (ContentDocumentLink cdl : Trigger.new) {
        if (cdl.LinkedEntityId.getSObjectType() == Case.SObjectType) {
            contentDocumentIds.add(cdl.ContentDocumentId);
        }
    }

    if (contentDocumentIds.isEmpty()) {
        return;
    }

    List<ContentVersion> contentVersions = [
        SELECT Id
        FROM ContentVersion
        WHERE ContentDocumentId IN :contentDocumentIds
        AND IsLatest = true
        AND NetworkId = :experienceSiteNetworkId
    ];

    attachmentAV.AttachmentAVScanner scanner = new attachmentAV.AttachmentAVScanner();
    for (ContentVersion cv : contentVersions) {
        scanner.submitContentVersionScan(cv.Id);
    }
}

Receive scan results (#)

Scan results are published as platform events. See Publish platform event for details.

Need help?

Do you have any questions? Please get in touch.

Send email