I want to insert a record in an object 'Invoice__c', after that I want to create an attachment related to this invoice. This attachment is a visualforce rendered as PDF. I have problems to call createInvoiceAttachment after createInvoice int the same execution context. In different context everything is right. What can i do?
Exectuion
Invoice__c i = Utils.createInvoice('0064E000002Jc91');
Utils.createInvoiceAttachment(i);
Utils class
public class Invoice {
public static Invoice__c createInvoice(String oppId){
Invoice__c inv = new Invoice__c();
inv.opp__c = oppId;
insert inv;
}
public static Attachment createInvoiceAttachment(Invoice__c inv){
PageReference pdf = Page.InvoicePage;
pdf.getParameters().put('id',inv.id);
Attachment at = new Attachment();
at.Body = pdf.getContentAsPDF();
at.ParentId = inv.Id;
insert at;
}
}