Onsubmit Client Script to check Attachments on Record producer

Chaisai
Kilo Expert

Hello,

 

I have a record producer on a custom table in custom application which has check box if checked need to attach the files , so i have been using below code it is working but even after attaching on the form i am getting same Alert and unable to submit the form , any help in this is much appreciated!

 

Onsubmit Client script code :

function onSubmit() {

attachOne = g_form.getValue('one');
attachTwo = g_form.getValue('two');
attachThree = g_form.getValue('three');

if(attachOne == 'true' || attachTwo == 'true' || attachThree == 'true') //checking if any of the attachment checkbox is ticked 
{

var att = new GlideRecord("sys_attachment");
att.addQuery("table_name", "sc_cat_item_producer"); 
att.addQuery("sys_created_by", g_user.userName); 
att.query();
if(!att.next())
{
alert("Please attach the Required Documnet to proceed with submitting.");
return false;
}

}

}

10 REPLIES 10

If you are using portal, then probably you may have to follow the below threads. We followed the same steps as mentioned in the below article and it worked fine for both portal and base view

 

https://community.servicenow.com/community?id=community_question&sys_id=dd618729db98dbc01dcaf3231f961977

OKay thanks for the links , i have been thinking of same so as you said it is working for portal with you , a quick question

Q : Can i write the UI script in the Global application and call the function in from client script which is in the Custom application ?

Yes, it will work. You can also create UI script specific to your application scope.

Chuck Tomasi
Tera Patron

Here's my take. Add 'var' before the g_form lines. 

Change the g_user.userName to g_user.userID. sys_created_by contains the login name (e.g. chuck.tomasi). 

function onSubmit() {

    var attachOne = g_form.getValue('one');
    var attachTwo = g_form.getValue('two');
    var attachThree = g_form.getValue('three');

    if (attachOne == 'true' || attachTwo == 'true' || attachThree == 'true') {

        var att = new GlideRecord("sys_attachment");
        att.addQuery("table_name", "sc_cat_item_producer");
        att.addQuery("sys_created_by", g_user.userID);
        att.query();
        if (!att.next()) {
            alert("Please attach the Required Documnet to proceed with submitting.");
            return false;
        }

    }

}

Ref: Training | ServiceNow Developers

Hi Chuck,

 

Thanks for your response!

 

For some reason even after me attaching any document to the record producer form it is still showing the alert to attach document and stopping me from submitting. Any thoughts ?