The CreatorCon Call for Content is officially open! Get started here.

Verify Attachment Record

Michael Galang
Tera Contributor

Hello experts, 

I have a request to do the following requirement for kb_knowledge table. 

I  created an onLoad client script Name: Verify Attachment Record

Action:
- When the article is checked out and the version is a new version and the article has an attachment set the
Attachment Reviewed field to mandatory.
- When the article checked out and the Attachment Reviewed checkbox is set to true and there is an attachment on the knowledge article, set the checkbox to false and mandatory.

But the following client script  and script include are not working, kindly advise on how to make it work. Thanks in advance. 

Client script: 

function onLoad() {
    var articleSysId = g_form.getUniqueValue();

    if (articleSysId) {
        // Create a GlideAjax call to the script include
        var ga = new GlideAjax('VerifyAttachmentRecordUtils');
        ga.addParam('sysparm_name', 'checkAttachmentStatus');
        ga.addParam('sysparm_article_sys_id', articleSysId);
        ga.getXMLAnswer(function(response) {
            var result = JSON.parse(response);
           
            // Check conditions for mandatory Attachment Reviewed field
            if (result.isCheckedOut && result.isNewVersion && result.hasAttachment) {
                g_form.setMandatory('u_attachment_reviewed', true);
                alert('This KB is checked out, a new version, and has an attachment.');
            }

            // When the article is checked out and the Attachment Reviewed checkbox is set to true and there is an attachment on the knowledge article
            if (result.isCheckedOut && result.attachmentReviewed && result.hasAttachment) {
                g_form.setValue('u_attachment_reviewed', false); // Reset to false
                g_form.setMandatory('u_attachment_reviewed', true); // Set to mandatory
                alert('This KB is checked out, Attachment Reviewed is true, and has an attachment.');
            }
        });
    }
}

Script include:

// Name: VerifyAttachmentRecordUtils
// Client Callable: true
// API Name: VerifyAttachmentRecordUtils

var VerifyAttachmentRecordUtils = Class.create();
VerifyAttachmentRecordUtils.prototype = {
    initialize: function() {},

    checkAttachmentStatus: function(articleSysId) {
        var result = {
            isCheckedOut: false,
            isNewVersion: false,
            hasAttachment: false,
            attachmentReviewed: false
        };

        // Check if the article is checked out
        var articleGr = new GlideRecord('kb_knowledge');
        if (articleGr.get(articleSysId)) {
            result.isCheckedOut = this.checkHistory(articleSysId);
            result.attachmentReviewed = articleGr.u_attachment_reviewed;
           
            // Check if the article has attachments
            result.hasAttachment = this.checkAttachments(articleSysId);
           
            // Check if the article is a new version (if it's the latest version)
            result.isNewVersion = this.isLatestVersion(articleSysId);
        }

        return result;
    },

   

isLatestVersion: function(articleSysId) {
var gr = new GlideRecord('kb_knowledge');
gr.addQuery('sys_id', articleSysId);
gr.orderByDesc('version');
gr.setLimit(2); // Get the current and the next version
gr.query();

if (gr.next()) {
var currentVersion = gr.getValue('version');
if (gr.next()) { // Move to the next record, if it exists
var nextVersion = gr.getValue('version');
return currentVersion > nextVersion; // current version is the highest
}
return true; // No next version found, current is the latest
}
return false;
}


    checkHistory: function(articleSysId) {
        var gr = new GlideRecord('sys_history_line');
        gr.addQuery('set.id', articleSysId);
        gr.addQuery('field', 'workflow_state');
        gr.addQuery('oldvalue', 'Published');
        gr.addQuery('newvalue', 'Draft');
        gr.query();
        return gr.hasNext();
    },

    type: 'VerifyAttachmentRecordUtils'
};







1 REPLY 1

Mark Manders
Mega Patron

What kind of field is your custom field?


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark