Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Onsubmit client script not validating attachment

EricG2
Tera Guru

I'm attempting to validate that a specific attachment is included on a cat_item before it can be submitted.

 

Script:

EricG2_1-1789735475099.png

 

function onSubmit() {
    var curUsr = g_user.userID;
	var mgr = g_form.getValue('requested_manager');

	if(curUsr!=mgr){
	
	var ga = new GlideAjax('AttachmentUtils2');
    ga.addParam('sysparm_name', 'hasRequiredAttachment');
    ga.addParam('sysparm_sys_id',  g_form.getValue('sysparm_item_guid'));
// tried using g_form.getUniqueValue()), but this didn't return the corred sys_id for the attachments record.
    ga.addParam('sysparm_search', 'Manager Approval');
    
    ga.getXMLWait();
    var answer = ga.getAnswer();

    if (answer !== 'true') {
        g_form.addErrorMessage('You must attach a file containing "Manager Approval" in the file name before submitting or have them submit this form.');
        return false; // Aborts form submission
	    }
	
}

 

Script Include 

EricG2_0-1789735418953.png

 

var recordId = this.getParameter('sysparm_sys_id');
        var searchTerm = this.getParameter('sysparm_search');
        gs.info('Eric - the record it is '+recordId);
		
        var gr = new GlideRecord('sys_attachment');
        gr.addQuery('table_sys_id', recordId);
        gr.query();
        
        while (gr.next()) {
            var fileName = gr.getValue('file_name') || '';
			gs.log('Eric - the record it is '+recordId);
			gs.log('Eric - the attahced file name is '+gr.file_name);
            if (fileName.toLowerCase().indexOf(searchTerm.toLowerCase()) !== -1) {
                return 'true';
            }
        }
        return 'false';
 },

    type: 'AttachmentUtils'
});

 This doesn't appear to be working.  
I've tried adding in gs messages and nothing appears, nor am i getting any error messages.

So I'm not sure it's even being called.

 

Suggestions?

1 ACCEPTED SOLUTION

EricG2
Tera Guru

Well - copilot's a wonderful thing.

I redid the Client script and Script include.  It's working now.

 

Client Script

EricG2_0-1789756507444.png

 

unction onSubmit() {
	var mgrSubmit = g_form.getValue(g_form.setValue('submitted_by_manager');

	if(mgrSubmit==true){
    var ga = new GlideAjax('AttachmentValidator');
	ga.addParam('sysparm_name', 'hasManagerApproval');
	ga.addParam('sysparm_table', 'sc_cart_item');//g_form.getTableName());
	ga.addParam('sysparm_sys_id', g_form.getValue('sysparm_item_guid'));
	
	var answer = ga.getXMLWait();
	var result = answer.documentElement.getAttribute("answer");	
	g_form.setValue('submitted_with_manager_approval',true);
	if (result !== 'true') {
		g_form.addErrorMessage('You need to attach an approval email from your manager before you can submit this request.  Make sure the name of the file includes "Manager Approval".');
		return false;
		}
	return true;
	

}}

 

Script Include

EricG2_1-1789757952126.png

 

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

    hasManagerApproval: function() {
        var gr = new GlideRecord('sys_attachment');
        gr.addQuery('table_name', this.getParameter('sysparm_table'));
        gr.addQuery('table_sys_id', this.getParameter('sysparm_sys_id'));
        gr.query();

        while (gr.next()) {
            if (gr.file_name.toString().toLowerCase().indexOf('manager approval') > -1) {

                return true;
            }
        }

        return false;
    },

    type: 'AttachmentValidator'
});

 

Not sure what the difference is, but it's working.

 

View solution in original post

6 REPLIES 6

PradeepReddyA
Tera Guru

Hi @EricG2 

 

I think the issue is due to usage of the getXMLWait() as this doesn't work in service portal. Please refer to these link.

PradeepReddyA_0-1789744814493.png

 

getXMLWait alternative

 

Hope that Helps!

EricG2
Tera Guru

Well - copilot's a wonderful thing.

I redid the Client script and Script include.  It's working now.

 

Client Script

EricG2_0-1789756507444.png

 

unction onSubmit() {
	var mgrSubmit = g_form.getValue(g_form.setValue('submitted_by_manager');

	if(mgrSubmit==true){
    var ga = new GlideAjax('AttachmentValidator');
	ga.addParam('sysparm_name', 'hasManagerApproval');
	ga.addParam('sysparm_table', 'sc_cart_item');//g_form.getTableName());
	ga.addParam('sysparm_sys_id', g_form.getValue('sysparm_item_guid'));
	
	var answer = ga.getXMLWait();
	var result = answer.documentElement.getAttribute("answer");	
	g_form.setValue('submitted_with_manager_approval',true);
	if (result !== 'true') {
		g_form.addErrorMessage('You need to attach an approval email from your manager before you can submit this request.  Make sure the name of the file includes "Manager Approval".');
		return false;
		}
	return true;
	

}}

 

Script Include

EricG2_1-1789757952126.png

 

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

    hasManagerApproval: function() {
        var gr = new GlideRecord('sys_attachment');
        gr.addQuery('table_name', this.getParameter('sysparm_table'));
        gr.addQuery('table_sys_id', this.getParameter('sysparm_sys_id'));
        gr.query();

        while (gr.next()) {
            if (gr.file_name.toString().toLowerCase().indexOf('manager approval') > -1) {

                return true;
            }
        }

        return false;
    },

    type: 'AttachmentValidator'
});

 

Not sure what the difference is, but it's working.