+ Start a Discussion
Amidou CisseAmidou Cisse 

trigger to upload file on library : help

error
//Trigger
trigger BlockContentLtrigger on ContentDocumentLink (before insert) 
{  
    for(ContentDocumentLink  contDocL : Trigger.new)
    {
      if (CDLHelper.isSharingAllowed(contDocL))
     {
          contDocL.addError('stop3 ');        
     }
   }  
}

//Class
public class CDLHelper {
    public static boolean isSharingAllowed(ContentDocumentLink cdl) {
        String docId = cdl.ContentDocumentId;  
        
        ContentVersion version = [select PublishStatus, FileExtension from ContentVersion where ContentDocumentId =: docId].get(0);        
       
        if (version.PublishStatus.equals('P') && (version.FileExtension != null)) {
            return false;
        }
        return true;
    }
}

 
Best Answer chosen by Amidou Cisse
rajat Maheshwari 6rajat Maheshwari 6

Hi Amidou,

Please use below and let me know the results :)

//Trigger
trigger BlockContentLtrigger on ContentDocumentLink (before insert) 
{  
    for(ContentDocumentLink  contDocL : Trigger.new)
    {
      if (CDLHelper.isSharingAllowed(contDocL))
     {
          contDocL.addError('stop3 ');        
     }
   }  
}

//Class
public class CDLHelper {
    public static boolean isSharingAllowed(ContentDocumentLink cdl) {
       // String docId = cdl.ContentDocumentId;  
        
        ContentVersion version = [select PublishStatus, FileExtension from ContentVersion where ContentDocumentId =: cdl.ContentDocumentId limit 1];        
       
        if (version.PublishStatus.equals('P') && (version.FileExtension != null)) {
            return false;
        }
        return true;
    }
}

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com

All Answers

rajat Maheshwari 6rajat Maheshwari 6

Hi Amidou,

Please use below and let me know the results :)

//Trigger
trigger BlockContentLtrigger on ContentDocumentLink (before insert) 
{  
    for(ContentDocumentLink  contDocL : Trigger.new)
    {
      if (CDLHelper.isSharingAllowed(contDocL))
     {
          contDocL.addError('stop3 ');        
     }
   }  
}

//Class
public class CDLHelper {
    public static boolean isSharingAllowed(ContentDocumentLink cdl) {
       // String docId = cdl.ContentDocumentId;  
        
        ContentVersion version = [select PublishStatus, FileExtension from ContentVersion where ContentDocumentId =: cdl.ContentDocumentId limit 1];        
       
        if (version.PublishStatus.equals('P') && (version.FileExtension != null)) {
            return false;
        }
        return true;
    }
}

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
This was selected as the best answer
Amidou CisseAmidou Cisse
Thanks Rajat,

I tried but I still have in the console an error with this line, which makes that I can not cover 100% my trigger.
//Trigger
trigger BlockContentLtrigger on ContentDocumentLink (before insert) 
{  
    for(ContentDocumentLink  contDocL : Trigger.new)
    {
      if (CDLHelper.isSharingAllowed(contDocL))
     {
          contDocL.addError('stop3 ');        
     }
   }  
}

//Class
public class CDLHelper {
    public static boolean isSharingAllowed(ContentDocumentLink cdl) {
       // String docId = cdl.ContentDocumentId;  
        
        ContentVersion version = [select PublishStatus, FileExtension from ContentVersion where ContentDocumentId =: cdl.ContentDocumentId limit 1];        
       
        if (version.PublishStatus.equals('P') && (version.FileExtension != null)) {
            return false;
        }
        return true;
    }
}
" System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, BlockContentLtrigger: execution of BeforeInsert
caused by: System.QueryException: List has no rows for assignment to SObject
Class.CDLHelper.isSharingAllowed: line 19, column 1
Trigger.BlockContentLtrigger: line 4, column 1: [] "
@IsTest
public class TestBlockContentLtrigger {
    @IsTest
    static void methodContentLtrigger()  {  
            ContentDocumentLink ordreCL = new ContentDocumentLink();
    Test.StartTest(); 
       if (CDLHelper.isSharingAllowed(ordreCL)) {
        for(Task task : [select Id, WhatId from Task where Id IN : setTaskIds]){           
            if(task.whatId != null){                    
                for (ContentDocumentLink dlink : doclinks){
                    if (dlink.LinkedEntityId == task.Id){
                        doclink.ContentDocumentId= dlink.ContentDocumentId;
                        doclink.LinkedEntityId = task.WhatId;
                        doclink.ShareType = 'V';
                        doclink.Visibility = 'InternalUsers';
                        doclinkL.add(doclink);
                        insert ordreCL;
                        ordreCL.addError('Pièce joint bloquée depuis objet ContentDocumentLink');
                     }
                }
            }
        }  
        insert doclinkL;     
    }
    Test.StopTest();
  }
User-added image
rajat Maheshwari 6rajat Maheshwari 6

@Amidou,

The error, seems that , The below query try to tetrive records, but there is no record in database.Please use debugging to check
ContentVersion version = [select PublishStatus, FileExtension from ContentVersion whereContentDocumentId =: cdl.ContentDocumentId limit 1];

Thanks

Amidou CisseAmidou Cisse
Hi Rajat, 

//BlockContentLtrigger
trigger BlockContentLtrigger on ContentDocumentLink (before insert) {  
    for(ContentDocumentLink  contDocL : Trigger.new) {
      if (CDLHelper.isSharingAllowed(contDocL)) {
          contDocL.addError('stp3');
     }
   }  
}

//CDLHelper
public class CDLHelper {
    public static boolean isSharingAllowed(ContentDocumentLink cdl) {
       // String docId = cdl.ContentDocumentId;        
        ContentVersion version = [select PublishStatus, FileExtension from ContentVersion where ContentDocumentId =: cdl.ContentDocumentId limit 1]; //ContentDocumentId =: docId].get(0);             
        if (version.PublishStatus.equals('P') && (version.FileExtension != null)) {
            return false;
        }
        return true;
    }
}


@istest
private class TestRestrictContactByName {
    @istest static void testname(){
        contact c = new contact(firstname='Satya',lastname='INVALIDNAME');
        test.startTest();
        database.SaveResult result = database.insert(c,false);
        test.stopTest();
        system.assertEquals('The Last Name "INVALIDNAME" is not allowed for DML', result.getErrors()[0].getMessage());
    }
}

I have a 0% cover on the trigger and the class, before I had up to 60%