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
Amidou CisseAmidou Cisse 

Hello Everybody, I need To test this trigger on ConctentDocumentLink : help

//BlockContentLinkTrigger
trigger BlockContentLinkTrigger on ContentDocumentLink (before insert) {  
    for(ContentDocumentLink  contDocL : Trigger.new) {
            contDocL.addError('Pièce joint bloquée depuis objet ContentDocumentLink');
   }  
}

 
Best Answer chosen by Amidou Cisse
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest
private class BlockContentLinkTriggerTest
{
    @isTest
    static void validateDCL()
    {
        ContentVersion cv = new ContentVersion();
        cv.title = 'test content trigger';      
        cv.PathOnClient ='test';  
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body');         
        cv.VersionData =bodyBlob;          
        insert cv; 
        
        ContentDocumentLink contentlink=new ContentDocumentLink();
		contentlink.contentdocumentid=[select contentdocumentid from contentversion where id =: cv.id].contentdocumentid;		
		contentlink.ShareType = 'V';
		
		test.starttest();
		
			try
			{		
				insert contentlink;
			}
			catch(Exception ee)
			{
				
			}
			
		test.stoptest();

    }
}

Let us know if this will help you
 

All Answers

Lokesh KumarLokesh Kumar
HI Cisse,

You have to fire a trigger on ContentVersion and query the ContentDocumentLink for more you can refer below link.

http://salesforce.stackexchange.com/questions/76015/trigger-on-contentdocument-not-working

Thanks
Lokesh
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest
private class BlockContentLinkTriggerTest
{
    @isTest
    static void validateDCL()
    {
        ContentVersion cv = new ContentVersion();
        cv.title = 'test content trigger';      
        cv.PathOnClient ='test';  
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body');         
        cv.VersionData =bodyBlob;          
        insert cv; 
        
        ContentDocumentLink contentlink=new ContentDocumentLink();
		contentlink.contentdocumentid=[select contentdocumentid from contentversion where id =: cv.id].contentdocumentid;		
		contentlink.ShareType = 'V';
		
		test.starttest();
		
			try
			{		
				insert contentlink;
			}
			catch(Exception ee)
			{
				
			}
			
		test.stoptest();

    }
}

Let us know if this will help you
 
This was selected as the best answer
Amidou CisseAmidou Cisse
I still have 0% coverage
Amit Chaudhary 8Amit Chaudhary 8
Your got any error ? Is above trigger is active ?
Amidou CisseAmidou Cisse
Any error and the trigger is active 
Amidou CisseAmidou Cisse
User-added imageUser-added image
Amidou CisseAmidou Cisse
Thanks Every body
It Run finaly, ​The problem came from the condition I made in contenVersion which also blocked contentDocumentLink