You need to sign in to do that
Don't have an account?
APEX Class Help - Create Test Class for Custom APEX Trigger on ContentDocumentLink
Hi there,
I am working on deploying a custom APEX trigger on the ContentDocumentLink object that checks a custom checkbox field called Has_Attachment__c on a custom object called "Advice" (Advice__c) when a file has been uploaded within a record.
Here is my current Trigger code:
Could anyone provide some assistance with creating an test class for my trigger above that will allow me to deploy it into Production?
Thanks in advance for any and all help. I really appreciate it.
Ricki
I am working on deploying a custom APEX trigger on the ContentDocumentLink object that checks a custom checkbox field called Has_Attachment__c on a custom object called "Advice" (Advice__c) when a file has been uploaded within a record.
Here is my current Trigger code:
trigger ContentDocumentLinkTrigger on ContentDocumentLink ( after insert, after update, after delete ) { List<ContentDocumentLink> cdls = ( Trigger.new == null ? Trigger.old : Trigger.new ); Set<ID> parentIds = New Set<ID>(); for ( ContentDocumentLink cdl : cdls ) { parentIds.add( cdl.LinkedEntityId ); } for ( List<Advice__c> adviceToUpdate: [ SELECT Id, ( SELECT Id FROM ContentDocumentLinks LIMIT 1 ) FROM Advice__c WHERE Id IN :parentIds ] ) { for ( Advice__c q : adviceToUpdate) { q.HasAttachment__c = true ; } update adviceToUpdate; } }Evidently, now that I am trying to move it into my Production org., I am required to have at least 75% code coverage. I am very new to Apex and I am having difficulty finding the right resources to help me create a test class.
Could anyone provide some assistance with creating an test class for my trigger above that will allow me to deploy it into Production?
Thanks in advance for any and all help. I really appreciate it.
Ricki
If you want to try the test then it should br like below:
@isTest
private class CompositeKeyTest {
static testMethod void testPurchase() {
//Create Document Parent Record
Advice__c adv = new Advice__c();
adv.Name='Test Advice';
// Add all the remaining required fields to insert the Advice record
Insert adv;
//Create Document
ContentVersion cv = new ContentVersion();
cv.Title = 'Test Document';
cv.PathOnClient = 'TestDocument.pdf';
cv.VersionData = Blob.valueOf('Test Content');
cv.IsMajorVersion = true;
Insert cv;
//Get Content Documents
List<ContentDocument> docList = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
//Create ContentDocumentLink
ContentDocumentLink cdl = New ContentDocumentLink();
cdl.LinkedEntityId = adv.Id;
cdl.ContentDocumentId = docList[0].Id;
cdl.shareType = 'V';
Insert cdl;
}
}
Thanks,
Maharajan.C
All Answers
I am unaware of the practise followed by your organization otherwise trigger needs only 1% coverage. Next thing is to get resourse for test class.Please find the below link for the same and I think it will help you for sure.
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_triggers
Please let me know if you need any other help.
Thanks
If you want to try the test then it should br like below:
@isTest
private class CompositeKeyTest {
static testMethod void testPurchase() {
//Create Document Parent Record
Advice__c adv = new Advice__c();
adv.Name='Test Advice';
// Add all the remaining required fields to insert the Advice record
Insert adv;
//Create Document
ContentVersion cv = new ContentVersion();
cv.Title = 'Test Document';
cv.PathOnClient = 'TestDocument.pdf';
cv.VersionData = Blob.valueOf('Test Content');
cv.IsMajorVersion = true;
Insert cv;
//Get Content Documents
List<ContentDocument> docList = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
//Create ContentDocumentLink
ContentDocumentLink cdl = New ContentDocumentLink();
cdl.LinkedEntityId = adv.Id;
cdl.ContentDocumentId = docList[0].Id;
cdl.shareType = 'V';
Insert cdl;
}
}
Thanks,
Maharajan.C