onSubmit script error: TypeError: Cannot read properties of null (reading 'value'):

Shari Romine We
Kilo Contributor

I am using a script to force adding an attachment to a request:

function onSubmit() {
var proof = gel('sysparm_item_guid').value;
//alert('proof'+proof);
var att = new GlideRecord("sys_attachment");
att.addQuery("table_name", "sc_cart_item");
att.addQuery("table_sys_id", proof);
// att.addQuery("sys_created_by", g_user.userName);
// att.addQuery("sys_created_on", '>=', 'javascript:gs.minutesAgo(10)');
att.query();
if (!att.next()) {
alert("Please attach a .zip file to the request containing the project that will be promoted to production before submitting.");
return false;
}
return true;
}

 

When I try to close the request I get this error:

Error MessageonSubmit script error: TypeError: Cannot read properties of null (reading 'value'):
function onSubmit() {
var proof = gel('sysparm_item_guid').value;
//alert('proof'+proof);
var att = new GlideRecord("sys_attachment");
att.addQuery("table_name", "sc_cart_item");
att.addQuery("table_sys_id", proof);
// att.addQuery("sys_created_by", g_user.userName);
// att.addQuery("sys_created_on", '>=', 'javascript:gs.minutesAgo(10)');
att.query();
if (!att.next()) {
alert("Please attach a .zip file to the request containing the project that will be promoted to production before submitting.");
return false;
}
return true;
}

This is not causing an error on other requests.  I am not proficient in JS so I have no idea how to fix this.  Any suggestions greatly appreciated.

7 REPLIES 7

palanikumar
Mega Sage

Hi

Are you testing this in ServicePortal? Below code does not work in Service Portal

gel('sysparm_item_guid').value

Refer below URL to make attachment mandatory in Service Portal

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

Thank you,
Palani

Mohith Devatte
Tera Sage
Tera Sage

Hello @Shari Romine Webster ,

instead of this you can utillize "Mandatory attachment" check box in the catalog item form which will make attachment mandatory  and throw the alert while you submit with out adding an attachment 

Search for your item and make the mandatory attachment true

 

find_real_file.png

find_real_file.png

MARK MY ANSWER CORRECT IF IT HELPS YOU

 

Soeren Maucher
Mega Sage

Hello,

Not sure about the script error, but you can make the attachment mandatory on the requested item itself. I think this could be an even simpler solution to your requirement.

find_real_file.png

I hope this helped!

Greetings,
Sören

Musab Rasheed
Tera Sage
Tera Sage

Hello,

It this related to Catalog item on Portal ? if yes why you need script when you can enforce from Catalog item form by checking checkbox ?

find_real_file.png

Regards

Please hit like and mark my response as correct if that helps
Regards,
Musab