3

I am seeing a ContentNote trigger called on individual rows even though a bulk insert is done. A stripped down version follows.

The debug log output demonstrates that 3 ContentNote objects are inserted in one DML operation. But I do not understand why the triggers appear to be called 3 times for 1 record each time rather than 1 times for 3 records. It as if this object has some non-bulkified logic behind the scenes. Or I am doing something crazy here?

(Some context: Creation order of ContentNote and ContentDocument and Triggers on ContentObjects.)

Test:

@IsTest
static void contentNoteTrigger() {
    SObject[] cns = new SObject[] {};
    for (Integer i = 0; i < 3; i++) {
        // This property is set to ContentNote.SObjectType
        SObject cn = ContentNotes.noteType.newSObject();
        cn.put('Title', 'T' + i);
        cn.put('Content', Blob.valueOf('B' + i));
        cns.add(cn);
    }
    insert cns;
}

Triggers:

trigger ContentDocumentTrigger on ContentDocument (
        before insert,
        before update,
        before delete,
        after insert,
        after update,
        after delete
        ) {
    cve.Notes.handle(
            Trigger.operationType,
            Trigger.old,
            Trigger.new,
            Trigger.oldMap,
            Trigger.newMap
            );
}

trigger ContentDocumentLinkTrigger on ContentLinkDocument (
        before insert,
        before update,
        before delete,
        after insert,
        after update,
        after delete
        ) {
    cve.Notes.handle(
            Trigger.operationType,
            Trigger.old,
            Trigger.new,
            Trigger.oldMap,
            Trigger.newMap
            );
}

Handler:

public class Notes {
    public static void handle(
            TriggerOperation operation,
            SObject[] oldList,
            SObject[] newList,
            Map<Id, SObject> oldMap,
            Map<Id, SObject> newMap) {

        System.debug('>>>'
                + ' operation ' + operation
                + ' oldList ' + (oldList != null ? oldList.size() : 0)
                + ' oldType ' + (oldList != null ? String.valueOf(oldList[0].getSobjectType()) : 'unknown')
                + ' newList ' + (newList != null ? newList.size() : 0)
                + ' newType ' + (newList != null ? String.valueOf(newList[0].getSobjectType()) : 'unknown')
        );
    }
}

Debug output (with cumulative usage blocks stripped out for brevity):

