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

Robbie
Kilo Patron
Kilo Patron

Hi @roomawakar,

 

Have you seen the article that @AbhishekGardade has provideed covering a number of use cases and scenarios - Kudos. I'd steer you towards this as a starting point.

 

https://www.servicenow.com/community/developer-articles/possible-ways-for-making-an-attachment-manda...

 

To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.




Thanks, Robbie

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;

            }

        }
    }


}

Ankur Bawiskar
Tera Patron
Tera Patron

@roomawakar 

this will work in both the UI Type

function onSubmit() {
    //Type appropriate comment here, and begin script below
    try {
        if (window == null && g_form.getValue('sbx_crr_requestType') == 'Manual Letter Generation for Address correction Requests') {
            // portal
            if (this.document.getElementsByClassName('get-attachment').length != 1) {
                alert('You must attach the completed form before submitting this request.');
                return false;
            }
        }
    } catch (ex) {
        // native view
        if (g_form.getValue('sbx_crr_requestType') == 'Manual Letter Generation for Address correction Requests') {
            var length = getCurrentAttachmentNumber();
            if (length != 1) {
                alert('You must attach the completed form before submitting this request.');
                return false;
            }
        }
    }
}

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 

Thank you for marking my response as helpful.

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