Attachment requirement onSubmit works in Service Catalog but the same doesn't work in ServicePortal.

Azamat M_
Kilo Contributor

Hi all,

I have a client script that's prompts the user to attach a file in order to submit their request. This functionality works as intended in ServiceCatalog 'Try it' view however when the same test case is run on Service Portal the user is not prompted with any message and instead the request gets created without any indication of file attachment.

I have read many of the other posts that are closely related to what I am experiencing however, none of the suggestions have been able to fix this issue for me.

I was wondering if anyone out there has/had similar problems and what did you do in order to fix it? Any help will be much appreciated.

My current code:

function onSubmit() {
    (function(){
        var cat_id = gel('sysparm_item_guid').value;//g_form.getUniqueValue();
        if(g_form.getValue('slbEmergency') == 'nonEmergency'){
            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("Approval attachment is required in order submit this request!");
                gr.setAbortAction(true);
                return false;
            }
        }
        }());
        
    }

1 ACCEPTED SOLUTION

Thanks Mark,

It works well for me as well although I am using Jakarta.

Regards

Ankur

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

View solution in original post

9 REPLIES 9

Mihir Mohanta
Kilo Sage

Hi,

You have to write a ui script and call that from on-submit client script.

Please go through the below link for details.

 

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

 

Thanks,

Mihir

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Azamat,

This won't work in service portal because gel is not allowed in portal side; use below approach:

create a UI script

UI Script: AttachmentFunctions

function getPortalAttachmentCount() {

  var length;

  try {

  length = angular.element("#sc_cat_item").scope().attachments.length;

  } catch(e) {

  length = -1;

  }

  return length;

}

then use onSubmit like this

function onSubmit() {

  try { //Works in non-portal ui

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

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

  alert('Approval attachment is required in order submit this request!');

  return false;

  }

  } catch(e) { //For Service Portal

  var count = getPortalAttachmentCount();

  if(count <= 0) {

  alert('Approval attachment is required in order submit this request!');

  return false;

  }

  }

}

go to your service portal and within that you must see a theme field; go to that theme

in the related list of JS includes; click new

give some display name; select the UI script you just now created; submit the record

 

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

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

Just did a test on Kingston out-of-the-box environment to prove this is working. Works instantly!

Kind regards,
Mark

find_real_file.png

find_real_file.png

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Thanks Mark,

It works well for me as well although I am using Jakarta.

Regards

Ankur

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

Thank you Ankur so much, I have been banging my head against that client script for a long time. Thanks to you now I have the fix and it runs perfectly.