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

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

Hi Ankur,



Thanks for the reply.


Answer to your question -- if the user clicks on Yes then do you want to remove the latest attachment from the cart.



If the user clicks on Yes, I wanted to delete existing attachment and allow them to attach the new attachment.


Hi Ks,



So consider user attaches 2 attachments file and file and gets the confirm box message. and user clicks on yes then which attachment should be removed?


file 1 or file 2 or the one which was attached recently or both?



Regards


Ankur


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

Hi Ks,



Any update on this?



Regards


Ankur


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