why in onSubmit() client script, Return false is not working in Service Portal?

Anand Shukla
Mega Guru

Hi Experts,

I am trying to validate some fields using glide ajax but return false is not working  alert is working.

Could you please help me with this?

After validation alert is showing but form gets submitted.

function onSubmit() {
	
  var ga = new GlideAjax('getUserdetails');
    ga.addParam('sysparm_name', 'getUserInformation');
    ga.addParam('sysparm_user_id', g_form.getValue('caller_name'));
    ga.getXML(parseUserResponse);

    function parseUserResponse(response) {
         var answer = response.responseXML.documentElement.getAttribute("answer");

    if (answer == 'Contractor') {

        if (g_form.getValue('work_in_china_or_russia') != 'No') {

            alert('Please Select all questions without any Error of Contractor form');
            return false;


        }
	}
    }
}
1 ACCEPTED SOLUTION

Hi,

then take this approach

I assume your called name is readonly once it gets auto-populated with logged in user and is not changed.

1) create string variable with name as Employment Type (employment_type) and hide it always using UI policy; keep it at the bottom of the form

2) then in default value of that variable use this to store the employment type

javascript:

var typ;
var user_sys_id = gs.getUserID(); // always logged in user
var user = new GlideRecord('sys_user');
user.addQuery('sys_id',user_sys_id);
user.query();
if (user.next()) {
typ = user.u_employment_type;
}
typ;

3) use this in your onSubmit and compare value stored in hidden variable

function onSubmit() {

	var answer = g_form.getValue('employment_type');
	if (answer == 'Contractor') {
		if (g_form.getValue('work_in_china_or_russia') != 'No') {
			alert('Please Select all questions without any Error of Contractor form');
			return false;
		}
	}
}

Regards
Ankur

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

View solution in original post

11 REPLIES 11

caller name should be the logged in user, so that should not change.

is there any other way by which i can validate the sys_user field and restrict the form submission.

 

Hi,

can you share what that script include function checks?

Regards
Ankur

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

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

getUserInformation: function() {
var user_sys_id = this.getParameter('sysparm_user_id');
var user = new GlideRecord('sys_user');
user.addQuery('sys_id',user_sys_id);
user.query();
if (user.next()) {
var typ = user.u_employment_type;
return typ;
}
},

type: 'getUserdetails'
});

Hi,

then take this approach

I assume your called name is readonly once it gets auto-populated with logged in user and is not changed.

1) create string variable with name as Employment Type (employment_type) and hide it always using UI policy; keep it at the bottom of the form

2) then in default value of that variable use this to store the employment type

javascript:

var typ;
var user_sys_id = gs.getUserID(); // always logged in user
var user = new GlideRecord('sys_user');
user.addQuery('sys_id',user_sys_id);
user.query();
if (user.next()) {
typ = user.u_employment_type;
}
typ;

3) use this in your onSubmit and compare value stored in hidden variable

function onSubmit() {

	var answer = g_form.getValue('employment_type');
	if (answer == 'Contractor') {
		if (g_form.getValue('work_in_china_or_russia') != 'No') {
			alert('Please Select all questions without any Error of Contractor form');
			return false;
		}
	}
}

Regards
Ankur

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

Thank you so much Ankur. Now it is working fine.