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
Akshay MhetreAkshay Mhetre 

test class coverage.....plz help

@AuraEnabled
     public static void updateFilesMainCase(String contentVersionIds,String recordId)
     {
     System.debug('============contentVersionIds===='+contentVersionIds);
         if(contentVersionIds != null){
             String[] contentVersionIdArr = contentVersionIds.split(',');
        Set<String> contentDocumentIds = new Set<String>();
        for(String docId : contentVersionIdArr)
        {
            if(docId != null && docId.length() > 0)
                contentDocumentIds.add(docId);
        }
        System.debug('============contentDocumentIds===='+contentDocumentIds);
      //  List<Case> listCase = [select Type,Subject from Case where Id=:recordId];
       String imgTitle;
        List<User> agencyName= [Select Id ,Contact.Account.Name,Profile.Name from User where Id =:UserInfo.getUserId() AND Profile.Name='RCU Agent Community'];
        if(agencyName.size()> 0){
            imgTitle = agencyName[0].Contact.Account.Name+'-'+String.valueOf(System.now());
            }
        System.debug('============imgTitle===='+imgTitle);
        List<ContentVersion> versionList = [SELECT FileExtension, Title FROM ContentVersion 
                                            WHERE ContentDocumentId IN :contentDocumentIds and IsLatest=true ] ;
        List<ContentVersion> contentVersionsToUpdate = new List<ContentVersion>();
        for(ContentVersion contVersion : versionList)
        {
            if(imgTitle != null){
              contVersion.Title = imgTitle;  
            }
        }
         update versionList;   
         System.debug('recordId: '+recordId);
        List<Case> caselist= [Select Id, isAgentReportUploaded__c From Case Where ParentId=:RecordId]; 
         for(Case subC: caseList){
             subC.isAgentReportUploaded__c = true;
         }
         if(!caselist.isEmpty()){
             update caselist;
         }
        //return imgTitle;
       } 
        // return 'error';
         }
}
Best Answer chosen by Akshay Mhetre
ravi soniravi soni
hy Akshay,
find your test class with 100% coverage.
@isTest
public class updateFilesMainCaseTest {
@isTest
    public static void test_Data(){
         //Create Account
        Account acct = new Account(Name='TEST_ACCT');
        insert acct;
        //Creat Contact 
        Contact con = new Contact(firstName='john', 
                    lastName='smith', 
                    Email='user@acme.com', 
                    HasOptedOutOfEmail=false,
                    AccountId = acct.Id);
       insert con;
        
        //Create contentVersion
        ContentVersion cv = new ContentVersion();
        cv.Title = 'Test Document';
        cv.PathOnClient = 'TestDocument.pdf';
        cv.VersionData = Blob.valueOf('Test Content');
        cv.IsMajorVersion = true;
        Insert cv;
        ContentVersion cv1 = new ContentVersion();
        cv1.Title = 'Test Document 1';
        cv1.PathOnClient = 'TestDocument.pdf';
        cv1.VersionData = Blob.valueOf('Test Content');
        cv1.IsMajorVersion = true;
        Insert cv1;
        
        string contentVersionIds = '';
        //Get Content Version
        List<ContentVersion> cvList = [SELECT Id, Title, ContentDocumentId FROM ContentVersion];
        System.assertEquals(cvList.size(), 2);
        for(ContentVersion oCV : cvList){
            contentVersionIds += oCV.ContentDocumentId + ',';
        }
        contentVersionIds.removeEnd(',');
            system.debug('contentVersionIds : ' + contentVersionIds);
        
        
         Profile pf= [Select Id from profile where Name='RCU Agent Community' limit 1]; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
		list<user> lstUser = new list<User>();
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id,
                         isActive = True,
                         ContactId = con.Id
                        ); 
						lstUser.add(uu);
         Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email'); 
        insert cas;
         Case cas1 = new Case(Status ='New', Priority = 'Medium', Origin = 'Email',ParentId = cas.Id); 
        insert cas1;
        
        updateFilesMainCase.updateFilesMainCaseMethod(contentVersionIds,cas.Id);
        
    }
}

don't forget to mark it as best answer if it's help you.
Thank you

All Answers

PriyaPriya (Salesforce Developers) 

Hi Akshay,

Have you tried the test class for it ? If yes, please provide it, we can work on that to increase the coverage.

Regards,

Priya Ranjan

ravi soniravi soni
hy Akshay,
find your test class with 100% coverage.
@isTest
public class updateFilesMainCaseTest {
@isTest
    public static void test_Data(){
         //Create Account
        Account acct = new Account(Name='TEST_ACCT');
        insert acct;
        //Creat Contact 
        Contact con = new Contact(firstName='john', 
                    lastName='smith', 
                    Email='user@acme.com', 
                    HasOptedOutOfEmail=false,
                    AccountId = acct.Id);
       insert con;
        
        //Create contentVersion
        ContentVersion cv = new ContentVersion();
        cv.Title = 'Test Document';
        cv.PathOnClient = 'TestDocument.pdf';
        cv.VersionData = Blob.valueOf('Test Content');
        cv.IsMajorVersion = true;
        Insert cv;
        ContentVersion cv1 = new ContentVersion();
        cv1.Title = 'Test Document 1';
        cv1.PathOnClient = 'TestDocument.pdf';
        cv1.VersionData = Blob.valueOf('Test Content');
        cv1.IsMajorVersion = true;
        Insert cv1;
        
        string contentVersionIds = '';
        //Get Content Version
        List<ContentVersion> cvList = [SELECT Id, Title, ContentDocumentId FROM ContentVersion];
        System.assertEquals(cvList.size(), 2);
        for(ContentVersion oCV : cvList){
            contentVersionIds += oCV.ContentDocumentId + ',';
        }
        contentVersionIds.removeEnd(',');
            system.debug('contentVersionIds : ' + contentVersionIds);
        
        
         Profile pf= [Select Id from profile where Name='RCU Agent Community' limit 1]; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
		list<user> lstUser = new list<User>();
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id,
                         isActive = True,
                         ContactId = con.Id
                        ); 
						lstUser.add(uu);
         Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email'); 
        insert cas;
         Case cas1 = new Case(Status ='New', Priority = 'Medium', Origin = 'Email',ParentId = cas.Id); 
        insert cas1;
        
        updateFilesMainCase.updateFilesMainCaseMethod(contentVersionIds,cas.Id);
        
    }
}

don't forget to mark it as best answer if it's help you.
Thank you
This was selected as the best answer