UI action not working in workspace

pk2184046
Mega Sage

Hi all,

    function downloadPDF() {
    var ga = new GlideAjax('sn_hr_core.GSSPDownloadpdf'); // Script Include name
    ga.addParam('sysparm_name', 'generatePDF');
    ga.addParam('sys_id', g_form.getUniqueValue()); // Current record sys_id
    ga.addParam('template_id', 'aa3586312b017a508e43f95fee91bf90'); // Template sys_id
    ga.addParam('pdf_name', 'Online Template.pdf');

    // g_form.showFieldMsg('downloadPDF', 'Generating PDF, please wait...', 'info');

    ga.getXMLAnswer(function(response) {
        if (response) {
                // g_form.save();
            var downloadUrl = '/sys_attachment.do?sys_id=' + response;
            window.location.href = downloadUrl; // Trigger download
                location.reload();
        } else {
            alert('Failed to generate PDF.');
        }
    });
   
}

I'm using the above code to generate a pdf in the hr task which is working in native UI but in the HR agent workspace it is not working. Workspace form button is checked true
workspace code

 

    function downloa
function onClick(g_form) {
    var ga = new GlideAjax('sn_hr_core.GSSPDownloadpdf'); // Script Include name
    ga.addParam('sysparm_name', 'generatePDF');
    ga.addParam('sys_id', g_form.getUniqueValue()); // Current record sys_id
    ga.addParam('template_id', 'aa3586312b017a508e43f95fee91bf90'); // Template sys_id
    ga.addParam('pdf_name', 'Online Template.pdf');

    // g_form.showFieldMsg('downloadPDF', 'Generating PDF, please wait...', 'info');

    ga.getXMLAnswer(function(response) {
        if (response) {
                // g_form.save();
            var downloadUrl = '/sys_attachment.do?sys_id=' + response;
            window.location.href = downloadUrl; // Trigger download
                location.reload();
        } else {
            alert('Failed to generate PDF.');
        }
    });
   
}
@Ankur Bawiskar can you please help
Pasting the script include
var GSSPDownloadpdf = Class.create();
GSSPDownloadpdf.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    generatePDF: function() {
   var recordId = this.getParameter('sys_id');
        var templateId = this.getParameter('template_id');
        var pdfName = this.getParameter('pdf_name');

        var attachmentSysId = new sn_doc.GenerateDocumentAPI()
            .generateDocumentForTask(recordId, templateId, pdfName);

        return attachmentSysId; // Return the sys_id of the attachment
    },

    type: 'GSSPDownloadpdf'
});



2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@pk2184046 

is the script include client callable?

is response coming from script include?

If not then ensure it's made Accessible from All Scopes

if response is coming then use this to open the URL

 if (response) {
            // g_form.save();
            var downloadUrl = '/sys_attachment.do?sys_id=' + response;
            open(downloadUrl);
        } 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

The issue is due to the script include which is set to this application scope only

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@pk2184046 

is the script include client callable?

is response coming from script include?

If not then ensure it's made Accessible from All Scopes

if response is coming then use this to open the URL

 if (response) {
            // g_form.save();
            var downloadUrl = '/sys_attachment.do?sys_id=' + response;
            open(downloadUrl);
        } 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

The issue is due to the script include which is set to this application scope only