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
Raju Chi 5Raju Chi 5 

testclass for approval process

Hi,
How to write test class for below class.

global class RecordAttachmentValidate {
    
   @AuraEnabled public static List<ContentDocumentLink> ContentDocumentList{get;set;}
    @AuraEnabled
    webservice static void ValidateAttachment(String RecordID){
       
        
       ContentDocumentList=[SELECT Id FROM ContentDocumentLink where LinkedEntityId =:RecordID];
        system.debug('ContentDocumentList'+ContentDocumentList);
        try{                   
            
            if(ContentDocumentList.size() !=0){               
                Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
                req1.setComments('Submitting request for approval automatically using Trigger');
                req1.setObjectId(RecordID);                   
                Approval.ProcessResult result = Approval.process(req1);
            } else{                
                throw new AuraHandledException('Please add attchments');

            }            
        }catch(Exception e){
            throw new AuraHandledException(e.getMessage());
            //return null;      
        }
        
    }
}
Best Answer chosen by Raju Chi 5
Suraj Tripathi 47Suraj Tripathi 47

Hi Raju,

Please find the solution of your test class with 91% coverage.

Test Class:

@isTest
public class RecordAttachmentValidateTest {  
    public static testmethod void ValidateAttachmentTest(){
        try{
            Account acct = new Account();
            acct.Name='test for LinkedEntityId';
            insert acct;
            
            ContentVersion contentVersion = new ContentVersion(
                Title = 'Test',
                PathOnClient = 'data.jpg',
                VersionData = Blob.valueOf('Test data'),
                IsMajorVersion = true
            );
            insert contentVersion;    
            List<ContentDocument> documents = [
                SELECT Id, Title, LatestPublishedVersionId 
                FROM ContentDocument
            ];
            
             ContentDocumentLink contentlink = New ContentDocumentLink();
            contentlink.LinkedEntityId = acct.id;
            contentlink.ContentDocumentId = documents[0].Id;
            contentlink.shareType = 'V';
            insert contentlink;
            test.startTest();
            RecordAttachmentValidate.ValidateAttachment(contentlink.LinkedEntityId);
            test.stopTest();
        }catch(exception e){
            system.debug('message:: '+e.getMessage());
            system.debug('Line:: '+e.getLineNumber());
            
        }
    }
}


Class : 

global class RecordAttachmentValidate {
    
   @AuraEnabled public static List<ContentDocumentLink> ContentDocumentList{get;set;}
    @AuraEnabled
    webservice static void ValidateAttachment(String RecordID){
       
        
       ContentDocumentList=[SELECT Id FROM ContentDocumentLink where LinkedEntityId =:RecordID];
        system.debug('ContentDocumentList'+ContentDocumentList);
        try{                   
            
            if(ContentDocumentList.size() !=0){               
                Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
                req1.setComments('Submitting request for approval automatically using Trigger');
                req1.setObjectId(RecordID);                   
                Approval.ProcessResult result = Approval.process(req1);
            } else{                
                throw new AuraHandledException('Please add attchments');

            }            
        }catch(Exception e){
            throw new AuraHandledException(e.getMessage());
            //return null;      
        }
        
    }
}
 

Please let me know it is working or not?

If it works then please mark it as the Best Answer so that other people would take reference from it.

Thank You!

All Answers

Suraj Tripathi 47Suraj Tripathi 47

Hi Raju,

Please find the solution of your test class with 91% coverage.

Test Class:

@isTest
public class RecordAttachmentValidateTest {  
    public static testmethod void ValidateAttachmentTest(){
        try{
            Account acct = new Account();
            acct.Name='test for LinkedEntityId';
            insert acct;
            
            ContentVersion contentVersion = new ContentVersion(
                Title = 'Test',
                PathOnClient = 'data.jpg',
                VersionData = Blob.valueOf('Test data'),
                IsMajorVersion = true
            );
            insert contentVersion;    
            List<ContentDocument> documents = [
                SELECT Id, Title, LatestPublishedVersionId 
                FROM ContentDocument
            ];
            
             ContentDocumentLink contentlink = New ContentDocumentLink();
            contentlink.LinkedEntityId = acct.id;
            contentlink.ContentDocumentId = documents[0].Id;
            contentlink.shareType = 'V';
            insert contentlink;
            test.startTest();
            RecordAttachmentValidate.ValidateAttachment(contentlink.LinkedEntityId);
            test.stopTest();
        }catch(exception e){
            system.debug('message:: '+e.getMessage());
            system.debug('Line:: '+e.getLineNumber());
            
        }
    }
}


Class : 

global class RecordAttachmentValidate {
    
   @AuraEnabled public static List<ContentDocumentLink> ContentDocumentList{get;set;}
    @AuraEnabled
    webservice static void ValidateAttachment(String RecordID){
       
        
       ContentDocumentList=[SELECT Id FROM ContentDocumentLink where LinkedEntityId =:RecordID];
        system.debug('ContentDocumentList'+ContentDocumentList);
        try{                   
            
            if(ContentDocumentList.size() !=0){               
                Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
                req1.setComments('Submitting request for approval automatically using Trigger');
                req1.setObjectId(RecordID);                   
                Approval.ProcessResult result = Approval.process(req1);
            } else{                
                throw new AuraHandledException('Please add attchments');

            }            
        }catch(Exception e){
            throw new AuraHandledException(e.getMessage());
            //return null;      
        }
        
    }
}
 

Please let me know it is working or not?

If it works then please mark it as the Best Answer so that other people would take reference from it.

Thank You!

This was selected as the best answer
Raju Chi 5Raju Chi 5
Hi Suraj,

Perfect.