I found a way to write an apex class that attaches my quote which is generated by an vf page as a pdf to my quote object.
Writing the test class is even a bigger problem for me. I get to 36% coverage and hop you guys can help me with it to get it working.
here's my class
public class quotePDFExtension {
ApexPages.StandardController controller;
public Quote quote {get;set;}
public PageReference rtn;
public quotePDFExtension(ApexPages.StandardController c){
quote = (Quote)c.getRecord();
rtn = c.view();
}
public PageReference attachQuotePDF() {
/* Get the page definition */
PageReference pdfPage = Page.angebot;
pdfPage.getParameters().put('id',quote.id);
/* generate the pdf blob */
Blob pdfBlob = pdfPage.getContent();
/* create the attachment against the offer */
Attachment a = new Attachment(parentId = quote.id, name=quote.Angebot_Nr__c + '.pdf', body = pdfBlob);
/* insert the attachment */
insert a;
/* send the user back to the offer detail page */
return rtn;
}
}
and here is my test class
@isTest
public class attachQuoteTestClass
{
static testMethod void testMethod1()
{
Quote testQuote = new Quote(OpportunityId = '0061x00000AN92q', Gutachtennummer__c = '999195041551', Gutachten_Datum__c = Date.today(), Angebotsdatum__c = Date.today());
testQuote.Name='Test Quote record' ;
insert testQuote;
/*
Attachment attach = new Attachment();
attach.Name='Unit Test Attachment';
Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
attach.body=bodyBlob;
attach.parentId = testQuote.Id;
insert attach;
List<Attachment> attachments=[select id, name from Attachment where parent.id=:testQuote.id];
System.assertEquals(1, attachments.size());
ApexPages.StandardController sc = new ApexPages.StandardController(testQuote);
quotePDFExtension ctr = new quotePDFExtension(sc);
*/
Test.StartTest();
ApexPages.StandardController sc = new ApexPages.StandardController(testQuote);
quotePDFExtension testQuotePlan = new quotePDFExtension(sc);
PageReference pageRef = Page.angebot;
pageRef.getParameters().put('id', String.valueOf(testQuote.Id));
Test.setCurrentPage(pageRef);
Blob b = blob.valueof('error');
Try { b = pageRef.getContent();}
Catch(Exception e)
{Attachment a = new Attachment(parentId = testQuote.id, name=testQuote.Angebot_Nr__c + '.pdf', body = b);
insert a;}
Test.StopTest();
}
}
Thanks a lot Thomas