2

I'm attempting to create a trigger on content delivery (ContentDistribution). However, my trigger isn't being fired when ContentDistribution records are created in an Apex controller for a VisualForce page. If they are created from the UI/UX or from execute anonymous, the trigger runs fine.

Apex creation:

ContentDistribution distribution = new ContentDistribution();
distribution.Name = newVersion.Title__c + '-' + Date.today();
distribution.ContentVersionId = newVersion.Id;
distribution.PreferencesNotifyOnVisit = false;
distribution.PreferencesNotifyRndtnComplete = false;

try {
    insert distribution;
}
catch (DmlException ex) {

}

Trigger:

trigger ContentDistributionTrigger on ContentDistribution (after insert) {
    system.debug('~~~~~ in trigger');
}

Debug log from Apex insert (notice no trigger call!):

16:33:54.0 (164541755)|DML_BEGIN|[119]|Op:Insert|Type:ContentDistribution|Rows:1
16:33:54.0 (164568849)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
16:33:54.0 (207713301)|DML_END|[119]

Debug log from UI:

16:30:48.0 (286152)|EXECUTION_STARTED
16:30:48.0 (312575)|CODE_UNIT_STARTED|[EXTERNAL]|VisualForce View State
16:30:48.0 (8178812)|CODE_UNIT_FINISHED|VisualForce View State
16:30:48.0 (9061246)|EXECUTION_FINISHED
16:30:48.109 (109687142)|CODE_UNIT_STARTED|[EXTERNAL]|01qXXXXXXXXXXXX|TestTrigger on ContentDistribution trigger event AfterInsert for [05DXXXXXXXXXXXX]

VF code:

<apex:page controller="XXXXX" showHeader="true" sidebar="false">
<apex:form >
    <apex:pageMessages />
    <apex:pageBlock >
        <apex:pageBlockSection columns="1">
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="File" for="file" />
                <apex:inputFile value="{!VersionData}" fileName="{!PathOnClient}" id="file"/>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
    <apex:pageBlock >
        <apex:commandButton action="{!upload}" value="Upload New Version" />
        <apex:commandButton action="{!cancel}" value="Cancel"/>
    </apex:pageBlock>
</apex:form>

Controller code:

public without sharing class XXXXX {

public Blob VersionData {get; set;}
public String PathOnClient {get; set;}

private ContentVersion newVersion;
private String versionId;
private String objectId;
private ContentVersion currentContent;
private List<ContentVersion> objectContent;

public XXXXX() {
    this.versionId = ApexPages.currentPage().getParameters().get('id');
    this.objectId = ApexPages.currentPage().getParameters().get('objId');

    this.newVersion = new ContentVersion();

    this.currentContent = getCurrentContent(this.versionId);
    this.objectContent = getObjectContent(this.objectId);
}

public PageReference upload() {

    if (!doesFileMatch()) {
        this.VersionData = null;
        return null;
    }

    for (ContentVersion content : this.objectContent) {
        if (content.PathOnClient == currentContent.PathOnClient) {
            this.newVersion.Title = content.Title;
            this.newVersion.TagCsv = content.TagCsv;
            this.newVersion.Title__c = content.Title__c;
            this.newVersion.Document_Type__c = content.Document_Type__c;
            this.newVersion.Document_Purpose__c = content.Document_Purpose__c;
            this.newVersion.ContentDocumentId = content.ContentDocumentId;
            this.newVersion.VersionData = this.VersionData;
            this.newVersion.PathOnClient = this.PathOnClient;
            this.newVersion.Display_On_List__c = true;
        }
    }

    this.VersionData = null;

    try {
        insert this.newVersion;
    }
    catch (DmlException ex) {
        return null;
    }

    createContentDelivery();
    return null;
}

private ContentVersion getCurrentContent(String versionId) {
    return [SELECT Id, PathOnClient, ContentDocumentId FROM ContentVersion WHERE Id = :versionId];
}

private List<ContentVersion> getObjectContent(String objectId) {
    List<ContentVersion> objectContent = new List<ContentVersion>();
    List<Id> objectContentIds = new List<Id>();
    List<ContentDistribution> deliveredContent = [SELECT ContentVersionId FROM ContentDistribution WHERE RelatedRecordId = :objectId];

    for (ContentDistribution content : deliveredContent) {
        objectContentIds.add(content.ContentVersionId);
    }

    if (!objectContentIds.isEmpty()) {
        objectContent = [SELECT Id, Title, TagCsv, ContentDocumentId, PathOnClient, Title__c, Document_Type__c, Document_Purpose__c FROM ContentVersion WHERE Id IN :objectContentIds];
    }

    return objectContent;
}

private Boolean doesFileMatch() {
    if (this.PathOnClient == this.currentContent.PathOnClient) {
        return true;
    }

    return false;
}

public void createContentDelivery() {
    ContentDistribution distribution = new ContentDistribution();
    distribution.Name = newVersion.Title__c + '-' + Date.today();
    distribution.ContentVersionId = newVersion.Id;
    //distribution.RelatedRecordId = this.objectId;
    distribution.PreferencesNotifyOnVisit = false;
    distribution.PreferencesNotifyRndtnComplete = false;

    try {
        insert distribution;
    }
    catch (DmlException ex) {
    }
}
}
  • How are you creating those records through Apex? Kindly share relevant portion of your apex code. – Mahmood Nov 8 '16 at 21:47
  • I've added the relevant code. – shuxrei Nov 8 '16 at 21:51
  • Is ContentDistribution record created successfully using above APEX call? Also check if your trigger is "active". – Mahmood Nov 8 '16 at 21:55
  • Trigger is active and content delivery is created successfully. It works when creating a content delivery via the UI just fine. – shuxrei Nov 8 '16 at 21:57
  • I've added the relevant debug log outputs as well. I'm about 99% sure this is a Salesforce bug. – shuxrei Nov 8 '16 at 22:01
