on submit catalog client script

Ravivarman Saee
Tera Contributor

i need the requestor to be from certain departments for a catalog item in service portal. if not they shouldnt be able to submit it. using below catalog client script and script include. it terminates the users from non wanted departments correctly but not allowing the member of that department to submit. rather it creates an endless loop. why ?any idea ?

client script 

 

function onSubmit() {
    // Prevent the form from submitting immediately
    var isSubmitAllowed = false;

    var ga = new GlideAjax('ValidateRequesterDepartment');
    ga.addParam('sysparm_name', 'validateRequester');
    ga.addParam('sys_id', g_form.getValue('who_needs_assistance'));


    ga.getXML(function(response) {
        processResponse(response);
    });

    // Prevent the form from submitting until the asynchronous check is done
    return false;

    function processResponse(validateRequester) {
        if (validateRequester === 'false') {
            alert('You are not authorized to submit this request');
            g_form.submitted = false;
        } else {
            alert('can submit');
            g_form.submitted = true;
            g_form.submit();
        }
    }
}

 

script include 

 

var ValidateRequesterDepartment = Class.create();
ValidateRequesterDepartment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    validateRequester: function() {
        var userSysId = this.getParameter('sys_id');
        var response = 'false';
        var user = new GlideRecord('sys_user');

        if (user.get(userSysId)) {
            var department = user.getValue('department');
            if (department == 'fd8ab044db7b6010a475d90ed39619d4' || department == '959d212adbc41c948a3cf0eb0c9619d5') {
                response = 'true';
            }
        }

        return response;
    }
});

 

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

Perhaps the return false is in the wrong spot?  Check this example for reference

https://www.servicenow.com/community/developer-blog/how-to-async-glideajax-in-an-onsubmit-script/ba-... 

swathisarang98
Giga Sage
Giga Sage

Hi @Ravivarman Saee ,

 

In your script the answer is always going to else part and g_form.submit() is happening in loop and its calling the script include again and again ,

 

and this line there is syntax error,

 

ga.addParam('sys_id', g_form.getValue('who_needs_assistance'));

 

it should be

 

ga.addParam('sysparm_id', g_form.getValue('who_needs_assistance'));

 

 

I have modified your code and tried it in pdi its working please do give it a try,

Client script:

 

function onSubmit() {
    if (g_scratchpad.isFormValid)
        return true;
    var user = g_form.getValue('requested_for');
    var ga = new GlideAjax('ValidateRequesterDepartment');
    ga.addParam('sysparm_name', 'validateRequester');
    ga.addParam('sysparm_id', user);
    ga.getXMLAnswer(setAnswer);
    return false;


    function setAnswer(answer) {
        if (answer === 'false' || answer == false) {
            alert('You are not authorized to submit this request');
            return false;
        } else {
            var actionName = g_form.getActionName();
            g_scratchpad.isFormValid = true;
            g_form.submit(actionName);
        }


    }
}

 

 

Script include:

 

var ValidateRequesterDepartment = Class.create();
ValidateRequesterDepartment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	validateRequester: function() {
        var userSysId = this.getParameter('sysparm_id');
		gs.info('sysid ' + userSysId);
        // var response = 'false';
        var user = new GlideRecord('sys_user');

        if (user.get(userSysId)) {
            var department = user.getValue('department');
			gs.info('department' + user.getDisplayValue('department'))
            if (department == '9a7ed3f03710200044e0bfc8bcbe5db7' || department == '5d7f17f03710200044e0bfc8bcbe5d43') {
                return true;
            }
			else{ 
				return false;
			}
        }

    },

    type: 'ValidateRequesterDepartment'
});

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang