You need to sign in to do that
Don't have an account?
Evan Shuey
Test class for contentDocument
Hello all. I am making a class that ensures that there is an attachment on an Opp whenever its closed won. Below is my class that actually does the logic. Below that is the test class. The logic works fine but I can only get 42% coverage though. The underlined code is what is not being covered, this is in a Lightning instance fyi. Thanks for the help!
public class OpportunityHandler {
public static void checkOppForAttachment (List<Opportunity> oppList){
for(Opportunity opp : oppList){
if(opp.stageName == 'Closed Won' || opp.stageName == 'Closed Won (R)'){
ContentDocumentLink cdl = null;
List<ContentDocumentLink> cdl2 = [Select Id from ContentDocumentLink where LinkedEntityId =:opp.Id];
if (cdl2.isEmpty()){
opp.addError('Please ensure the contract is attached prior to closing this Opportunity');
}
}
}
}
}
Test Class
@isTest
public class OpportunityHandlerTest {
@isTest public static void validateAttachment() {
//Create Account and Opportunity
List<RecordType> acctRecType = [Select Id from RecordType where developerName = 'Company' and sObjectType = 'Account'];
List<RecordType> oppRecType = [Select Id from RecordType where developerName = 'AM_Channel' and sObjectType = 'Opportunity'];
Test.startTest();
try{
Account acct = new Account();
acct.name = 'Test Account1';
acct.RecordTypeId = acctRecType.get(0).id;
insert acct;
Opportunity opp = new Opportunity();
opp.Name = 'test opp';
opp.CloseDate = system.today()+1;
opp.StageName = 'Initial Pitch/Demo';
opp.Use_Case__c = 'Brand Tracker';
opp.AccountId = acct.Id;
opp.RecordTypeId = oppRecType.get(0).id;
insert opp;
Opportunity opp2 = new Opportunity();
opp2.Name = 'test opp2';
opp2.CloseDate = system.today()+1;
opp2.StageName = 'Initial Pitch/Demo';
opp2.Use_Case__c = 'Brand Tracker';
opp2.AccountId = acct.Id;
opp2.RecordTypeId = oppRecType.get(0).id;
insert opp2;
ContentVersion contentVersion = new ContentVersion(
Title = 'a picture',
PathOnClient = 'Pic.jpg',
VersionData = Blob.valueOf('Test Content'),
IsMajorVersion = true);
insert contentVersion;
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
//create ContentDocumentLink record
ContentDocumentLink cdl = New ContentDocumentLink();
cdl.LinkedEntityId = opp.id;
cdl.ContentDocumentId = documents[0].Id;
cdl.shareType = 'V';
cdl.visibility = 'AllUsers';
insert cdl;
List<Opportunity> updateList = new List<Opportunity>();
List<ContentDocumentLink> cdl2 = [Select Id from ContentDocumentLink where LinkedEntityId =:opp.Id];
List<Opportunity> oldOpp = [Select id from Opportunity where Name = 'test opp'];
for (Opportunity runUpdate : oldOpp){
runUpdate.stageName = 'Closed Won (R)';
updateList.add(runUpdate);
system.debug(runUpdate.stageName);
update updateList;
System.assertEquals(documents.size(), 1);
}
}
catch(Exception e){
System.Assert(e.getMessage().contains('Please ensure the contract is attached prior to closing this Opportunity'));
}
test.stopTest();
}
}
public class OpportunityHandler {
public static void checkOppForAttachment (List<Opportunity> oppList){
for(Opportunity opp : oppList){
if(opp.stageName == 'Closed Won' || opp.stageName == 'Closed Won (R)'){
ContentDocumentLink cdl = null;
List<ContentDocumentLink> cdl2 = [Select Id from ContentDocumentLink where LinkedEntityId =:opp.Id];
if (cdl2.isEmpty()){
opp.addError('Please ensure the contract is attached prior to closing this Opportunity');
}
}
}
}
}
Test Class
@isTest
public class OpportunityHandlerTest {
@isTest public static void validateAttachment() {
//Create Account and Opportunity
List<RecordType> acctRecType = [Select Id from RecordType where developerName = 'Company' and sObjectType = 'Account'];
List<RecordType> oppRecType = [Select Id from RecordType where developerName = 'AM_Channel' and sObjectType = 'Opportunity'];
Test.startTest();
try{
Account acct = new Account();
acct.name = 'Test Account1';
acct.RecordTypeId = acctRecType.get(0).id;
insert acct;
Opportunity opp = new Opportunity();
opp.Name = 'test opp';
opp.CloseDate = system.today()+1;
opp.StageName = 'Initial Pitch/Demo';
opp.Use_Case__c = 'Brand Tracker';
opp.AccountId = acct.Id;
opp.RecordTypeId = oppRecType.get(0).id;
insert opp;
Opportunity opp2 = new Opportunity();
opp2.Name = 'test opp2';
opp2.CloseDate = system.today()+1;
opp2.StageName = 'Initial Pitch/Demo';
opp2.Use_Case__c = 'Brand Tracker';
opp2.AccountId = acct.Id;
opp2.RecordTypeId = oppRecType.get(0).id;
insert opp2;
ContentVersion contentVersion = new ContentVersion(
Title = 'a picture',
PathOnClient = 'Pic.jpg',
VersionData = Blob.valueOf('Test Content'),
IsMajorVersion = true);
insert contentVersion;
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
//create ContentDocumentLink record
ContentDocumentLink cdl = New ContentDocumentLink();
cdl.LinkedEntityId = opp.id;
cdl.ContentDocumentId = documents[0].Id;
cdl.shareType = 'V';
cdl.visibility = 'AllUsers';
insert cdl;
List<Opportunity> updateList = new List<Opportunity>();
List<ContentDocumentLink> cdl2 = [Select Id from ContentDocumentLink where LinkedEntityId =:opp.Id];
List<Opportunity> oldOpp = [Select id from Opportunity where Name = 'test opp'];
for (Opportunity runUpdate : oldOpp){
runUpdate.stageName = 'Closed Won (R)';
updateList.add(runUpdate);
system.debug(runUpdate.stageName);
update updateList;
System.assertEquals(documents.size(), 1);
}
}
catch(Exception e){
System.Assert(e.getMessage().contains('Please ensure the contract is attached prior to closing this Opportunity'));
}
test.stopTest();
}
}
Adjust that class to something as follows: note: code not compiled As for the test class:
give that a whirl
Regards
Andrew