There is a weird behaviour when referencing a visualforce page.
I have a screen flow and I'm testing an Apex Action using @InvocableMethod. My method receives a List of Ids, and I use those to get a visualforce renderAs="pdf" that will render content using fields of each record, and insert them as attachments:
@InvocableMethod(label='Adjuntar PDF')
public static void adjuntarPDF(List<Id> presupuestos){
List<Attachment> adjuntos = new List<Attachment>();
for(Id presId : presupuestos){
System.debug('presupuestoId ====> ' + presId);
PageReference pdf = Page.PDFPrueba;
pdf.getParameters().put('id', presId);
Attachment adjunto = new Attachment();
adjunto.Name = 'PDF PRES';
adjunto.Body = pdf.getContent();
adjunto.ParentId = presId;
adjuntos.add(adjunto);
}
insert adjuntos;
}
Visualforce controller:
public with sharing class PDFPruebaController {
public String presupuestoId;
public Quote presupuesto {get;set;}
public PDFPruebaController() {
presupuestoId = ApexPages.currentPage().getParameters().get('id');
System.debug('presupuestoId visualforce =====> ' + presupuestoId);
presupuesto = [SELECT Id FROM Quote WHERE Id = :presupuestoId];
}
}
For some reason, when testing the Screen flow, The apex controller throws a List has no rows for assignment error, when executing the query. The thing is, I logged the Id in the controller to see if it was being passed correctly, and it was, according to the logs:
The Id matches what the flow debugger tells me of the created record.
This is happening when debugging and also when trying to execute the flow normally. Is this an unexpected error, or am I missing something?
pdf.getContent()
operates outside the current transaction so if the preceding Apex code lines inserted the record thePDFPruebaController
would not see it as the transaction containing the insert will not have committed at that point. But I would guess that this is not a problem in flows, though I don't know what the transactional model is for multiple @InvocableMethod calls so perhaps it is a problem...