43.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;NBA,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WAVE,INFO;WORKFLOW,INFO
12:38:57.1 (1416302)|EXECUTION_STARTED
12:38:57.1 (1452174)|CODE_UNIT_STARTED|[EXTERNAL]|01pf0000000x1D6|cve.NoteMigrationBatchableTest.contentNoteTrigger()
12:38:57.1 (16501439)|DML_BEGIN|[610]|Op:Insert|Type:SObject|Rows:3
12:38:57.1 (28098355)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HML|cve.ContentDocumentTrigger on ContentDocument trigger event BeforeInsert|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (31360662)|USER_DEBUG|[357]|DEBUG|>>> operation BEFORE_INSERT oldList 0 oldType unknown newList 1 newType ContentDocument
12:38:57.1 (32242368)|CODE_UNIT_FINISHED|cve.ContentDocumentTrigger on ContentDocument trigger event BeforeInsert|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (54280142)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HML|cve.ContentDocumentTrigger on ContentDocument trigger event AfterInsert|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (54651926)|USER_DEBUG|[357]|DEBUG|>>> operation AFTER_INSERT oldList 0 oldType unknown newList 1 newType ContentDocument
12:38:57.1 (55351761)|CODE_UNIT_FINISHED|cve.ContentDocumentTrigger on ContentDocument trigger event AfterInsert|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (71518588)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HML|cve.ContentDocumentTrigger on ContentDocument trigger event BeforeInsert|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (71812326)|USER_DEBUG|[357]|DEBUG|>>> operation BEFORE_INSERT oldList 0 oldType unknown newList 1 newType ContentDocument
12:38:57.1 (72515949)|CODE_UNIT_FINISHED|cve.ContentDocumentTrigger on ContentDocument trigger event BeforeInsert|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (77836899)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HML|cve.ContentDocumentTrigger on ContentDocument trigger event AfterInsert|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (78075518)|USER_DEBUG|[357]|DEBUG|>>> operation AFTER_INSERT oldList 0 oldType unknown newList 1 newType ContentDocument
12:38:57.1 (78753802)|CODE_UNIT_FINISHED|cve.ContentDocumentTrigger on ContentDocument trigger event AfterInsert|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (85166445)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HML|cve.ContentDocumentTrigger on ContentDocument trigger event BeforeInsert|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (85381693)|USER_DEBUG|[357]|DEBUG|>>> operation BEFORE_INSERT oldList 0 oldType unknown newList 1 newType ContentDocument
12:38:57.1 (86065895)|CODE_UNIT_FINISHED|cve.ContentDocumentTrigger on ContentDocument trigger event BeforeInsert|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (90693473)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HML|cve.ContentDocumentTrigger on ContentDocument trigger event AfterInsert|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (90957612)|USER_DEBUG|[357]|DEBUG|>>> operation AFTER_INSERT oldList 0 oldType unknown newList 1 newType ContentDocument
12:38:57.1 (91653020)|CODE_UNIT_FINISHED|cve.ContentDocumentTrigger on ContentDocument trigger event AfterInsert|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (149520994)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HMQ|cve.ContentDocumentLinkTrigger on ContentDocumentLink trigger event BeforeInsert|__sfdc_trigger/cve/ContentDocumentLinkTrigger
12:38:57.1 (150013102)|USER_DEBUG|[357]|DEBUG|>>> operation BEFORE_INSERT oldList 0 oldType unknown newList 1 newType ContentDocumentLink
12:38:57.1 (150726889)|CODE_UNIT_FINISHED|cve.ContentDocumentLinkTrigger on ContentDocumentLink trigger event BeforeInsert|__sfdc_trigger/cve/ContentDocumentLinkTrigger
12:38:57.1 (223089987)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HMQ|cve.ContentDocumentLinkTrigger on ContentDocumentLink trigger event AfterInsert|__sfdc_trigger/cve/ContentDocumentLinkTrigger
12:38:57.1 (223448677)|USER_DEBUG|[357]|DEBUG|>>> operation AFTER_INSERT oldList 0 oldType unknown newList 1 newType ContentDocumentLink
12:38:57.1 (224168615)|CODE_UNIT_FINISHED|cve.ContentDocumentLinkTrigger on ContentDocumentLink trigger event AfterInsert|__sfdc_trigger/cve/ContentDocumentLinkTrigger
12:38:57.1 (236219488)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HML|cve.ContentDocumentTrigger on ContentDocument trigger event BeforeUpdate|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (236538455)|USER_DEBUG|[357]|DEBUG|>>> operation BEFORE_UPDATE oldList 1 oldType ContentDocument newList 1 newType ContentDocument
12:38:57.1 (237229932)|CODE_UNIT_FINISHED|cve.ContentDocumentTrigger on ContentDocument trigger event BeforeUpdate|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (243689004)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HML|cve.ContentDocumentTrigger on ContentDocument trigger event AfterUpdate|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (243983648)|USER_DEBUG|[357]|DEBUG|>>> operation AFTER_UPDATE oldList 1 oldType ContentDocument newList 1 newType ContentDocument
12:38:57.1 (244778403)|CODE_UNIT_FINISHED|cve.ContentDocumentTrigger on ContentDocument trigger event AfterUpdate|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (257443558)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HMQ|cve.ContentDocumentLinkTrigger on ContentDocumentLink trigger event BeforeInsert|__sfdc_trigger/cve/ContentDocumentLinkTrigger
12:38:57.1 (257708522)|USER_DEBUG|[357]|DEBUG|>>> operation BEFORE_INSERT oldList 0 oldType unknown newList 1 newType ContentDocumentLink
12:38:57.1 (258391299)|CODE_UNIT_FINISHED|cve.ContentDocumentLinkTrigger on ContentDocumentLink trigger event BeforeInsert|__sfdc_trigger/cve/ContentDocumentLinkTrigger
12:38:57.1 (290942926)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HMQ|cve.ContentDocumentLinkTrigger on ContentDocumentLink trigger event AfterInsert|__sfdc_trigger/cve/ContentDocumentLinkTrigger
12:38:57.1 (291266435)|USER_DEBUG|[357]|DEBUG|>>> operation AFTER_INSERT oldList 0 oldType unknown newList 1 newType ContentDocumentLink
12:38:57.1 (292266397)|CODE_UNIT_FINISHED|cve.ContentDocumentLinkTrigger on ContentDocumentLink trigger event AfterInsert|__sfdc_trigger/cve/ContentDocumentLinkTrigger
12:38:57.1 (321547177)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HML|cve.ContentDocumentTrigger on ContentDocument trigger event BeforeUpdate|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (321927536)|USER_DEBUG|[357]|DEBUG|>>> operation BEFORE_UPDATE oldList 1 oldType ContentDocument newList 1 newType ContentDocument
12:38:57.1 (322868304)|CODE_UNIT_FINISHED|cve.ContentDocumentTrigger on ContentDocument trigger event BeforeUpdate|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (328440445)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HML|cve.ContentDocumentTrigger on ContentDocument trigger event AfterUpdate|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (328875032)|USER_DEBUG|[357]|DEBUG|>>> operation AFTER_UPDATE oldList 1 oldType ContentDocument newList 1 newType ContentDocument
12:38:57.1 (329862766)|CODE_UNIT_FINISHED|cve.ContentDocumentTrigger on ContentDocument trigger event AfterUpdate|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (341966647)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HMQ|cve.ContentDocumentLinkTrigger on ContentDocumentLink trigger event BeforeInsert|__sfdc_trigger/cve/ContentDocumentLinkTrigger
12:38:57.1 (342282022)|USER_DEBUG|[357]|DEBUG|>>> operation BEFORE_INSERT oldList 0 oldType unknown newList 1 newType ContentDocumentLink
12:38:57.1 (343016551)|CODE_UNIT_FINISHED|cve.ContentDocumentLinkTrigger on ContentDocumentLink trigger event BeforeInsert|__sfdc_trigger/cve/ContentDocumentLinkTrigger
12:38:57.1 (369675752)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HMQ|cve.ContentDocumentLinkTrigger on ContentDocumentLink trigger event AfterInsert|__sfdc_trigger/cve/ContentDocumentLinkTrigger
12:38:57.1 (370019273)|USER_DEBUG|[357]|DEBUG|>>> operation AFTER_INSERT oldList 0 oldType unknown newList 1 newType ContentDocumentLink
12:38:57.1 (370754442)|CODE_UNIT_FINISHED|cve.ContentDocumentLinkTrigger on ContentDocumentLink trigger event AfterInsert|__sfdc_trigger/cve/ContentDocumentLinkTrigger
12:38:57.1 (380178743)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HML|cve.ContentDocumentTrigger on ContentDocument trigger event BeforeUpdate|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (380497937)|USER_DEBUG|[357]|DEBUG|>>> operation BEFORE_UPDATE oldList 1 oldType ContentDocument newList 1 newType ContentDocument
12:38:57.1 (381197363)|CODE_UNIT_FINISHED|cve.ContentDocumentTrigger on ContentDocument trigger event BeforeUpdate|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (385991302)|CODE_UNIT_STARTED|[EXTERNAL]|01qf00000005HML|cve.ContentDocumentTrigger on ContentDocument trigger event AfterUpdate|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (386509529)|USER_DEBUG|[357]|DEBUG|>>> operation AFTER_UPDATE oldList 1 oldType ContentDocument newList 1 newType ContentDocument
12:38:57.1 (387374558)|CODE_UNIT_FINISHED|cve.ContentDocumentTrigger on ContentDocument trigger event AfterUpdate|__sfdc_trigger/cve/ContentDocumentTrigger
12:38:57.1 (415888379)|CODE_UNIT_FINISHED|cve.NoteMigrationBatchableTest.contentNoteTrigger()
12:38:57.1 (416735350)|EXECUTION_FINISHED
2

I created a Salesforce Case on this and that was handled promptly and effectively.

Unfortunately the answer was that the current implementation supports the "one at a time" situation that arises from the user interface but does not support the "bulk" case as the ContentDocument objects are created one at a time from the ContentNote objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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