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:
- The logic submits the ContentVersion/Attachment for scanning.
- The scan is happening asynchronously. When the scan is completed, the
attachmentAV__ScanResult__c recordlinked to the Attachment/ContentVersion object is updated with the scan result. - 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.
- Setup > Developer Console (or quick search
Developer Console) - File > New > Apex Trigger
- Enter name:
ScanCaseFileAttachments - Select sObject:
ContentDocumentLink - 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.