Skip to main content
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;      

        }

        

    }

}
2 个回答
  1. 2021年4月24日 18:16

    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!

0/9000