How to delete the PDF generated for knowledge article after downloading by client on service portal?

XKun
Tera Contributor

Hello all,

 

I have created two script included functions, one for generate PDF of current knowledge article once client click on "Export" on Service Portal, another one will delete the PDF file and txt. afterwards in the system. Then, the pdf file will not be visible as attachment afterwards, which we don't want. However, I am unable to call the delete function on client script.

What we need: client can export knowledge article as pdf, and the pdf generated will not shown as attachment on the portal.

Could you please help me?

 

Here is the function for generate article as pdf:

var DownloadPDFCustom = Class.create();
DownloadPDFCustom.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    kbArticleDownload: function(kbID) {
        //call the creation of the articles.
        if (!kbID) {
            kbID = this.getParameter('sysparm_kbID');
        }
        var knowledgeRecord = new GlideRecord('kb_knowledge');
        var kbText = '';
        if (knowledgeRecord.get(kbID)) {
            if (new GlidePluginManager().isActive('com.snc.knowledge_blocks')) {
                kbText = new global.KBBlock().getArticleContent(knowledgeRecord, gs.getUserID());
            } else
                kbText = knowledgeRecord.getValue('text');
        }

        var v = new sn_pdfgeneratorutils.PDFGenerationAPI;
        var result = v.convertToPDF(kbText, "kb_knowledge", kbID, knowledgeRecord.number + " " + knowledgeRecord.getValue('short_description'));

        return '/sys_attachment.do?sys_id=' + result.attachment_id;
    },
    type: 'DownloadPDFCustom'
});
 
Here is the function to delete the pdf and txt. file generated:
var DeleteAvWAttachment = Class.create();
DeleteAvWAttachment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    deleteAttachment: function(kbID, kbNumber) {
        var pdfID = this.deleteOldAttachment(kbID, kbNumber, 'kb_knowledge', 'application/pdf');
        if (pdfID) {
            this.deleteOldAttachment(pdfID, 'Summary of : ' + kbNumber, 'sys_attachment', 'text/plain');
        }
    },

    deleteOldAttachment: function(kbID, kbNumber,tableName,contentType) {
        var attachmentID = '';
        var sysAtt = new GlideRecord('sys_attachment');
        sysAtt.addQuery("table_sys_id", kbID);
        sysAtt.addEncodedQuery("file_nameSTARTSWITH" + kbNumber);
        sysAtt.addQuery("table_name", tableName);
        sysAtt.addQuery("content_type", contentType);
        sysAtt.query();
        if (sysAtt.next()) {
            gs.info('FOUND ' + sysAtt.file_name);
            attachmentID = sysAtt.sys_id.toString();
            sysAtt.deleteRecord(); // Each export attaches a pdf to article. Delete the existing attachment before
        }
        gs.info(attachmentID);
        return attachmentID;
    },

    type: 'DeleteAvWAttachment'
});
 
On client script:
    c.exportThisArticle=function()
{
var ga = new GlideAjax('DownloadPDFCustom');
ga.addParam('sysparm_name' , 'kbArticleDownload');
ga.addParam('sysparm_kbID',c.data.article_sys_id);
ga.getXML(getResponse);
 
function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
window.open(answer);
}
 
var de = new GlideAjax('DeleteAvWAttachment');
de.addParam('sysparm_name','deleteAttachment');
de.addParam('sysparm_kbID', c.data.article_sys_id);
de.addParam('sysparm_kbNumber',c.data.number);
de.getXML(getResponseDe);
 
function getReponseDe(response){
var answerDe = response.responseXML.documentElement.getAttribute("answer");
alert ('delete' + answerDe);
}
}
 
Thank you in advance!
0 REPLIES 0