Make Attachment mandatory on Catalog Item based on field

Dazler
Mega Sage

Hi, I am needing to create a catalog client script to make an attachment mandatory based on a field selection on a Catalog Item.  I  know that there is a feature in the Madrid version to select "make attachment mandatory" but I do not need it for the entire catalog item, but only when a certain field is selected.  

This is what I have so far, but I am having trouble.  Any help will be truly appreciated.

function onSubmit() {

if(g_form.getValue('bulk_request') == 'true'){

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;

}

}

}

 

5 REPLIES 5

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Will this check be done on the Platform UI or on the Service Portal?

If Service Portal:
gel('sysparm_item_guid').value
Will not work, simply doesn't work.

For example scripting to use with Client Script, have a look at below article. Yes I know the title looks about the out-of-the-box checkbox which is not for variable dependent :-), though the article also contains scripting how to perform an attachment check, just add that scripting within an if condition checking your variable.
Service Portal Catalog Items: Hide Attachment / Mandatory Attachment [Madrid]

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Matthew Smith
Kilo Sage

Hi Dazler

I'd recommend putting in some more alerts to check the variable values are what you are expecting. At a quick glance, I suspect the bulk_request value could be a Boolean (and won't need the apostrophes), I don't know if gel will work on Service Portal and I don't believe you can put an exclamation mark before the gr.next() query (but could be wrong).

- Matt

 

 

Ishita Shrivast
Kilo Guru

Hi Dazler,

Try this code

var cat_id= $('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("please attach a file")

return false;

}

 

 

Regards,

Ishita Shrivastava

Developer

If my answer helped you in any way, please mark it as correct/helpful.

 

 

 

 

AbhishekGardade
Giga Sage

Check out this,

function onSubmit() {
var option = g_form.getValue('bulk_request');
try { //Works in non-portal ui

if (option == 'true') {

var attachments = document.getElementById('header_attachment_list_label');

if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {

alert('You must include the required attachment.');

return false;

}

}

}

catch (e) { //For Service Portal

var count = getSCAttachmentCount();

if (count <= 0 && option == 'true') {

alert('You must include the required attachment.');

return false;

}

}

}

 

NOTE: You could see the Isolate script is set to TRUE by default, this script would only execute if I uncheck the Isolate script or make it to FALSE

To disable this on a per-script basis, configure this form and add the "Isolate script" field.

The Isolate script disables this feature on script basis but if you need to disable it completely, then you should create a new system property on sys_properties table with name as "glide.script.block.client.globals", Type as true/false and set its value to false. This will disable the security feature for all scripts in the global scope. The property name vary for scoped applications with an addition of scope name as its prefix. For eg sn_customerservice.glide.script.block.client.globals would be the property name used for Customer Service application.

Related Knowledge Article - KB0694479

Please mark as Correct Answer and Helpful, if applicable.
Thank You!
Abhishek Gardade

Thank you,
Abhishek Gardade