Not able to submit the catalog form in service portal when the if condition does not match

rajtiwari02
Tera Contributor
function onSubmit() {
    var onBehalf = g_form.getValue('u_raising_on_behalf_of');
    var reqMail = g_form.getValue('u_requested_for');
 
    alert('Step 1: onBehalf Value - ' + onBehalf);
    alert('Step 2: reqMail Value - ' + reqMail);
 
    if (onBehalf === 'u_others') {
        alert('Step 3: Inside "on behalf of others" condition');
 
        var ga = new GlideAjax('Magnum_Kyriba_Catalog');
        ga.addParam('sysparm_name', 'getUserName');
 
        // Use Asynchronous Call & Prevent Submission
        ga.getXMLAnswer(function(response) {
            var loggedInUserName = response;
            alert('Step 3.1: Current Logged-in User Name - ' + loggedInUserName);
 
            if (reqMail && loggedInUserName && loggedInUserName === reqMail.trim()) {
                g_form.addErrorMessage('Please enter a different email ID.');
                alert('Step 4: User Name matches requested user - Submission Blocked');
            } else {
                alert('Step 5: User Name does not match, proceeding with submission');
               
                g_form.submit();
 
                //return true;
            }
        });
      
        return false; // Stop Immediate Submission Until Validation Completes
    }
 
    return true; // Allow submission if "on behalf of" is NOT "others"
}

<<<-------------------------------------------------------->>>>>

Hi collogues,
I have written a catalog onSubmit() client script as above to validate the field Requested for "u_requested_for" (type = single line). If the logged in user enters his own userid and select the choice from the field Raising on behalf ("u_raising_on_behalf_of") (type= select box) the field as others("u_others") then the submission of the form should be stopped with errormessage but if any one of the conditions fails, then the form should be submitted. 

Here the validation part is working fine (Submission is prevented) but when it goes to else part (step5) instead of allowing the submission only once, it's going into infinite loop and again starting from step 1. Any one faced similar issue . Thanks in advance .

attaching the snip of the script to avoid any confusion 




2 ACCEPTED SOLUTIONS

Nick Parsons
Mega Sage

Calling g_form.submit(); submits your form again so the onSubmit() handler will run again. You need some sort of persistent variable that you can set and then read within your handler to understand if the form has been validated or not, and then return true to continue normal form submission from your onSubmit handler if it's been set.

 

First, update your callback to set a property on g_scratchpad (this is the persistent property that we can read on the next execution of your onSubmit to prevent an infinite loop):

// important: getActionName() must be called synchronously  (not within the callback)
var actionName = g_form.getActionName(); 
ga.getXMLAnswer(function(response) {
  ...
  else {
    alert('Step 5: User Name does not match, proceeding with submission');
    // add a property to g_scratchpad so that we can read it when `onSubmit()` fires again
    g_scratchpad.formValidated = true;
    g_form.submit(actionName);
  }
});

 

Then at the top of your script, prevent the infinite loop and return true to continue with normal form submission if the formValidated property we set within the callback is set:

function onSubmit() {
  if (g_scratchpad.formValidated) // continue with normal form submission if form has been validated
    return true;
  // ... rest of your code ...
}

This is also the approached suggested by ServiceNow in KB0783579 - How to do async validation in an onsubmit client script. for dealing with asynchronous form validation.   

View solution in original post

Hello @rajtiwari02 ,

 

Your script is using "g_user.getUserName()" but there is no such method. That's why it is not working.

You need to use "g_user.userName".

 

Regards,

Robert

View solution in original post

9 REPLIES 9

James Chun
Kilo Patron

Hi @rajtiwari02,

 

I believe there is no need to have g_form.submit(); in the script as it's already being submitted.

That's probably why it's getting stuck in a loop.

 

Also, please note that you can't prevent submission with AJAX in onSubmit Client script.

You will need to modify your script, refer to this article - How To: Async GlideAjax in an onSubmit script - ServiceNow Community

 

Cheers

Nick Parsons
Mega Sage

Calling g_form.submit(); submits your form again so the onSubmit() handler will run again. You need some sort of persistent variable that you can set and then read within your handler to understand if the form has been validated or not, and then return true to continue normal form submission from your onSubmit handler if it's been set.

 

First, update your callback to set a property on g_scratchpad (this is the persistent property that we can read on the next execution of your onSubmit to prevent an infinite loop):

// important: getActionName() must be called synchronously  (not within the callback)
var actionName = g_form.getActionName(); 
ga.getXMLAnswer(function(response) {
  ...
  else {
    alert('Step 5: User Name does not match, proceeding with submission');
    // add a property to g_scratchpad so that we can read it when `onSubmit()` fires again
    g_scratchpad.formValidated = true;
    g_form.submit(actionName);
  }
});

 

Then at the top of your script, prevent the infinite loop and return true to continue with normal form submission if the formValidated property we set within the callback is set:

function onSubmit() {
  if (g_scratchpad.formValidated) // continue with normal form submission if form has been validated
    return true;
  // ... rest of your code ...
}

This is also the approached suggested by ServiceNow in KB0783579 - How to do async validation in an onsubmit client script. for dealing with asynchronous form validation.   

Robert H
Mega Sage

Hello @rajtiwari02 ,

 

You don't need an AJAX call and a Script Include to get the current user's user ID (user_name).

Just use "g_user.userName".

 

function onSubmit() {
	var onBehalf = g_form.getValue('u_raising_on_behalf_of');
	var reqMail = g_form.getValue('u_requested_for');

	alert('Step 1: onBehalf Value - ' + onBehalf);
	alert('Step 2: reqMail Value - ' + reqMail);

	if (onBehalf === 'u_others') {
		alert('Step 3: Inside "on behalf of others" condition');

		var loggedInUserName = g_user.userName;
		alert('Step 3.1: Current Logged-in User Name - ' + loggedInUserName);

		if (reqMail && loggedInUserName && loggedInUserName === reqMail.trim()) {
			g_form.addErrorMessage('Please enter a different email ID.');
			alert('Step 4: User Name matches requested user - Submission Blocked');
			return false;
		} else {
			alert('Step 5: User Name does not match, proceeding with submission');
		}
	}
}

 

Regards,

Robert

Hello @rajtiwari02 ,

 

What's the concern with this much simpler solution?

 

Regards,

Robert