attachment check on UI action

Lean S
Mega Expert

Hi,

How can we make attachment mandatory if not attached to form on clicking UI action. Basically Before any action after clicking button, it has to check attachment and a field value should be filled, if not i has to alert user and stop UI action functionality going further. Kindly help with this scenario.

 

Thanks,

Lean S

13 REPLIES 13

Hi @Lean S ,

 

You can use the same script in your custom UI action.

Select "Client" checkbox on the UI action if not already checked, and then, on the OnClick field, put the function name "checkAttachment" that'll be called, and in the script section, wrap the script with the same function name i.e. "checkAttachment" that you're calling OnClick.

 

function checkAttachment() {
    //get your ref_field value
    var ref_field = g_form.getValue('your_ref_field_name');
	
    var attachments = new GlideRecord("sys_attachment");
    attachments.addQuery("table_name", "your_table_name");
    attachments.addQuery("table_sys_id", g_form.getUniqueValue());
    attachments.query();

    //if ref_field is empty and no attachments are there alert user
    if (!ref_field && !attachments.hasNext()) {
        g_form.addErrorMessage("Please add an attachment before submitting.");
        return false;
    }
}

Refer to the screenshot below:

 

find_real_file.png

 

Thanks & Regards,

Rishabh Jha

Aavenir (https://www.aavenir.com/)

Hi @Lean S 

 

Another simple way would be to use the same onSubmit client script, and if the button clicked is your UI action, and then only do the attachment check. Something like below:

function onSubmit() {

    var action = g_form.getActionName();
    
    if(action == 'your_action_name') {
      //get your ref_field value
      var ref_field = g_form.getValue('your_ref_field_name');
	
      var attachments = new GlideRecord("sys_attachment");
      attachments.addQuery("table_name", "your_table_name");
      attachments.addQuery("table_sys_id", g_form.getUniqueValue());
      attachments.query();

      //if ref_field is empty and no attachments are there alert user
      if (!ref_field && !attachments.hasNext()) {
        g_form.addErrorMessage("Please add an attachment before submitting.");
        return false;
      }
   }

}

 

Thanks & Regards,

Rishabh Jha

Aavenir (https://www.aavenir.com/)

@Lean S

As best practice it is not recommended to use GlideRecord in client side scripting.

You can use GlideAjax and Script Include to determine if attachment is present or not.

It should work for any UI action.

Please find the updated UI action code and Script Include

It would check if attachment is present and referTo field is present then it would pass control to server side

UI Action:

Onclick: checkCondition()

Script:

function checkCondition(){

    var ga = new GlideAjax('CheckAttachment');
    ga.addParam('sysparm_name', "check");
    ga.addParam('sysparm_sysId', g_form.getUniqueValue());
    ga.getXMLAnswer(function(answer){

    if(answer.toString() == 'true' && g_form.getValue('referTo_feld') != ''){
    // if attachment present then pass control to server side
    gsftSubmit(null, g_form.getFormElement(), '<button_action_name>');
    // give the ui action name in above line
   
    }
    return false;

    });

}

if(typeof window == 'undefined')
   runServerSideCode();

function runServerSideCode(){

// your server side code here

}

Script Include: It should be client-callable

var CheckAttachment = Class.create();

CheckAttachment.prototype = Object.extendsObject(AbstractAjaxProcessor, {

check: function () {

var attachmentRec = new GlideRecord("sys_attachment");
attachmentRec.addQuery("table_name", "your_table_name");
attachmentRec.addQuery("table_sys_id", this.getParameter('sysparm_sysId'));
attachmentRec.query();

return attachmentRec.hasNext();
},

type: 'CheckAttachment'

});

Regards
Ankur

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

manjusha
Kilo Guru

hi,

use below code to check attachment attached or not 

function onSubmit() {

 

  var attach= new GlideRecord("sys_attachment");

 

  attach.addQuery("table_name", "incident");

 

    attach.query();

 

  if (!attach.next()) {

 

      alert("attachment missing.");

       return false;
  }
   return true;
}
Thanks,
Manjusha Bangale