function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jonathan Wolff 7Jonathan Wolff 7 

Create a button to start XLSX-Genarator Apex

Hello, I have a XLSXGenerator apex. I want to start the apex by using a button. For now I can only start it by workbench.
In workbench i make apex execute with the command:
XLSXGenerator.generate(new List<String>{})

The XLSXGenarator code is:
 
public class XLSXGenerator {
    //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;
    }

}

​​​​​​​
PriyaPriya (Salesforce Developers) 
Hi Jonathan,

Can you please check the below method

Now create a custom button:
Goto --> Setup --> Object --> Buttons, links and Actions section-->Click New Button or Link.
Enter the Name of the button
Behaviour : Execute Javascript
Content source : On-Click Javascript
use below JS:
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
if({!Account__c.Name}!=Null)
{
sforce.apex.execute("SampleClass","sampleMethod",{}"});
alert("This is {!Account__c.Name}");
}

Hope this is helpful!

Regards,
Ranjan
Jonathan Wolff 7Jonathan Wolff 7
Hi Priya, I tried it your way but the problem I have is that the Javascript button does not show in the Page layout and I cant even add the button in Edit Page (does not show up)