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

rajtiwari02
Tera Contributor

Hi @Robert H , thanks for the solution but I am using Service portal for the form submission even after trying that only I posted the concern on community. Thanks all for your help.

Hello @rajtiwari02 ,

 

I don't understand. g_form.userName works in Service Portal as well.

 

RobertH_2-1748615175778.png

 

I'm not saying that the other solutions are incorrect. But for your particular requirement using AJAX and Script Includes just to get the user ID is unnecessarily complex.

 

Regards,

Robert

 

 

Hi @Robert H , I understood your point but the script you provided there "g_user" has been used. Your recent output is correct with g_form.username. Please refer below my previous code which was not working as I was using "g_user" API  same as you have used above in service portal client script.

 

 

 

function onSubmit() {
    // Retrieve values from the form
    var onBehalf = g_form.getValue('u_raising_on_behalf_of');
    var reqMail = g_form.getValue('u_requested_for');
 
    // Debugging alerts
    alert('Step 1: onBehalf Value - ' + onBehalf);
    alert('Step 2: reqMail Value - ' + reqMail);
 
    // Check if 'on behalf of' is set to 'others' and validate requested user
    if (onBehalf === 'u_others') {
        alert('Step 3: Inside "on behalf of others" condition');
        alert('Step 3.1: Current Logged-in User ID - ' + g_user.getUserName());
 
        // Ensure reqMail is properly retrieved and compared
        if (reqMail && g_user.getUserName() && g_user.getUserName() === reqMail.trim()) {
            alert('Step 4: User ID matches requested user - Submission Blocked');
g_form.addErrorMessage('Please provide Different Email');
 
            return false; // Prevent form submission
        } else {
            alert('Step 5: User ID does not match, proceeding with submission');
        }
    } else {
        alert('Step 6: onBehalf is NOT "others", proceeding with submission');
    }
 
    return true; // Allow form submission if no blocking condition is met
}
 

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

Hi @Robert H 

 

Thanks for your help. Yes, it's working. Actually, I had limited time, so I tried the random solution. But yes, its correct.  thank you all, have a great day.