I want to get the attachment length for the catalog item from service portal before request submiti

Shantharao
Kilo Sage

Hi All,
I want to get the attachment length for the catalog item from service portal before request submit
I have one variable called "request approval" which is check box
if above check box is true,
I need to make the attachment mandatory before submitting the request
I am trying to get the attachment length using onSubmit client but not working as expected please find the below scripts, I have tested in different ways,
Please try the above requirement in your PDIs and provide the working code here 

ClientScript

function onSubmit() {
    //Type appropriate comment here, and begin script below

    var sysId = g_form.getUniqueValue(); // current record sys_id

    var ga = new GlideAjax('AttachmentChecker');
    ga.addParam('sysparm_name', 'getAttachmentCount');
    ga.addParam('sysparm_sys_id1', sysId);

    var attachmentCount = 0;
    g_form.addInfoMessage("from ATT scrippt sysId=>" + sysId + "initialize value attachmentCount=>" + attachmentCount);

    ga.getXMLAnswer(function(response) {
        attachmentCount = parseInt(response);
        alert("Answer from ATT script attachmentCount=>" + attachmentCount);

        if (attachmentCount === 0) {
            alert("Please add at least one attachment before submitting.");
            return false; // prevent submission
        } else {
            return true; // allow submission
        }
    });

    return false; // stop default submit until GlideAjax returns
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++
ScriptInclude
var AttachmentChecker = Class.create();
AttachmentChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {

   

    getAttachmentCount: function() {
        var sysId = this.getParameter('sysparm_sys_id1');
        var ga = new GlideRecord('sys_attachment');
        ga.addQuery('table_sys_id', sysId);
        ga.query();
        gs.log("count=>" + ga.getRowCount() + " sysId=>" + sysId, "hb");
        return ga.getRowCount();
    },


    type: 'AttachmentChecker'
});



Thanks

1 REPLY 1

PoonkodiS
Mega Sage

Hi @Shantharao 

could you try this script

var allowSubmit = false;

function onSubmit() {

// If already validated, allow submit
if (allowSubmit) {
return true;
}

// Check if checkbox variable is true
if (!g_form.getValue('request_approval')) {
return true; // no attachment required
}

var sysId = g_form.getUniqueValue(); 

var ga = new GlideAjax('AttachmentChecker');
ga.addParam('sysparm_name', 'getAttachmentCount');
ga.addParam('sysparm_sys_id', sysId);

ga.getXMLAnswer(function(answer) {

var count = parseInt(answer);

if (count === 0) {
g_form.addErrorMessage('Please add at least one attachment before submitting.');
allowSubmit = false;
} else {
allowSubmit = true;
g_form.submit(); // submit AFTER validation
}
});

return false; // ALWAYS stop initial submit
}

 

Script Include (Client Callable)

var AttachmentChecker = Class.create();
AttachmentChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getAttachmentCount: function () {

var sysId = this.getParameter('sysparm_sys_id');

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', sysId);
gr.query();

return gr.getRowCount().toString();
},

type: 'AttachmentChecker'
});

 

Regards,

Poonkodi