+ Start a Discussion
Sebastian JudSebastian Jud 

I need to improve my code coverage

Hi everyone! I need to improve my code coverage, the current code coverage 62%. In my understanding the problem reside in Adobe Docusign. If I am right, is there any way to skip that?

this is the code:


 

public class UploadAttachmentControllerAccounts {
    
    public String selectedType {get;set;}
    public String description {get;set;}
    private Account account {get;set;} 
    public String fileName {get;set;}
    public Blob fileBody {get;set;}
    
    public UploadAttachmentControllerAccounts(ApexPages.StandardController controller) { 
        this.Account = (account)controller.getRecord();
    }   
    
    // creates a new Account_Attachment__c record
    private Database.SaveResult saveCustomAttachment() {
        Account_Document_Attachment__c obj = new Account_Document_Attachment__c();
        obj.Account__c = Account.Id; 
        obj.description__c = description;
        obj.type__c = selectedType;
        // fill out cust obj fields
        return Database.insert(obj);
    }
    
    // create an actual Attachment record with the Account_Attachment__c as parent
    private Database.SaveResult saveStandardAttachment(Id parentId) {
        Database.SaveResult result;
        
        Attachment attachment = new Attachment();
        attachment.body = this.fileBody;
        attachment.name = this.fileName;
        attachment.parentId = parentId;
        // inser the attahcment
        result = Database.insert(attachment);
        // reset the file for the view state
        fileBody = Blob.valueOf(' ');
        return result;
    }
    
    /**
    * Upload process is:
    *  1. Insert new Account_Attachment__c record
    *  2. Insert new Attachment with the new Account_Attachment__c record as parent
    *  3. Update the Account_Attachment__c record with the ID of the new Attachment
    **/
    public PageReference processUpload() {
        try {
            Database.SaveResult customAttachmentResult = saveCustomAttachment();
        
            if (customAttachmentResult == null || !customAttachmentResult.isSuccess()) {
                ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                  'Could not save attachment.'));
                return null;
            }
        
            Database.SaveResult attachmentResult = saveStandardAttachment(customAttachmentResult.getId());
        
            if (attachmentResult == null || !attachmentResult.isSuccess()) {
                ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                  'Could not save attachment.'));            
                return null;
            } else {
                // update the custom attachment record with some attachment info
                Account_Document_Attachment__c customAttachment = [select id from Account_Document_Attachment__c where id = :customAttachmentResult.getId()];
                customAttachment.name = this.fileName;
                customAttachment.Attachment__c = attachmentResult.getId();
                update customAttachment;
            }
        
        } catch (Exception e) {
            ApexPages.AddMessages(e);
            return null;
        }
        
        return new PageReference('/'+Account.Id);
    }
    
    public PageReference back() {
        return new PageReference('/'+Account.Id);
    }     

}
Test:

 

User-added image

Thanks in advance!

Best Answer chosen by Sebastian Jud
Raj VakatiRaj Vakati
ApexPages.StandardController sc = new ApexPages.StandardController(acc); 
UploadAttachmentControllerAccounts testCon = new UploadAttachmentControllerAccounts(sc);
 PageReference pageRef = Page.demo;
 pageRef.getParameters().put('Id', String.valueOf(acc.Id));

// ADD this two lines
testCon.fileName ='test.pdf';
fileName.fileBody =Blob.valueOf('asd');

 

All Answers

Raj VakatiRaj Vakati
public class UploadAttachmentControllerAccounts {
    
    public String selectedType {get;set;}
    public String description {get;set;}
    private Account account {get;set;} 
    public String fileName {get;set;}
    public Blob fileBody {get;set;}
    
    public UploadAttachmentControllerAccounts(ApexPages.StandardController controller) { 
        this.Account = (account)controller.getRecord();
    }   
    
    // creates a new Account_Attachment__c record
   @TestVisible   private Database.SaveResult saveCustomAttachment() {
        Account_Document_Attachment__c obj = new Account_Document_Attachment__c();
        obj.Account__c = Account.Id; 
       // obj.description__c = description;
        obj.type__c = selectedType;
        // fill out cust obj fields
        return Database.insert(obj);
    }
    
    // create an actual Attachment record with the Account_Attachment__c as parent
    @TestVisible  private Database.SaveResult saveStandardAttachment(Id parentId) {
        Database.SaveResult result;
        
        Attachment attachment = new Attachment();
        attachment.body = this.fileBody;
        attachment.name = this.fileName;
        attachment.parentId = parentId;
        // inser the attahcment
        result = Database.insert(attachment);
        // reset the file for the view state
        fileBody = Blob.valueOf(' ');
        return result;
    }
    
    /**
* Upload process is:
*  1. Insert new Account_Attachment__c record
*  2. Insert new Attachment with the new Account_Attachment__c record as parent
*  3. Update the Account_Attachment__c record with the ID of the new Attachment
**/
    public PageReference processUpload() {
        try {
            Database.SaveResult customAttachmentResult = saveCustomAttachment();
            
            if (customAttachmentResult == null || !customAttachmentResult.isSuccess()) {
                ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                                                           'Could not save attachment.'));
                return null;
            }
            
            Database.SaveResult attachmentResult = saveStandardAttachment(customAttachmentResult.getId());
            
            if (attachmentResult == null || !attachmentResult.isSuccess()) {
                ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                                                           'Could not save attachment.'));            
                return null;
            } else {
                // update the custom attachment record with some attachment info
                Account_Document_Attachment__c customAttachment = [select id from Account_Document_Attachment__c where id = :customAttachmentResult.getId()];
                customAttachment.name = this.fileName;
                //customAttachment.Attachment__c = attachmentResult.getId();
                update customAttachment;
            }
            
        } catch (Exception e) {
            ApexPages.AddMessages(e);
            return null;
        }
        
        return new PageReference('/'+Account.Id);
    }
    
    public PageReference back() {
        return new PageReference('/'+Account.Id);
    }     
    
}
 
@isTest
public class UploadAttachmentControllerAccounts_Test {
    public static testmethod void testCase1(){
        Account acc = new Account();
        acc.Name ='Test' ; 
        insert acc ;
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
        UploadAttachmentControllerAccounts testCon = new UploadAttachmentControllerAccounts(sc);
        
        PageReference pageRef = Page.demo;
        pageRef.getParameters().put('Id', String.valueOf(acc.Id));
        testCon.processUpload();
        testCon.back();
       
        testCon.saveCustomAttachment();
        testCon.saveStandardAttachment(acc.Id);
        
        Test.setCurrentPage(pageRef);  
        
    }
}

 
Sebastian JudSebastian Jud

Thank you Raj! I am having this error now when I am tryng to deploy the change set

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name, Body]: [Name, Body] 
Stack Trace: Class.UploadAttachmentControllerAccounts.saveStandardAttachment: line 32, column 1 Class.UploadAttachmentControllerAccounts_Test.testCase1: line 16, column 1

 

Any ideas?

Thanks again!

Raj VakatiRaj Vakati
ApexPages.StandardController sc = new ApexPages.StandardController(acc); 
UploadAttachmentControllerAccounts testCon = new UploadAttachmentControllerAccounts(sc);
 PageReference pageRef = Page.demo;
 pageRef.getParameters().put('Id', String.valueOf(acc.Id));

// ADD this two lines
testCon.fileName ='test.pdf';
fileName.fileBody =Blob.valueOf('asd');

 
This was selected as the best answer
Raj VakatiRaj Vakati
// ADD this two lines
testCon.fileName ='test.pdf';
testCon.fileBody =Blob.valueOf('asd');

 
Sebastian JudSebastian Jud
Thank you so much! You've saved me a lot of time!