I'm able to get all the data by calling the Page.getContentAsPDF()
function by selecting all the record from Account
object.
Basically, I'm loading the pdf content based on query from page constructor with specific parameter as the condition in my query:
PDF generator page:
...
<thead >
<tr>
<td>No.</td>
<td>Outlet Name</td>
<td>Entity</td>
<td>Address</td>
<td>Nominal</td>
<td>Minimum Purchase</td>
</tr>
</thead>
<tbody>
<apex:repeat id="repeatdata" var="dt" value="{!selectedProductOutletList}" >
<tr>
<td>{!FLOOR(count)}</td>
<td>{!dt.Outlet__r.Name}</td>
<td>{!dt.Outlet__r.Account__r.Name}</td>
<td>{!dt.Address__c}</td>
<td>{!dt.Voucher_Nominal_DSText__c}</td>
<td>{!dt.Voucher_Minimum_Purchase_DSText__c}</td>
</tr>
<apex:variable var="count" value="{!count+ 1}"/>
</apex:repeat>
</tbody>
...
PDF generator controller:
public class PDFGeneratorController {
...
public List<Account> selectedProductOutletList{get;set;}
public PDFGeneratorController() {
//get all selection based on selectedid parameter, this query not getting updated data although the getContentAsPDF() is running after DML update code
//selectedIds example: 0012800001BOPGW,0012800001BOPGX,0012800001BOPGY
String selectedIds = ApexPages.currentPage().getParameters().get('sid');
String selectedIdsDec = EncodingUtil.urlDecode(selectedIds, 'UTF-8');
List<String> selectedIdList = selectedIdsDec.split(',');
DescribeSObjectResult dr = Account.getSObjectType().getDescribe();
List<String> fieldList = new List<String>(dr.fields.getMap().keySet());
String fields = String.join(fieldList, ',');
String qAll = 'SELECT ' + fields + ', Outlet__r.Name, Outlet__r.Account__c, Outlet__r.Account__r.Name, Outlet__r.OwnerId, Outlet__r.Owner.Name, Outlet__r.CreatedDate'
+ ' FROM Account';
qAll += ' WHERE Id IN :selectedIdList'
selectedProductOutletList = Database.query(qAll);
}
...
}
But the main problem is, I already make change to the account record and it's not getting reflected in my getContentAsPDF()
. Because the order of updating the Account record happens after getContentAsPDF()
so the pdf file still showing the old Account data.
The save function
...
public void processSelected() {
Voucher__c voucherObj = new Voucher__c();
voucherObj.Name = 'Record name';
insert voucherObj;
//this will update the account record
List<Account> productOutletToUpdatePrice = new List<Account>();
for (Account productOutletObj : (List<Account>) stdSetControllerProductOutlet.getRecords()) {
productOutletObj.Price__c = 'Some values that updated from apex class';
productOutletObj.Minimum_Purchase__c = 'Some values that updated from apex class';
productOutletToUpdatePrice.add(productOutletObj);
}
update productOutletToUpdatePrice;
//populate urlencoded selected ids
//I only populate the id here, all the records must be getting updated by above code
//these id will be passed to pdfpagecontroller to be consumed by query
List<Account> selectedProductOutletList = (List<Account>) stdSetControllerProductOutlet.getRecords();
List<String> selectedIdList = new List<String>();
for (Account accountObj : selectedProductOutletList) {
selectedIdList.add(accountObj.Id);
}
String selectedIds = String.join(selectedIdList, ',');
String selectedIdsEnc = EncodingUtil.urlEncode(selectedIds, 'UTF-8');
//generate selected account list pdf from visualforce page
//[main problem] this pdf page generation not getting the recent update from above code
PageReference prGenerateSelected = Page.PDFGeneratorPage;
prGenerateSelected.getParameters().put('sid', selectedIdsEnc);
prGenerateSelected.setRedirect(true);
Blob outletListBlob = prGenerateSelected.getContentAsPDF();
//put generated pdf as attachment
Attachment outletListAtt = new Attachment();
outletListAtt.ParentId = voucherObj.Id;
outletListAtt.ContentType = 'application/pdf';
outletListAtt.Name = 'Vouchername.pdf';
outletListAtt.Body = outletListBlob;
insert outletListAtt;
}
...
So, is there any workaround for make sure the Page.getContentAsPDF()
is running after I update the record?
I understand that the current page and the PDFGeneratorPage is different context, but I already made change into database level and the page still doesn't show the recent data.
I need to get recent record (that was updated) to be showing in Page.getContentAsPDF()