[SalesForce] Apex Trigger on Attachment object not being fired

I wrote this apex trigger for Attachment sObject following the answer here. The trigger basically counts the total number of attachments for a record of type Assignment__c and updates the field Count_Attachment__c. But the trigger doesn't seem to be firing when I add a pdf file to the Notes and Attachments section in a record of custom object type Assignment__c. The Count_Attachment__c field isn't updated and I don't see anything in the logs in developer console. I'd googled and found an old issue with triggers on attachment object dating back to 2010. Is it still an issue?

trigger count_attachements on Attachment (after insert, after update, after delete, after undelete) {
    Set<Id> parentIdsSet = new Set<Id>(); //stores the parentids linked with modified attachments
    List<Attachment> attachmentList = new List<Attachment>(); //stores the list of modified attachments

    if (Trigger.new != null) {
        attachmentList.addAll(Trigger.new);
    } else if (Trigger.old != null){
        attachmentList.addAll(Trigger.old);
    }

    for (Attachment at: attachmentList) {
        parentIdsSet.add(at.ParentId);
    }

    parentIdsSet.remove(null);

    //stores mapping parentIds to total attachment count for that id
    Map<Id, Integer> objecttIdToAttachmentCount = new Map<Id, Integer>();

    for(AggregateResult ar: [SELECT count(Id) attachmentCount, parentId FROM Attachment WHERE parentId IN :parentIdsSet GROUP BY parentId]) {
        Id objId = (Id)ar.get('parentId');
        Integer count = (Integer)ar.get('attachmentCount');

        count = count == null ? 0 : count;
        objecttIdToAttachmentCount.put(objId, count);
    }

    //for cases where parentid's last attachment is deleted as the above SOQL query wont include the deleted attachments
    for(Id parentId: parentIdsSet) {
        if(!objecttIdToAttachmentCount.containsKey(parentId)) {
            objecttIdToAttachmentCount.put(parentId, 0);
        }
    }

    // not necessary but useful if multiple object types in the parent list    
    Map<String, String> sobjectNameToTargetField = new Map<String, String> {
        'Assignement__c' => 'Count_Attachment__c'
    };

    List<SObject> parentRecordsToUpdate = new List<SObject>();

    for (Id parentId: objecttIdToAttachmentCount.keySet()) {
        SObjectType currentType = parentId.getSObjectType();

        if(!sobjectNameToTargetField.containsKey(currentType.getDescribe().getName())) {
            continue;
        }

        SObject parent = currentType.newSObject();
        parent.put(sobjectNameToTargetField.get(currentType.getDescribe().getName()), objecttIdToAttachmentCount.get(parentId));

        parentRecordsToUpdate.add(parent);
    }

    parentRecordsToUpdate.sort();
    update parentRecordsToUpdate;

}

Best Answer

Your trigger is not executing because, as you mention in comments, you're using Lightning Experience. In Lightning Experience, files are attached using the Content system, which creates ContentDocument, ContentVersion, and ContentDocumentLink records. The ContentDocumentLink associates a record to its parent. No Attachment is created in Lightning Experience.

You'll need to write your trigger against ContentDocumentLink. I'd suggest getting started by reading through the SOAP API reference linked above. There are some interesting restrictions around how this object works, especially as to how you can perform queries against it.

Related Topic