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
kermit.thefrogkermit.thefrog 

Attach pdf to Opportunity -> testCoverage ApexCode

I have a flow that helps users to create an opportunity with products. When the flow finishes I want salesforce to generate a quote (consisting of data from Account, Opportunity and OpportunityProducts). This quote is generated as a pdf and I want that this quote is attached as a pdf-file to the opportunity.

I found some useful hints to set this up, but it seems that my testclass is not covering at least 75% (just 5%). Can somebody please look into this a point me into the right direction to get this running?

ApexClass:
 
public class attachPDFToOpportunity {
    
    private final Opportunity a; 
    
    
    public attachPDFToOpportunity(ApexPages.StandardController standardPageController) {
        a = (Opportunity)standardPageController.getRecord(); 
    }
    
    
    public PageReference attachPDF() {
        
        
        PageReference pdfPage = Page.wedoQuote; 
        Blob pdfBlob; 
        if (!Test.isRunningTest()) { 
            pdfBlob = pdfPage.getContent(); 
        } else { 
            pdfBlob = Blob.valueOf('Some Text');
        }
        Attachment attach = new Attachment(parentId = a.Id, Name = 'pdfAttachmentDemo.pdf', body = pdfBlob); 
        insert attach; 
        
        
        PageReference pageWhereWeWantToGo = new ApexPages.StandardController(a).view(); 
        pageWhereWeWantToGo.setRedirect(true); 
        return pageWhereWeWantToGo; 
    }

}
testClass:
@isTest
private class attachPDFToOpportunityTest {
    
    //tests attachPDFToOpportunity
    @isTest //defines method for use during testing only
    static void attachPDFLogic() {
        //BEGIN: Some Setup Items...
        List<Opportunity> opportunities = new List<Opportunity>();
        opportunities.add(new Opportunity(Name = 'TestOppApex', StageName = 'Qualification', CloseDate = date.today()));
        
        insert opportunities;
        //END: Some Setup Items...
        
        Test.startTest(); //denote testing context
        
        PageReference pageRef = Page.attachPDFToOpportunity; //create a page reference to attachPDFToAccount.page
        Test.setCurrentPage(pageRef); //set page context
        ApexPages.StandardController standardController = new ApexPages.standardController(opportunities[0]); //instantiate the standard Account object controller
        attachPDFToOpportunity ext = new attachPDFToOpportunity(standardController); //instantiate the extension
        
        String validationURLString = ext.attachPDF().getURL(); //get the URL that is returned after the attachPDF() method is invoked
        String opportunityIdAsString = Id.valueOf(opportunities[0].Id); //variable represtenting the Account record Id as a String
        System.assertEquals(true, validationURLString.contains(opportunityIdAsString.left(15))); //validate that the URL contains the Id of the Account record
        
        Test.stopTest(); //revert from testing context
    }

}


Many thanks!
Best Answer chosen by kermit.thefrog
RituSharmaRituSharma
Too many DML statements means that in entire context more than 150 DML calls have been made. You will need to analyze the complete stuff that's getting executed.

All Answers

RituSharmaRituSharma
Explicitly call attachPDF method from the test class to improve the code coverage.
kermit.thefrogkermit.thefrog
Many thanks for looking into my issue. This is my first test class ever; so how do I do that "call attach pdf method"?
kermit.thefrogkermit.thefrog
I have added a line to call attach pdf. New code looks like this, but it gives me an error message: too many DML statements.
How to solve this?
@isTest
private class attachPDFToOpportunityTest {
    
    
    @isTest 
    static void attachPDFLogic() {
        
        List<Opportunity> opportunities = new List<Opportunity>();
        opportunities.add(new Opportunity(Name = 'TestOppApex', StageName = 'Qualification', CloseDate = date.today()));
        
        attachPDFLogic();
        
        insert opportunities;
        
         
        
        Test.startTest(); 
        
        PageReference pageRef = Page.attachPDFToOpportunity; 
        Test.setCurrentPage(pageRef); 
        ApexPages.StandardController standardController = new ApexPages.standardController(opportunities[0]); 
        attachPDFToOpportunity ext = new attachPDFToOpportunity(standardController); 
        
        String validationURLString = ext.attachPDF().getURL(); 
        String opportunityIdAsString = Id.valueOf(opportunities[0].Id); 
        System.assertEquals(true, validationURLString.contains(opportunityIdAsString.left(15))); 
       
        
        Test.stopTest(); 
    }

}

 
RituSharmaRituSharma
Too many DML statements means that in entire context more than 150 DML calls have been made. You will need to analyze the complete stuff that's getting executed.
This was selected as the best answer
kermit.thefrogkermit.thefrog
found it. The code works fine, but the error message was invoked through some other bad code in my org.
Many thanks for helping me!