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
Makam RuthvikMakam Ruthvik 

Cover test class for querying template and sending it through flow

Hi,
Can anyone tell me how to cover the get 100% coverage for the below class





global class SNL_002A_EmailAlert {

  @InvocableMethod
  public static void download(List<Order> OrderId) {
      
     set<id> ordId = new set<id>();
      for (Order s:OrderId){
          ordId.add(s.Id);
         } 
   
        List<Order> OrderList =[Select Id,BillToContactId from Order where Id IN : ordId];
      
         String templateId, whoId, whatId;

           Messaging.SingleEmailMessage email =  Messaging.renderStoredEmailTemplate( templateId, whoId, whatId);

    /**** Attach files to the message ****/
    
     List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>();
     List<id> ContentDocumentids = new List<id>();
      
      for(contentDocumentLink CDLink : [SELECT LinkedEntityid, ContentDocumentid FROM contentDocumentLink WHERE LinkedEntityid IN : ordId]){
               {
                   ContentDocumentids.add(CDLink.ContentDocumentid);  
                }
      
                 system.debug('ContentDocumentids'+ContentDocumentids);
                for ( ContentVersion cversion : [SELECT title, 
                                                        PathOnClient, 
                                                        versiondata 
                                                  FROM contentversion 
                                                  WHERE ContentDocumentId IN :ContentDocumentids  
                                                  ])
                 {
                  blob WOCFbody = cversion.versiondata;
                  system.debug('body : '+WOCFbody+'-----------'+cversion.title);
                  Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                  efa.setFileName(cversion.title);
                  efa.setBody(WOCFbody);
                  attachments.add(efa); 
                 }
            }

    email.setFileAttachments(attachments);

    /******* Set the message template ******/
      
    String tempName  = String.valueOf(System.Label.SNL_002A_Template);
    List<EmailTemplate> templates = [SELECT Id, Subject,Markup,body, HtmlValue FROM EmailTemplate WHERE DeveloperName =:tempName];
    if (!templates.isEmpty()) {
        
          templateId = templates[0].Id;
          whoId = OrderList[0].BillToContactId;
          whatId = OrderId[0].Id;
      
          email.setTargetObjectId(whoId);
          email.setSubject(email.getSubject());
          email.sethtmlBody(email.gethtmlBody());
          email.saveAsActivity = false;
    }
 
    /*******Send the message  *******/
    try {
      Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
    } catch (Exception e) {
      throw e;
    }
  
  }

}
SwethaSwetha (Salesforce Developers) 
HI Ruthvik,
The code provided in question does not highlight the uncovered lines in bold. Since this code is huge and requires an understanding of your implementation, it might not be possible to provide exact edit suggestions. However, the below articles give a good insight into how coverage can be improved

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines 

Examples of process builder/sendEmail  test classes:
https://salesforce.stackexchange.com/questions/96131/how-to-write-a-test-class-for-an-invocablemethod
https://salesforce.stackexchange.com/questions/227162/test-class-help-send-email-class/227165
https://developer.salesforce.com/forums/?id=9060G000000BibcQAC

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
mukesh guptamukesh gupta
Hi Makam,

Can you please share screen shot of running test class

 
Suraj Tripathi 47Suraj Tripathi 47

hi,

do some needful changes according to your code.

public static testMethod void createContactformCaseTest(){
        
        
        
        
        try{
            Contact con=new Contact();
            con.LastName='Test Data';
            con.Email='abc@gmail.com';
            insert con;
            
            
            
            
            
            
            // Insert Account
            
            Account a = new Account();
            a.Name = 'Test Account';
            insert a;
            
            // Insert Product
            Product2 p = new Product2();
            p.Name = ' Test Product ';
            p.Description='Test Product Entry 1';
            p.productCode = 'ABC';
            p.isActive = true;
            insert p;
            
            
            Id pricebookId = Test.getStandardPricebookId();
            
            // Insert PricebookEntry
            
            PricebookEntry standardPrice = new PricebookEntry();
            standardPrice.Pricebook2Id = pricebookId;
            standardPrice.Product2Id = p.Id;
            standardPrice.UnitPrice = 1;
            standardPrice.IsActive = true;
            standardPrice.UseStandardPrice = false;
            insert standardPrice ;
            
            // Insert Order
            
            Order o = new Order();
            o.Name = 'Test Order ';
            o.Status = 'Draft';
            o.EffectiveDate = system.today();
            o.EndDate = system.today() + 4;
            o.AccountId = a.id;
            o.Pricebook2Id =  pricebookId ;
            o.BillToContactId=con.id;
            
            insert o;
            system.debug('orderid::'+o);
            List<order> orderlist=new List<Order>();
            orderlist.add(o);
            
            
            
            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.LinkedEntityId=sub.id;
            content.origin = 'H';
            insert content;
            system.debug('content::'+content);
            ContentDocumentLink contentlink=new ContentDocumentLink();
            contentlink.LinkedEntityId=o.id;
            contentlink.contentdocumentid=[select contentdocumentid from contentversion where id =: content.id].contentdocumentid;
            contentlink.ShareType = 'I';
            contentlink.Visibility = 'AllUsers'; 
            
            
            insert contentlink;
            system.debug('contentlink::'+contentlink);
            
           
            
            
            test.startTest();
            className.download(orderlist);
            test.stopTest();
        }catch(exception e){
            system.debug('message::'+e.getMessage());
            system.debug('Line::'+e.getLineNumber());
        }
 

 

Please mark it as the Best Answer so that other people would take references from it .