attachment validation using UI Action

Somujit1
Tera Contributor

Hello Experts,

 

I have an existing UI Action which sets some alerts and mandatory field validations on click on the button. In addition to the existing behaviour, I need the UI action to validate if attachments are attached to the record while submit.

 

I have created a script include to check it attachments are associated to the record and have update the UI Action as below. 

 

On Submit, if the attachment is not available, the alert shows and the record does not get submitted.

 

However, it is not doing the all the other validations as mentioned in the UI Action scripts and also doesnot submit the record if attachment is found.

 

Can you please guide me with the correct script to achieve the attachment validations, and also keeping the other existing script validations unimpacted

 

Script Include -

var SysAttachmentUtil = Class.create();
SysAttachmentUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    attachmentValidation: function() {
        var checkListId = this.getParameter('sysparm_id');
        var attachmentID = new GlideRecord('sys_attachment');
        attachmentID.addQuery('table_name','u_check_list');
        attachmentID.addQuery('table_sys_id', checkListId);
        attachmentID.query();
        if (!attachmentID.next()) {
            answer = 'no associated attachment';
        }
        return answer;
    },
    type: 'SysAttachmentUtil'
});

 

UI Action -

Action Name - u_submit

Client - checked

onClick - validateOpenToSubmit()

Script -

function validateOpenToSubmit() {
    var checklistID = g_form.getUniqueValue();
    var testingAttach = g_form.getValue('u_testing_evidence_available');
    var ccl_co = g_form.getValue('u_owner_comments');
    var st = g_form.getValue('state');
    if (testingAttach == '0') {
        var attachmentID = new GlideAjax("SysAttachmentUtil");
        attachmentID.addParam("sysparm_name", "attachmentValidation");
        attachmentID.addParam("sysparm_id", checklistID);
        attachmentID.getXMLAnswer(function(answer) {
            if (answer == 'no associated attachment') {
                alert("Please attach the testing plan before submitting the task"); //Attachment is mandatory while submitting the task 
return false;
            }
        });
return false;
    }
    if (st == 32 && ccl_co == '') {
        alert("Please provide justification in Owner comments field before submitting the task");
        g_form.setMandatory('u_owner_comments', true);
        return false;
    }
    g_form.setValue('state', 34);
    gsftSubmit(null, g_form.getFormElement(), 'u_submit');
    g_form.setValue('state', 0);
}
 
if (typeof window == 'undefined') {
    runServerOpenToSubmit();
}
 
function runServerOpenToSubmit() {
     new global.StateFlow().processFlow(current, 'sys_id', 'manual');
        action.setRedirectURL('/change_request.do?sys_id=' + current.parent.sys_id);
}

 

Thanks

3 REPLIES 3

Ankur Bawiskar
Tera Patron

@Somujit1 

your GlideAjax is Asynchronous so it doesn't wait for result

use getXMLWait() for synchronous call

function validateOpenToSubmit() {
    var checklistID = g_form.getUniqueValue();
    var testingAttach = g_form.getValue('u_testing_evidence_available');
    var ccl_co = g_form.getValue('u_owner_comments');
    var st = g_form.getValue('state');
    
    // Attachment check only if u_testing_evidence_available is 0
    if (testingAttach == '0') {
        var attachmentID = new GlideAjax("SysAttachmentUtil");
        attachmentID.addParam("sysparm_name", "attachmentValidation");
        attachmentID.addParam("sysparm_id", checklistID);
        var answer = attachmentID.getXMLWait();  // Synchronous: waits for response
        if (answer == 'no associated attachment') {
            alert("Please attach the testing plan before submitting the task");
            return false;
        }
    }
    
    // Other validations
    if (st == 32 && ccl_co == '') {
        alert("Please provide justification in Owner comments field before submitting the task");
        g_form.setMandatory('u_owner_comments', true);
        return false;
    }
    
    // Submit if all pass
    g_form.setValue('state', 34);
    gsftSubmit(null, g_form.getFormElement(), 'u_submit');
    g_form.setValue('state', 0);
}

if (typeof window == 'undefined') {
    runServerOpenToSubmit();
}

function runServerOpenToSubmit() {
    new global.StateFlow().processFlow(current, 'sys_id', 'manual');
    action.setRedirectURL('/change_request.do?sys_id=' + current.parent.sys_id);
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Hi @Ankur Bawiskar ,

 

I updated the UI action script to use the get XMLWait, as provided by you. It doesnot show the alert message and also proceeds with submitting the record.

Please advice on the way forward to correct the script.

 

Thanks

Tanushree Maiti
Mega Sage

Hi @Somujit1 

In your UI action-
 
Uncheck the client checkbox
Remove the onClick function
Script:
var attachChk = new GlideRecord("sys_attachment");
attachChk.addQuery("table_name", current.getTableName());
attachChk.addQuery('table_sys_id', current.getUniqueValue());
attachChk.addEncodedQuery('content_typeSTARTSWITHimage');
attachChk.query();
if(!attachChk.next()) {
gs.addErrorMessage("Please attach the testing plan before submitting the task");
current.setAbortAction(true);
}else{
current.state = 'completed';
current.update();
}
 
 
 
 
You can also write before insert and update business rule to check mandatory attachment.
 
Before insert BR :
 
var attachChk = new GlideRecord("sys_attachment");
attachChk.addQuery("table_name", current.getTableName());
attachChk.addQuery("table_sys_id", current.sys_id);
attachChk.query();
if (!attachChk.next()) {
      gs.addErrorMessage("Please attach the testing plan before submitting the task");
      current.setAbortAction(true);
}
Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: