Can’t pinpoint what is causing DUPLICATE_VALUE error on contentDistribution insert

I have a custom app that is used for scheduling face-to-face trainings. I’m trying to set up a class that will run daily to gather all trainings scheduled for the next two weeks, and send out training materials in the form of content packs.

I have a custom object, Content_Mapping__c, that ties product record ids to content version record ids. Scheduling is done from a custom object, tracker_record__c (as in Fulfillment Tracker).

    date twoWks = system.today().addDays(14);

    //maps product id to content map record
    map<id, content_mapping__c> conMap = new map<id, content_mapping__c>();

    conDis = new list<ContentDistribution>();

    //list of trainings in the next two weeks for which materials have not been sent
    trList = [SELECT product__r.id, school_district__r.name, comparison_date__c FROM tracker_record__c WHERE comparison_date__c >= :date.today() and comparison_date__c <= :twoWks and materials_sent__c = FALSE and (product_type__c = 'Onsite Training - Full Day' or product_type__c = 'Onsite Training - Half Day')];

    if(trList.size() > 0){
        for(content_mapping__c cm : [SELECT product__r.id, ContentVersionID__c, ContentDocumentID__c FROM content_mapping__c WHERE ContentDocumentID__c != NULL and ContentVersionID__c != NULL]){
            system.debug(cm.product__r.id);
            conMap.put(cm.product__r.id,cm);
        }

        for(tracker_record__c tr : trList){

            //make sure product has materials mapped to it
            if(conMap.get(tr.product__r.id) != NULL ){

                //create content distribution for each tracker record
                ContentDistribution cd = new ContentDistribution();
                cd.Name = 'test';
                cd.ContentVersionId = conMap.get(tr.product__r.id).ContentVersionID__c;
                cd.PreferencesAllowOriginalDownload = true;
                cd.PreferencesAllowPDFDownload = true;
                cd.PreferencesAllowViewInBrowser = true;
                cd.ExpiryDate = tr.Comparison_Date__c.addDays(2);
                cd.PreferencesExpires = true;
                cd.RelatedRecordId = tr.id;
                conDis.add(cd);
            }
        }

        insert conDis;

This fails every time at the insert, with a DUPLICATE_VALUE error on unknown fields for unknown records. Any advice?

UPDATE:

This is my full code I’m running in the dev console.

date twoWks = system.today().addDays(14);

list<tracker_record__c> trList = [SELECT product__r.id, school_district__r.name, comparison_date__c FROM tracker_record__c WHERE comparison_date__c >= :date.today() and comparison_date__c <= :twoWks and materials_sent__c = FALSE and (product_type__c = 'Onsite Training - Full Day' or product_type__c = 'Onsite Training - Half Day')];
list<contentDistribution> conDis = new list<ContentDistribution>();

map<id, content_mapping__c> conMap = new map<id, content_mapping__c>();

if(trList.size() > 0){
    list<content_mapping__c> cm = [SELECT product__r.id, ContentVersionID__c, ContentDocumentID__c FROM content_mapping__c WHERE ContentDocumentID__c != NULL and ContentVersionID__c != NULL];
    for(content_mapping__c c : cm ){
        system.debug(c.product__r.id);
        conMap.put(c.product__r.id,c);
    }

    integer counter = 1;
    for(tracker_record__c tr : trList){
        if(conMap.get(tr.product__r.id) != NULL){
            //create content distribution for each tracker record
            ContentDistribution cd = new ContentDistribution();
            cd.Name = 'test'+counter;
            system.debug(cd.name);
            counter++;
            system.debug(tr.product__r.id);
            cd.ContentVersionId = conMap.get(tr.product__r.id).ContentVersionID__c;
            cd.PreferencesAllowOriginalDownload = true;
            cd.PreferencesAllowPDFDownload = true;
            cd.PreferencesAllowViewInBrowser = true;
            cd.ExpiryDate = tr.Comparison_Date__c.addDays(2);
            cd.PreferencesExpires = true;
            cd.relatedRecordId = tr.id;
            conDis.add(cd);
            system.debug(cd);
        }
    }
insert conDis;
}

And the error:

14:09:09.109
(109319129)|DML_BEGIN|[36]|Op:Insert|Type:ContentDistribution|Rows:2
14:09:09.337 (337716128)|DML_END|[36] 14:09:09.337
(337827278)|EXCEPTION_THROWN|[36]|System.DmlException: Insert failed.
First exception on row 1; first error: DUPLICATE_VALUE, duplicate
value found: duplicates value on record with id: :
[] 14:09:09.338 (338308565)|FATAL_ERROR|System.DmlException: Insert
failed. First exception on row 1; first error: DUPLICATE_VALUE,
duplicate value found: duplicates value on record with id:
: []

Here are the two contentDistribution records that are failing on insert:

DEBUG|ContentDistribution:{Name=test1, ContentVersionId=068m00000009X4zAAE,
PreferencesAllowOriginalDownload=true,
PreferencesAllowPDFDownload=true, PreferencesAllowViewInBrowser=true,
ExpiryDate=2015-06-21 00:00:00, PreferencesExpires=true,
RelatedRecordId=a4S60000000L1cgEAC}

DEBUG|ContentDistribution:{Name=test2,
ContentVersionId=068m00000009X4zAAE,
PreferencesAllowOriginalDownload=true,
PreferencesAllowPDFDownload=true, PreferencesAllowViewInBrowser=true,
ExpiryDate=2015-06-20 00:00:00, PreferencesExpires=true,
RelatedRecordId=a4S60000000KztXEAS}

Answer

I was just running into this issue as well and it appears to be a bug on Salesforce’s side. If you insert the ContentDistribution records one at a time it succeeds. Of course that might not be feasible depending on how many you need to insert.

I created a support case a while back and this is the response I received:

Just wanted to share the update by R&D team. The behavior is Working as Designed however it is not documented anywhere. Thus, they have made a note to share the information with our technical writer to update that ContentDistribution cannot be updated within a second. To simultaneously insert multiple content distribution records, you need to have different content version Ids.

Attribution
Source : Link , Question Author : Jason Kelley , Answer Author : Jeff

Leave a Comment