The CreatorCon Call for Content is officially open! Get started here.

Make attachment clip mandatory while submitting request

roomawakar
Tera Contributor

Hi All,

 

I am having a requirement to make the attachment clip mandatory while submitting the request once a particular check box is selected.

Firstly, i wrote a onSubmit catalog client script with the below code but this is working on desktop view but it isn't working on the Employee Center Service Portal.

 

if (g_form.getValue('sbx_crr_requestType') == 'Manual Letter Generation for Address correction Requests') {

 

        try {

            var cat_id_return = gel('sysparm_item_guid').value;

            //alert('Happy '+cat_id_return);

            var gr = new GlideRecord("sys_attachment");

            gr.addQuery("table_name", "sc_cart_item");

            gr.addQuery("table_sys_id", cat_id_return);

            gr.query();

            if (!gr.next()) {

                alert("Please attach the Letter File to proceed further.");

                return false;

            }

 

I know 'gel' isn't supported by Service Portal so i did search in the community and found one solution to create a UI Script and call it in the catalog client script, but this also isn't working.

UI Script:

API Name: GlobalCatalogItemFunctions

function getSCAttachmentCount() {

 

  var length;

 

  try {

 

  length = angular.element("Copies/Reprints Request").scope().attachments.length;

 

  } catch(e) {

 

  length = -1;

 

  }

 

  return length;

 

}

 

Catalog Client Script

if (g_form.getValue('sbx_crr_requestType') == 'Manual Letter Generation for Address correction Requests') {

 

        try {

            var cat_id_return = gel('sysparm_item_guid').value;

            //alert('Happy '+cat_id_return);

            var gr = new GlideRecord("sys_attachment");

            gr.addQuery("table_name", "sc_cart_item");

            gr.addQuery("table_sys_id", cat_id_return);

            gr.query();

            if (!gr.next()) {

                alert("Please attach the Letter File to proceed further.");

                return false;

            }

 

 

        } catch (e) { //For Service Portal

 

            var count = getSCAttachmentCount();

 

            if (count <= 0) {

 

                alert('Please attach the Letter File to proceed further.');

 

                return false;

 

            }

 

        }

    }

 


Can someone please let me know how to achieve this solution.

 

Thanks,

Rooma

1 ACCEPTED SOLUTION

Shruti
Mega Sage
Mega Sage
function onSubmit() {

    if (g_form.getValue('sbx_crr_requestType') == 'Manual Letter Generation for Address correction Requests') {

        try {

            var cat_id_return = g_form.getParameter("sysparm_item_guid");

            var gr = new GlideRecord("sys_attachment");

            gr.addQuery("table_name", "sc_cart_item");

            gr.addQuery("table_sys_id", cat_id_return);

            gr.query();

            if (!gr.next()) {
                alert("Attachment is mandatory");
                return false;
            }

        } catch (e) {

            if (this.document.getElementsByClassName('get-attachment').length == 0) {
                g_form.addErrorMessage(getMessage('Attachment is mandatory'));
                return false;

            }

        }
    }


}

View solution in original post

7 REPLIES 7

@roomawakar 

Any update to this?

If my response helped please mark it correct as well so that it benefits future readers.

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

@roomawakar 

Using GlideRecord is not recommended in client script.

It will be flagged in health scan report of the instance.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@roomawakar 

As per new community feature you can mark multiple responses as correct.

I provided the correct solution as well to your question.

If my response helped please mark it correct as well so that it benefits future readers.

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