SOLUTION: Make Attachment Mandatory in Catalog Item ServiceNow

One of the basic requirement while developing ServiceNow catalog item is To make attachment mandatory in catalog item in ServiceNow or make attachment mandatory on form in ServiceNow. Below I have shared the code which you have to write in catalog client script and in Script Include to separately to implement this functionality or to achieve this functionality on your service catalog item. I have also shared the video below which will provide complete demonstration to implement the required functionality.

How to Make Attachment Mandatory in Catalog Item ServiceNow ?

Paste below mentioned code in your onSubmit Catalog Client Script and on Client Callable Script Include.

Catalog Client Script Code:

function onSubmit() {
 //Type appropriate comment here, and begin script below  
var catalogItemID = g_form.getValue('sysparm_item_guid');


var demo = new GlideAjax('CheckAttachmentCatalogItem');
demo.addParam('sysparm_name', 'checkAttachment');
demo.addParam('sysparm_catalog_id', catalogItemID);
demo.getXMLWait();

var attachment=demo.getAnswer();
if(attachment == 'yes'){
        return true;
} else if(attachment == 'no'){
        alert("Please attach Image of your Employee Card");
        return false;
}
   
}

Script Include Code:

var CheckAttachmentCatalogItem = Class.create();
CheckAttachmentCatalogItem.prototype = Object.extendsObject(AbstractAjaxProcessor, {

checkAttachment: function(){
var gr = new GlideRecord("sys_attachment");
gr.addQuery('table_name', "sc_cart_item");
gr.addQuery('table_sys_id', this.getParameter('sysparm_catalog_id'));
gr.query();
if (gr.next()) {
         return 'yes';
}
else{
        return 'no';
}
},
    type: 'CheckAttachmentCatalogItem'
});

Please note: Please change class name "CheckAttachmentCatalogItem" accordingly to your script include name. 


Video Demonstration:

I below video we have demonstrated that how we can implement the required functionality in ServiceNow catalog item form.

I hope above code and video demonstration will definitely help you to make the attachment mandatory on ServiceNow form. Still, If you have any doubt or facing any problem to implement the  functionality "make attachment mandatory in catalog item servicenow" you can comment below in the comment box. I will reply on the same and if required we can also connect in hangout to resolve the same.

8 comments:

  1. Thanks for this amazing tip, however just want to mention that this will not work in Service Portal, I believe getXMLWait() is not supported, I ended up here looking a solution for this, we actually have same requirement for service portal our code is similar to yours we used to have all catalog items on Itil view, but now company wants to use all via portal.

    ReplyDelete
  2. For Portal side - Open your Catalog Item. Makesure your Catalog Item is in Default View. Then Go to Portal Settings tab, there you'll find one check box 'Mandatory Attachment'. Mark that tik. There you go.

    ReplyDelete
  3. Instead of code on a yes/no field type, why not use the attachment field type and make it mandatory.
    The form won't save until all Mandatory fields have data.

    ReplyDelete
    Replies
    1. Even after choosing "yes" on the field type like you mentioned but i will not attach anything . Here in this form still gets submitted, to check if it's really attached we need scripting.

      Delete
  4. Hi Sir
    This code working on classic view but when i am using on service portal view when i submit form without attachment so I am getting a error msg instead of alert pop .could you please suggest me how to fix it .

    ReplyDelete
  5. Hello Sir,I used this code but I have a issue with that because when i submit form without attaching a file on service portal view , so i get error msg instead of alert pop up . Could you please suggest me how to get rid of this problem?

    ReplyDelete
  6. Will this solution work for a scope application? If not, can you share what changes needs to be made to make it work in a scope application. Or maybe you can do a video on how to do a mandatory attachment in a scope application. Thank you for all your contribution to the Service Now community.

    ReplyDelete
  7. according to my script i am able to make the attachment mandatory on portal but not on ui..

    ReplyDelete

Thankyou !!!!

Powered by Blogger.