via script Tool , how to lookup for the data in the attachement of the KB

Sundeep Kumar1
Tera Contributor

Bellow is the script tool which i wrote and it works perfectly fine to extract the information from a kb article, however  sometime i am not able to get the requested information from the KB article which has an attachment ( PDF, word, csv, excel):

can someone please assist, how to extract information from kb article  which has an attachment ( PDF, word, csv, excel).

 

Script inputs 

Input name

 

Description

number

Knowledge article number

 

Script:
(function(inputs) {

    // only string inputs are allowed

    // return outputs object where the keys in it are understandable by LLM

    // Retrieve KB article

    var kb_gr = new GlideRecord('kb_knowledge');

    kb_gr.addQuery('number', inputs.number);

    kb_gr.orderByDesc('version');

    kb_gr.setLimit(1);

    kb_gr.query();

 

    if (kb_gr.next()) {

        return kb_gr.text.toString();

    } else {

        throw new Error('No KB found with number ' + inputs.number);

    }

 

})(inputs);

5 REPLIES 5

rpriyadarshy
Tera Guru

  See if this heslps you . You Can tweak it as per ur need.

 

(function(inputs) {

 

    // Retrieve KB article (latest version by number)
    var kb_gr = new GlideRecord('kb_knowledge');
    kb_gr.addQuery('number', inputs.number);
    kb_gr.orderByDesc('version');
    kb_gr.setLimit(1);
    kb_gr.query();

 

    if (!kb_gr.next()) {
        throw new Error('No KB found with number ' + inputs.number);
    }

 

    // Start with KB article body
    var outputText = '';
    outputText += (kb_gr.text ? kb_gr.text.toString() : '');

 

    // ---- Attachments: read text content (or "Summary of : <file>.txt") ----
    var gsa = new GlideSysAttachment(); // Attachment API 
    var attGR = gsa.getAttachments('kb_knowledge', kb_gr.getUniqueValue()); // list attachments 

 

    var attachmentTextParts = [];
    var MAX_ATTACHMENTS_TO_READ = 10;         
    var MAX_CHARS_PER_ATTACHMENT = 20000;     
    var readCount = 0;

 

    while (attGR.next() && readCount < MAX_ATTACHMENTS_TO_READ) {

 

        var fileName = attGR.getValue('file_name') + '';
        var contentType = attGR.getValue('content_type') + '';

 

        // Skip the auto-generated summary files in the "original loop"
        // We'll include summaries when we either detect they are the file itself OR when we look them up.
        var isSummaryTxt = fileName.indexOf('Summary of :') === 0 && fileName.endsWith('.txt');

 

        var textContent = '';

 

        // 1) If attachment itself is readable text, read it directly via getContent()
        // getContent(sys_attachment GR) returns content as string 
        if (contentType.indexOf('text/') === 0 || fileName.match(/\.(txt|md|csv|json|xml|log)$/i) || isSummaryTxt) {
            textContent = gsa.getContent(attGR) + '';
        }
        else {
 
            // "Summary of : <originalFile>.txt" for readable binary docs (pdf/doc/xlsx etc.) 
            var summaryName = 'Summary of : ' + fileName + '.txt';

 

            var sumGR = new GlideRecord('sys_attachment');
            sumGR.addQuery('table_name', 'kb_knowledge');
            sumGR.addQuery('table_sys_id', kb_gr.getUniqueValue());
            sumGR.addQuery('file_name', summaryName);
            sumGR.setLimit(1);
            sumGR.query();

 

            if (sumGR.next()) {
                textContent = gsa.getContent(sumGR) + ''; // extracted text lives here 
            }
        }

 

        // Normalize + trim
        if (textContent) {
            textContent = textContent.replace(/\u0000/g, ''); // remove nulls if any

 

            if (textContent.length > MAX_CHARS_PER_ATTACHMENT) {
                textContent = textContent.substring(0, MAX_CHARS_PER_ATTACHMENT) +
                    '\n...[truncated to ' + MAX_CHARS_PER_ATTACHMENT + ' chars]';
            }

 

            attachmentTextParts.push(
                '\n\n--- Attachment: ' + fileName + ' (' + contentType + ') ---\n' + textContent
            );
            readCount++;
        }
    }

 

    if (attachmentTextParts.length > 0) {
        outputText += '\n\n====================\nATTACHMENTS CONTENT\n====================';
        outputText += attachmentTextParts.join('');
    }

 

    return outputText;

 

})(inputs);

 

Regards

RP