1

I am having trouble setting the relatedRecordId on ContentDistribution. When I run from the trigger on ContentDocumentLink I get the following error: FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Related Record ID: id value of incorrect type: 0051U000002ZdEiQAK: [RelatedRecordId]. I can run it anonymous fine with no errors. My code is below.

public static List<ContentDistribution> CreateContentDistribution(Id testId) {
        List<ContentDistribution> toBeInserted = new List<ContentDistribution> ();
        List<ContentDocumentLink> cdlList = [select contentdocument.id, contentdocument.title, contentdocument.filetype from contentdocumentlink where linkedentityid = :testId];
        if(cdlList.size() > 0){
            List<Id> cdlIds = new List<Id>();
            for(ContentDocumentLink cdl : cdlList){
                cdlIds.add(cdl.contentdocument.id);
            }
            list<ContentVersion> cvList = [select id from contentversion where contentdocumentid IN :cdlIds];
            System.debug(cvList);
            List<Id> cvIds = new List<Id>();
            for(ContentVersion cv : cvList){
                cvIds.add(cv.Id);
            }
            List<ContentDistribution> cdList = [SELECT ContentVersionId FROM ContentDistribution WHERE ContentVersionId IN :cvIds];
            set<ContentVersion> newList = new Set<ContentVersion>();
            if(cdList.size() == 0){
                newList.addAll(cvList);
            }
            for(ContentVersion cv : cvList){
                List<ContentDistribution> dupe = [SELECT ContentVersionId FROM ContentDistribution WHERE ContentVersionId = :cv.Id];
                if(dupe.size() == 0){
                    newList.add(cv);
                }
            }
            cvList.clear();
            cvList.addAll(newList);
            for(ContentVersion cv : cvList){
                ContentDistribution cd = new ContentDistribution();
                cd.Name = 'Test';
                cd.ContentVersionId = cv.id;
                cd.PreferencesAllowViewInBrowser= true;
                cd.PreferencesLinkLatestVersion=true;
                cd.PreferencesNotifyOnVisit=false;
                cd.PreferencesPasswordRequired=false;
                cd.PreferencesAllowOriginalDownload= true;
                cd.RelatedRecordId = testId;
                toBeInserted.add(cd);
            }
            insert toBeInserted;
            return toBeInserted;
        } else {
            return toBeInserted;
        }
    }

My trigger:

trigger contentDocumentLinkTrigger on ContentDocumentLink (after insert) {
    for(ContentDocumentLink cdl : Trigger.new){
        Id relatedId = cdl.LinkedEntityId;
        Images.CreateContentDistribution(relatedId);
    }
}

1 Answer 1

2

Your error:

FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Related Record ID: id value of incorrect type: 0051U000002ZdEiQAK: [RelatedRecordId]

indicates 0051U000002ZdEiQAK which is a User record ID, is incorrect type.

It's because RelatedRecordId does not accept Id of type User. Please see ContentDistribution Object Reference.

Solution

When uploading a file, Salesforce automatically creates a ContentDocumentLink record for the user as well as the parent object. So what you can do is filter out the records which LinkedEntityId is User Id.

if (testId.getSObjectType() != User.getSObjectType()) {}
}
// or simply
if (String.valueOf(testId).left(3) != '005') {}

Please note that LinkedEntityId can also link to a Group.

1
  • Awesome that worked!! I appreciate it!! Jul 20, 2020 at 2:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .