Limit number of attachements on catalog Item

now_dev
Mega Expert

Hi All,

I have created a macro for adding attachments on catalog item( Client don't want to use oob feature) and now wanted to restrict the user from adding more than one attachment. Also, wanted to add an override feature.

So, If there is more than one attachment, the user should receive a message saying " Only one invoice allowed, do you want to replace the existing one? — Yes/NO"

If the user clicks on "Yes" it should override with the new attachment.

Any help would be appreciated.

Thanks

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Ks,



you can have onSubmit catalog client script to achieve this:


if user clicks on Yes then do you want to remove the latest attachment from the cart ? can you explain more on this



var sys_id = gel('sysparm_item_guid').value;
var attachment = new GlideRecord('sys_attachment');

attachment.addQuery('table_name','sc_cart_item');


attachment.addQuery('table_sys_id',sys_id);

attachment.query();    


var numberOfAttachments = attachment.rows.length;



if(numberOfAttachments >1){


var confirmBox = confirm("Only one invoice allowed, do you want to replace the existing one? — Yes/NO");


if(confirmBox.toString() == 'true'){


// logic to remove latest attachment


return true;


}


else{


return false;


}


}


return true;




Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

13 REPLIES 13

now_dev
Mega Expert

Hi Ankur,



There is a slight change the requirement now. When user attach attachment and click "Attach" button, would it be possible to limit the user with Just one attachment?


When user hit "Attach", can I restrict user to add more than 1 attachment?



find_real_file.png


Hi Ks,



Yes you can check onSubmit and stop form submission if more than 1 attachments.



var sys_id = gel('sysparm_item_guid').value;
var attachment = new GlideRecord('sys_attachment');

attachment.addQuery('table_name','sc_cart_item');


attachment.addQuery('table_sys_id',sys_id);

attachment.query();    


var numberOfAttachments = attachment.rows.length;



if(numberOfAttachments >1){


alert("Only 1 attachment can be added");


return false;


}


return true;



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

now_dev
Mega Expert

Hi Ankur,



Above shared code, I have already implemented but this is running when the user inserts attachment and click on order now button.


Also, I forgot to mention that I am working on Geneva version.



What I am trying to do, is to stop user to add multiple attachments before submitting the form


Hi Ks,



So it will stop the   user from form submission if more than 1 attachments. I think you are saying that in scoped app gel() and GlideRecord() in client side won't work.


So one solution to that is have this onSubmit client script in global scope and test it once.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

now_dev
Mega Expert

Hi Ankur,



Let me rephrase my question. Below is the attachment window which opens when the user tries to insert attachment.



I am working in global scope application. on Submit script works fine but I have a requirement where I want to prompt the user when then click on Attach button highlighted in yellow.



This attachment window looks slightly different in Geneva.



find_real_file.png