onsubmit client script for attachments without getelement

Nitya2
Kilo Guru

We are using below catalog client script for  mandatory attachments , but as per best practices servicenow asking us to use without gel function in 5th line (var cat_id = gel('sysparm_item_guid').value;)

kindly help me to change below code  without gel method

 

function onSubmit() {
//Type appropriate comment here, and begin script below
var condtn = g_form.getValue('Server_com_decom');
if(condtn == 'Commission'){
var cat_id = gel('sysparm_item_guid').value;
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", cat_id);
gr.query();
if (!gr.next()) {
alert("You must attach a file to submit.");
return false;
}
}
}

1 ACCEPTED SOLUTION

Hi 

Give a final try:

function onSubmit() {

var OwnOther = g_form.getValue('Server_com_decom');
if (OwnOther == 'Commission') {
var cat_id = g_form.getValue('sysparm_item_guid');
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", cat_id);
gr.query();
if (!gr.next()) {
          alert("You must add an attachment before submitting this request.");
          return false;
         }
}

}

 

Thanks

Thanks,
Murthy

View solution in original post

9 REPLIES 9

Upender Kumar
Mega Sage

Please review

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

Upender Kumar
Mega Sage

You can also get help from

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

 

Murthy Ch
Giga Sage

Hi @Nitya

 

You can try these two below codes:

1) On Submit() client script:

function onSubmit() {
 
var OwnOther = g_form.getValue('Server_com_decom');
if (OwnOther == 'Commission') {
try {
 
var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
 
alert("You must attach a file to submit");
 
return false;
 
}
} catch (e) {
 
if ((this.document.getElementsByClassName('get-attachment').length == 0)) {
 
alert('Please attach before submitting the request.');
 
return false;
}
 
}
}
}
 
 
2) On submit() client script:

Script: onSubmit

Note: Ensure Isolate Script field is set to False for this Catalog Client Script to ensure DOM manipulation works

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

var OwnOther = g_form.getValue('Server_com_decom');
if (OwnOther == 'Commission') {

var countRequired = 1;
if(window == null){
// portal
if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
alert('You must add attachment before submitting this request.');
return false;
}
}
else{
// native view
var length = $j("li.attachment_list_items").find("span").length;
if(length != countRequired){
alert('You must add attachmen before submitting this request.');
return false;
}
}
}

}
 
Thanks
Thanks,
Murthy

Nitya2
Kilo Guru

document.getElementById    is DOM manpulated functionality 

Servicenow /client are referring to write code with out   document.getElementById   or  gel 

As per client script best pratice, Avoid DOM manipulation. Instead of this I want to use Glide form API or some different approach.

 

kindly help me