You need to sign in to do that
Don't have an account?
Jonathan Wolff 7
Execute method from apex with VF button
Hi I have a apex class 'XLSXGenerator' which contains a method generate. When I insert XLSXGenerator.generate(new List<String>); in the execute Apex in Workbench, i get an xlsx as result. But I would like to trigger it with a visualforce button instead of workbench. Could you tell me how i can achive this?
My apex class:
My apex class:
public class XLSXGenerator { public PageReference generate() { return null; } //public static String generate(List<String> textList) { public static String generate(List<String> AccList) { // Build XLSX File Content PageReference xlsxTemplate = page.XLSXTemplate; // xlsxTemplate.getParameters().put('textList', System.JSON.serialize(textList)); xlsxTemplate.getParameters().put('AccList', System.JSON.serialize(AccList)); Blob xlsxContent; if (Test.isRunningtest()) { xlsxContent = Blob.valueOf('Sample'); } else { xlsxContent = xlsxTemplate.getContent(); } // Build XLSX File Frame StaticResource xlsxTemplateFrame = [SELECT Body FROM StaticResource WHERE Name = 'XLSXTemplateFrame' LIMIT 1]; Zippex xlsx = new Zippex(xlsxTemplateFrame.Body); // Add the Content to the Frame to complete the File xlsx.addFile('xl/worksheets/sheet1.xml', xlsxContent, null); // Save XLSX File ContentVersion cv = new ContentVersion(); String title = 'XLSXExample'; cv.Title = title; cv.PathOnClient = title + ' - ' + DateTime.now() + '.xlsx'; cv.VersionData = xlsx.getZipArchive(); insert cv; Id contentDocumentid = [SELECT Id FROM ContentDocument WHERE LatestPublishedVersionId = :cv.Id].Id; return URL.getOrgDomainUrl().toExternalForm() + '/sfc/servlet.shepherd/document/download/' + contentDocumentid; } }
You can use <apex:actionFunction> and <button> with onclick event in VF.
for example :
<button value="click" label="button1" onclick="{!showAction}"/>
<apex:actionFucntion action="{!generate}" name="showAction">
<apex:param name="firstParam" value="<LIST_OF_STRING>" />
</apex:actionFunction>
In Apex you can get calue of <apex:param> by ApexParameter.getCurrentPage().getParameters().get('firstParam');
alternatively you can create a variable in apex class and use assignTo attribute of <apex:param>
<apex:param name="firstParam" assignTo = "{!VARIABLE_NAME}" value="<LIST_OF_STRING>" />
Note : <apex:actionFunction> should be child of <apex:form>
If it helps, Please mark this as best answer!!