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
Shruthi NarsiShruthi Narsi 

Create PDf Test Class

I have written a apex class to generate PDF On Quote. I have wirtten a test class but I am getting below error

User-added image

Apex Class

public class QuotePDF {
    public boolean show{get;set;}
    public id pdtId{get;set;}
    public Quote op{get;set;}
    public QuoteLineItem oli{get;set;}
    public string pb{get;set;}
    public Id opid{get;set;}
    public id prodid{get;set;}
    public list<QuoteLineItem> OliList{get;set;}
    public list<Quotelineitem> OliCreate;
    public integer deleteIndex{get;set;}
    
    public QuotePDF()
    {
        show=false;
        oli=new QuoteLineItem();
        op=new Quote();
        OliList=new list<QuoteLineItem>();
        OliCreate=new list<Quotelineitem>();
    }
    
    public void save(){
        pricebook2 spbid=[select id from pricebook2 where isStandard=true limit 1];
        
        opportunity o1=[select id from opportunity limit 1];
        op.OpportunityId=o1.id;
        op.Pricebook2Id=spbid.id;
        upsert op;
        map<id,id>pdtTopbemap=new map<id,id>();
        
        for(pricebookentry pb:[select product2id,id from pricebookentry where pricebook2id =:spbid.id ])
        {
            pdtTopbemap.put(pb.product2id,pb.id);
        }
        for(QuoteLineItem o:OliList)
        {
            o.QuoteId=op.id;
            o.unitprice=o.unitprice; 
            o.quantity=o.quantity;
            o.pricebookentryid=pdtTopbemap.get(o.product2id);
            o.Product2Id=o.product2id;
            OliCreate.add(o);
        }
        insert OliCreate;
    }
    public void AddOli()
    {
        show=true;
        QuoteLineItem olitemp = new QuoteLineItem();
        OliList.add(olitemp);
    }
    
    public void deleteOpp()
    {
        OliList.remove(deleteIndex);
    }
}

Test Class

@isTest
Public class QuoteApexClassTest {
    @isTest public static void QuoteApextestMethod() {
        Opportunities__c opp = new Opportunities__c();
        opp.Name = 'TestOpp';
        opp.Close_Date__c = System.today() + 5;
        opp.Stage__c = 'Prospecting';
        Insert opp;
        
        Quotes__c q = new Quotes__c();
        q.Name = 'test';
        q.OpportunityId__c = opp.Id;
        Insert q;
        
        ApexPages.StandardController sc = new ApexPages.StandardController(q);
        PageReference pageRef = Page.QuoteTemplate;
        pageRef.getParameters().put('id', String.valueOf(q.Id));
        Test.setCurrentPage(pageRef);
        
        QuoteApexClass qac = new QuoteApexClass(sc);        
        qac.saveQuoteAsPDFandEmail();
        qac.attachPDF();
        qac.getShowPrintLink(); 
        qac.print();
    } 
}

 
AbhishekAbhishek (Salesforce Developers) 
Hi Shruthi,

Try the code as mentioned in the below blog,

https://gist.github.com/citrus/19ce62aded1e6d9ff8ffa25db00bad71

https://developer.salesforce.com/forums/?id=906F0000000AbLQIA0

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks.