+ Start a Discussion
Leonard Silon 16Leonard Silon 16 

Help with a test class please

Hi - I am hoping there is someone with a little time on their hands that can and is willing to help me. We only have one developer that sort of kind of knows SFDC. However, 110% of his time is tied up in a huge enterprise project. We have the below trigger that I need a test class for. 

Thank you in advance if you can help. I understand if no one can

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
trigger CheckboxIfFile on ContentDocumentLink (after insert) {
    
  Schema.DescribeSObjectResult  r = Case.sObjectType.getDescribe();
    String keyPrefix = r.getKeyPrefix();
    
    if(trigger.isInsert){
        for(ContentDocumentLink att : trigger.New){
            
            if(keyPrefix == String.valueOf(att.LinkedEntityId).left(3)){
                
                ContentDocument doc = [SELECT Id, Title from ContentDocument WHERE Id = :att.ContentDocumentId];
                
                if (doc.Title.containsIgnoreCase('screen')) {
                    
                    Case con =  [SELECT Id, Screen_Shots_Attached__c from Case WHERE Id = :att.LinkedEntityId LIMIT 1];
                    con.Screen_Shots_Attached__c  = True;
                    update con;
                }
            }
        }
    }
}
Best Answer chosen by Leonard Silon 16
Ajay K DubediAjay K Dubedi
Hi Leonard,

Try the following code it may helpful for you:
@isTest
public class CheckboxIfFile_Test {
    @isTest static void CheckboxIfFile_test_Method()
    {
        Case con = new Case();
        con.Screen_Shots_Attached__c=false;
        con.Status='New';
        con.Origin='Phone';
        insert con;
    
        ContentVersion contentVersionInsert = new ContentVersion(
            Title = 'Test',
            PathOnClient = 'Test.jpg',
            VersionData = Blob.valueOf('Test Content Data'),
            IsMajorVersion = true
        );
        insert contentVersionInsert;
 
        
        ContentVersion contentVersionSelect = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :contentVersionInsert.Id LIMIT 1];
        List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
        
        ContentDocumentLink contentlink=new ContentDocumentLink();
             contentlink.LinkedEntityId=con.id; 
             contentlink.ShareType= 'C';   
             contentlink.ContentDocumentId=documents[0].Id;
             contentlink.Visibility = 'AllUsers';
        insert contentlink;
    }
}
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
 

All Answers

Ajay K DubediAjay K Dubedi
Hi Leonard,

Try the following code it may helpful for you:
@isTest
public class CheckboxIfFile_Test {
    @isTest static void CheckboxIfFile_test_Method()
    {
        Case con = new Case();
        con.Screen_Shots_Attached__c=false;
        con.Status='New';
        con.Origin='Phone';
        insert con;
    
        ContentVersion contentVersionInsert = new ContentVersion(
            Title = 'Test',
            PathOnClient = 'Test.jpg',
            VersionData = Blob.valueOf('Test Content Data'),
            IsMajorVersion = true
        );
        insert contentVersionInsert;
 
        
        ContentVersion contentVersionSelect = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :contentVersionInsert.Id LIMIT 1];
        List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
        
        ContentDocumentLink contentlink=new ContentDocumentLink();
             contentlink.LinkedEntityId=con.id; 
             contentlink.ShareType= 'C';   
             contentlink.ContentDocumentId=documents[0].Id;
             contentlink.Visibility = 'AllUsers';
        insert contentlink;
    }
}
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
 
This was selected as the best answer
Leonard Silon 16Leonard Silon 16
I received the below error when validating. I am not sure what it does not have sufficient access to? Class Name Method Name Error Message CheckboxIfFile_Test CheckboxIfFile_test_Method System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_OR_READONLY, Invalid sharing type C: [ShareType] Stack Trace: Class.CheckboxIfFile_Test.CheckboxIfFile_test_Method: line 28, column 1
Leonard Silon 16Leonard Silon 16
I changed contentlink.ShareType= 'C'; to contentlink.ShareType= 'V'; and it worked