You need to sign in to do that
Don't have an account?
Shruthi Narsi
Create PDF Test Class not working
I have written a test class to create PDF but logic is not working can anyone help me with the test class
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.print();
}
}
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.print();
}
}
Review below working examples for attach pdf test class.
https://developer.salesforce.com/forums/?id=9060G000000I5W6QAK
https://www.interactiveties.com/blog/2018/attach-pdf-code-coverage.php
https://blog.jeffdouglas.com/2010/07/14/attach-a-pdf-to-a-record-in-salesforce/
Thanks,
Vinay Kumar