0

There are no bugs in Salesforce.

I have used this code to run from Developer console and trigger on ContentDistribution is perfectly firing.

First, created a ContentVersion and followed by ContentDistribution (same as your code)

ContentVersion newVersion = new ContentVersion();
newVersion.Title = 'my version title';
newVersion.pathOnClient = 'C:/Santanu/Plan.txt';
newVersion.VersionData=blob.valueof('test data');
insert newVersion;

ContentDistribution distribution = new ContentDistribution();
distribution.Name = newVersion.title + ' ' +  Date.today();
distribution.ContentVersionId = newVersion.Id;
distribution.PreferencesNotifyOnVisit = false;
distribution.PreferencesNotifyRndtnComplete = false;

try {
    insert distribution;
}
catch (DmlException ex) {

}

Update based on your code

I have checked that trigger is firing properly.

In the upload method, certain condition is not satisfying. Thats why you are unable to get the trigger log.

Suggest you to put debug statements in these lines:

  1. before and after this line if (!doesFileMatch())

  2. Inside the for loop after this line if (content.PathOnClient == currentContent.PathOnClient)

  3. Before and after insert this.newVersion; line

  4. Inside createContentDelivery method.

  • This does not resolve my issue. The trigger is still not firing from a controller on a VisualForce page. – shuxrei Nov 9 '16 at 15:14
  • Can you please post VFP and controller code – Santanu Boral Nov 9 '16 at 16:01
  • Posted. I've tried to move the creation to both a future method and a batch for testing, and none of them fired the trigger. So far I've only been able to successfully fire the trigger from the UI or execute anonymous. – shuxrei Nov 9 '16 at 18:03
  • find my updated answer – Santanu Boral Nov 9 '16 at 22:57
  • Your answer still does not resolve my issue. Find the debug logs in the original post. From the controller, the ContentDistribution is being inserted but the trigger is never firing. I know what my code is doing. It is all running, but the trigger is never being fired from the Apex controller. I've also tried moving my createContentDelivery method to @future and passing in the ContentVersion id, but that also did not fire the trigger. Same with moving to a batch in a wild attempt at closure on this issue. Nothing has fired the trigger except the UI and execute anon for me. – shuxrei Nov 10 '16 at 14:52

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.