Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Attachment mandatory on task and RITM based on Yes/No Variable

Dileep Raju
Giga Guru

Hi Team
I'm trying to make attachments mandatory on a SC task and Form . I used a catalog client script but it is not working as expected. Can Some one help me Please?

function onSubmit() {
  if ((g_form.getValue('test') == 'Yes') && (document.getElementsByClassName('get-attachment').length == 0)) {
  alert("Attachment is mandatory when test is marked as 'Yes.'");
  return false;
}
   
}

 

1 ACCEPTED SOLUTION

Rajat Gupta6
Tera Guru

function onSubmit() {

if(g_form.getValue('test') == 'Yes'){

      var sys_id = g_form.getUniqueValue(); // gets sys_id of catalog task

      var attachment = new GlideRecord('sys_attachment');

      attachment.addQuery('table_name','sc_task');

      attachment.addQuery('table_sys_id',sys_id);

      attachment.query();

      if (!attachment.next()) {

              alert("Please add the attachment");

              return false;

      }

}

    return true;

}

 

Please accept my solution if it works for you and thumps up.

View solution in original post

3 REPLIES 3

Rajat Gupta6
Tera Guru

function onSubmit() {

if(g_form.getValue('test') == 'Yes'){

      var sys_id = g_form.getUniqueValue(); // gets sys_id of catalog task

      var attachment = new GlideRecord('sys_attachment');

      attachment.addQuery('table_name','sc_task');

      attachment.addQuery('table_sys_id',sys_id);

      attachment.query();

      if (!attachment.next()) {

              alert("Please add the attachment");

              return false;

      }

}

    return true;

}

 

Please accept my solution if it works for you and thumps up.

folusho
Tera Guru

@Dileep Raju 

Please try this:

 

1. Create a UI script called: "setAttachmentMandatory". Set UI Type to "All".

Put this in the script section.

(function() {
    "use strict";

    return {

        setAttachmentMandatory: function(isMandatory) {
            var isSuccess;
            try {
                angular.element("#sc_cat_item").scope().c.data.sc_cat_item.mandatory_attachment = isMandatory;
                isSuccess = true;
            } catch (e) {
                isSuccess = false;
                //gs.log('setAttachmentMandatory() failed: ' + e);
            }

            return isSuccess;
        },

        type: "AttachmentUtil"
    };
})();

 

2. Then in your catalog item, change the onSubmit catalog Client script to onChange... and add this script:

Make sure to check the "isolate script" and set UI Type to "All"

if (g_form.getValue('test') == 'Yes') {
    g_ui_scripts.getUIScript('setAttachmentMandatory').then(function (script) {
        script.setAttachmentMandatory(true);
    });
} else {
    g_ui_scripts.getUIScript('setAttachmentMandatory').then(function (script) {
        script.setAttachmentMandatory(false);
    });
}

 

Please mark as helpful if it works for you. Thanks

maheshkhatal
Mega Sage
Mega Sage

Hello @Dileep Raju ,

Create a script include and have it called from the client script:

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

    checkAttachment: function() {
        var recordID = this.getParameter('sysparm_id');
        var gr = new GlideRecord("sys_attachment");
        gr.addQuery('table_name', 'sc_req_item');
        gr.addQuery('table_sys_id', recordID);
        gr.query();

        if (gr.next()) {
            return 'yes';
        } else {
            return 'no';
        }
    },

    type: 'CheckAttachmentCatalogItem'
});

 

 

Your client script to make attachments mandatory based on the script include response will be:

 

function onSubmit() {
    var testValue = g_form.getValue('test'); // Replace 'test' with your variable name

    if (testValue == 'Yes') {
        var ga = new GlideAjax('CheckAttachmentCatalogItem');
        ga.addParam('sysparm_name', 'checkAttachment');
        ga.addParam('sysparm_id', g_form.getUniqueValue()); // Get the RITM sys_id

        var hasAttachment = ga.getXMLWait();

        var result = ga.getAnswer();
        
        if (result == 'no') {
            g_form.addErrorMessage("Attachment is mandatory when 'test' is marked as 'Yes'.");
            return false;
        }
    }

    return true;
}

Mark this response as helpful if it resolved your doubt.

Thank you,

Mahesh.