function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DJ 367DJ 367 

before trigger on ContentDocumentLink throwing error

Hello All,

I am writting trigger on ContentDocumentLink but while uploading file I am getting below error. need help please.

User-added image
 
trigger tesDocUpload on ContentDocumentLink (before insert) {
	List<string> strIds = trigger.newmap.keyset() ;
	 List<ContentDocumentLink> lstContDoc= [Select Id,ContentDocumentId From ContentDocumentLink where LinkedEntityId IN: strIds ];
	
		for(ContentDocumentLink cd : lstContDoc){
			if(lstContDoc.size() > 3){
				cd.addError('Only 3 files can be uploaded!');
			}
		}
}

 
Naveen KNNaveen KN
can you check the size with System.debug(lstContDoc.size()); before the for loop in this error scenario.

--
Naveen
DJ 367DJ 367
size is 0
Naveen KNNaveen KN
In that case, I think it is failing at the for loop. 
 
Add null check to for loop and test with a different set of records[with size of 0, 2 and 3] and see if it is throwing that error. 

Hope it will resolve your issue. Update the status. 

--
Naveen 
Team codengine.in 
DJ 367DJ 367
but why it is zero.
Deepali KulshresthaDeepali Kulshrestha
Hi DJ,

trigger tesDocUpload on ContentDocumentLink (before insert) {
    try{
        Set<Id> strIds = trigger.newmap.keyset() ;
        List<ContentDocumentLink> lstContDoc= [Select Id,ContentDocumentId From ContentDocumentLink where LinkedEntityId IN: strIds ];
        System.debug('lstContDoc-->'+lstContDoc);
        for(ContentDocumentLink cd : lstContDoc){
            if(lstContDoc.size() > 3){
                cd.addError('Only 3 files can be uploaded!');
            }
        }
    }catch (DmlException dee)
        {
            System.debug('DmlException Message' + dee.getLineNumber() + 'DmlException Message = ' +dee.getMessage());

        }catch (Exception ee) {
            System.debug('Exception Message' + ee.getLineNumber() + 'Exception Message = ' + ee.getMessage());
        }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha