function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Intigration vIntigration v 

Test class for attached files in Salesforce

Hi,
can anyone Please help me for writing test class for below code.
global without sharing class checkDoconButton {
   webservice static Boolean checkDocVersion(id LeadId){
        list<ContentDocumentLink>Doclst= new list<ContentDocumentLink>();
        list<string>verifiedDoc= new list<string>();
        list<Lead> lstLead=new list<Lead>();
        list<string> lstdoctitle=new list<string>();
        Boolean isDocument;
        lstLead=[select Phone,City__c from Lead where id=:LeadId];
        string city='Ver.'+' 0'+lstLead[0].City__c;
        Doclst= [SELECT ContentDocumentId,contentdocument.title FROM ContentDocumentLink WHERE LinkedEntityId =:LeadId];
        
        for(ContentDocumentLink doclink:Doclst){
            if(doclink.contentdocument.title.contains(city)){
                verifiedDoc.add(doclink.contentdocument.title);
            }
            
        }
        if(verifiedDoc.size()>0){
        isDocument=true;
        }
        else{
            isDocument=false;
        }
        return isDocument;
    }

}
Best Answer chosen by Intigration v
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below test class it gives you around 93% coverage.
 
@istest
public class checkDoconButtonTest {
   @istest
static void  checkdocbutton() {
    Test.startTest();
    Lead l = new Lead();
    l.LastName='sample';
    l.Company='sample';
    l.Status='Qualified';
    l.City__c='hyderabad';
    insert l;
    ContentVersion cv = new ContentVersion();
cv.Title = 'Ver. 0hyderabad';
cv.PathOnClient = 'TesthyderabadDocument.pdf';
cv.VersionData = Blob.valueOf('Test Content');
cv.IsMajorVersion = true;
Insert cv;

//Get Content Documents
Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;

//Create ContentDocumentLink 
ContentDocumentLink cdl = New ContentDocumentLink();
cdl.LinkedEntityId = l.Id;
cdl.ContentDocumentId = conDocId;
cdl.shareType = 'V';
Insert cdl;
    
   Boolean check= checkDoconButton.checkDocVersion(l.id);
    system.assertEquals(True, check);
    Test.stopTest();
}
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
​​​​​​​

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below test class it gives you around 93% coverage.
 
@istest
public class checkDoconButtonTest {
   @istest
static void  checkdocbutton() {
    Test.startTest();
    Lead l = new Lead();
    l.LastName='sample';
    l.Company='sample';
    l.Status='Qualified';
    l.City__c='hyderabad';
    insert l;
    ContentVersion cv = new ContentVersion();
cv.Title = 'Ver. 0hyderabad';
cv.PathOnClient = 'TesthyderabadDocument.pdf';
cv.VersionData = Blob.valueOf('Test Content');
cv.IsMajorVersion = true;
Insert cv;

//Get Content Documents
Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;

//Create ContentDocumentLink 
ContentDocumentLink cdl = New ContentDocumentLink();
cdl.LinkedEntityId = l.Id;
cdl.ContentDocumentId = conDocId;
cdl.shareType = 'V';
Insert cdl;
    
   Boolean check= checkDoconButton.checkDocVersion(l.id);
    system.assertEquals(True, check);
    Test.stopTest();
}
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
​​​​​​​
This was selected as the best answer
mukesh guptamukesh gupta
Hi, 

Please use below code:-
 
@IsTest
Public class TestContentDocumentLink {
    
    static testmethod void testmethod1(){
        Test.startTest();
         Lead led = new Lead(LastName='Test', Company='Test',City__c = 'Delhi', Phone = '12333446', Status='Open - Not Contacted');
		 Insert led;

        ContentVersion content=new ContentVersion(); 
        content.Title='Header_Picture1'; 
        content.PathOnClient='/' + content.Title + '.jpg'; 
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body'); 
        content.VersionData=bodyBlob; 
        content.origin = 'H';
        insert content;
        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=led.id;
        contentlink.contentdocumentid=[select contentdocumentid from contentversion where id =: content.id].contentdocumentid;
        contentlink.ShareType = 'I';
        contentlink.Visibility = 'AllUsers'; 
        
        
        insert contentlink;
        Test.stopTest();
        
    }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
mukesh guptamukesh gupta
Hi 

Please use updated code:-
 
@IsTest
Public class TestContentDocumentLink {
    
    static testmethod void testmethod1(){
        Test.startTest();
         Lead led = new Lead(LastName='Test', Company='Test',City__c = 'Delhi', Phone = '12333446', Status='Open - Not Contacted');
		 Insert led;

        ContentVersion content=new ContentVersion(); 
        content.Title='Header_Picture1'; 
        content.PathOnClient='/' + content.Title + '.jpg'; 
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body'); 
        content.VersionData=bodyBlob; 
        content.origin = 'H';
        insert content;
        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=led.id;
        contentlink.contentdocumentid=[select contentdocumentid from contentversion where id =: content.id].contentdocumentid;
        contentlink.ShareType = 'I';
        contentlink.Visibility = 'AllUsers'; 
        
        
        insert contentlink;
		
		checkDoconButton.checkDocVersion(led.Id);
        Test.stopTest();
        
    }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Intigration vIntigration v
Hi Praveen,
Am getting the below error when i run the test class

"Methods defined as TestMethod do not support Web service callouts".
Thank you!
Intigration vIntigration v
Hi Mukesh,
Getting the same error when i try to run your test class.
"Methods defined as TestMethod do not support Web service callouts".
Thank you!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you confirm the code you shared is correct because I dont see any callout in the method which you shared.

Thanks,
 
Intigration vIntigration v
Hi Praveen,
am calling this class in javascript button.Please see the javascript code.am getting error if i define the method public instead of webserice to use in javascript button.
{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
var isDocument=sforce.apex.execute("checkDoconButton", "checkDocVersion", {LeadId:"{!Lead.Id}"});
if(isDocument=="true"){
alert('document already exists');
}
Intigration vIntigration v
Hi Praveen,
can you help me to resolve this error "Methods defined as TestMethod do not support Web service callouts".
Thank you!
Intigration vIntigration v
Hi Praveen,
Its working fine now.
Thank you!