You need to sign in to do that
Don't have an account?
Leonard 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;
}
}
}
}
}
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;
}
}
}
}
}
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
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