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
Subodh ShuklaSubodh Shukla 

How to cover this lines of code in test class:

public void updateAttachmentWithLDCNumber(Map<String, String> mapAccount_LDCValue){
        List<Attachment> lstAttachmentToUpdate = new List<Attachment>();
        List<Attachment> lstAttachment = [SELECT Id, 
                                                 Name, 
                                                 Body,
                                                 ParentId
                                            FROM Attachment 
                                           WHERE ParentId IN: mapAccount_LDCValue.keySet()];
        if(lstAttachment.isEmpty()) return;
        for(Attachment objAttachment : lstAttachment){
            Blob csvBody = objAttachment.body;
            if(csvBody == null) continue;
            String strCSVBody = csvBody.toString();
            List<String> lstRows = strCSVBody.split('\n');
            Integer columnIndex = null;
            String finalCSV = lstRows[0];
            List<String> lstHeader = finalCSV.split(',');
            for(Integer i = 0; i < lstHeader.size(); i++){
                if(lstHeader[i].contains('LDCNumber')){
                    columnIndex = i;
                    break;
                }
            }
            if(columnIndex == null) continue;
            for(Integer i = 1; i < lstRows.size(); i++){
                List<String> lstCoumn =  lstRows[i].split(',');
                lstCoumn.set(columnIndex, mapAccount_LDCValue.get(objAttachment.ParentId));
                finalCSV += '\n' + String.join(lstCoumn, ',');
            }
            objAttachment.body = Blob.valueOf(finalCSV);
            lstAttachmentToUpdate.add(objAttachment);
        }
        update lstAttachmentToUpdate;
    }

 
karthikeyan perumalkarthikeyan perumal
Hello

use below Ttest class Code Coverage 90%

Note : AttachmentWithLDCNumber this is your class name
@isTest
public class Attachmentupdate_TEST{
 
      
     public Static testMethod void Attachmentupdate(){
     
       
       
         Account acc=new Account(Name='Acme Inc');
        insert acc;
        
        Attachment attach=new Attachment();      
        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body,LDCNumber');
        attach.body=bodyBlob;
        attach.parentId=acc.id;
        insert attach;
        
        Attachment attach1=new Attachment();     
        attach1.Name='Unit Test Attachment';
        Blob bodyBlob1=Blob.valueOf('Unit Test Attachment Body,LDCNumber');
        attach1.body=bodyBlob1;
        attach1.parentId=acc.id;
        insert attach1;
        
        Map<string,string> TestMap= New Map<string,string>();
        TestMap.put(string.Valueof(acc.id),string.Valueof(acc.id));
        Test.StartTest(); 
        AttachmentWithLDCNumber TstCon= new AttachmentWithLDCNumber();
        TstCon.updateAttachmentWithLDCNumber(TestMap);        
        
        
       Test.StopTest();
      }
     
     
      
}

Hope this will help you, 

Mark Best ANSWER if its works for you, 

Thanks
karthik
 
karthikeyan perumalkarthikeyan perumal
Hello, 

Use this Test class.. 100% code Coverage, 

Note : AttachmentWithLDCNumber-- Class name replace your class here

 AttachmentWithLDCNumber TstCon= new AttachmentWithLDCNumber();
@isTest
public class Attachmentupdate_TEST{
 
      
     public Static testMethod void Attachmentupdate(){
     
         Account acc=new Account(Name='Acme Inc');
        insert acc;
        
        Attachment attach=new Attachment();      
        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body,LDCNumber \n,Test test test test ');
        attach.body=bodyBlob;
        attach.parentId=acc.id;
        insert attach;
        
        Attachment attach1=new Attachment();     
        attach1.Name='Unit Test Attachment';
        Blob bodyBlob1=Blob.valueOf('Unit Test Attachment Body,LDCNumber \n,Test test test test  ');
        attach1.body=bodyBlob1;
        attach1.parentId=acc.id;
        insert attach1;
        
        Map<string,string> TestMap= New Map<string,string>();
        TestMap.put(string.Valueof(acc.id),string.Valueof(acc.id));
        Test.StartTest(); 
        AttachmentWithLDCNumber TstCon= new AttachmentWithLDCNumber();
        TstCon.updateAttachmentWithLDCNumber(TestMap);       
                
       Test.StopTest();
      }
     
      
      
      
}

Mark Best ANSWER if its works for you, 

Thanks
karthik