Client Script On Submit with Ajax

Alex150
Mega Sage

Hello community,

I'm wondering if there is the best practise to 'Reject/Abort' submit action on the Client side with GlideAjax. The main issue is observing when we have to validate smth on Submit action.

 

- We can't using methods the "getXmlWait()" anymore due to is not currently supported and does't work in Workspace/Portal. Previously it was working solution and there weren't any issues with it, but this method was deprecated.

 

- We can't using the getXML(function) method as we need to return "false" to abort submit action. Script runs function in the end of processing. All out of function will be processed first and it is not possible to make "return false". 

 

-Adding hidden fields/checkboxes etc to populate them to define if conditions met on change. I think this solution sound as "hardcode" and I avoid of using such approach and believe there is a more elegant solution.

 

- In native interface we can exchange the Submit Client Script with BR and g_scratchpad, but I was not able to find a nice solution for Service Catalog/Portal.

 

So, any ideas regarding this case?

3 REPLIES 3

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Alex150 ,

 

ServiceNow has provided support KB article for that. Refer below article:-

How to do async validation in an onsubmit client script. 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Hello,

Thanks for reply.

function onSubmit() {
if (g_scratchpad.isFormValid){
	return true;
}
var actionName = g_form.getActionName();
var ga = new GlideAjax("SOMEFUNCTION");
ga.addParam(.....);
ga.getXML(function() {
	g_scratchpad.isFormValid = true;
	g_form.submit(actionName);
});
	return false;
}

 

I tried this solution, it didn't help me. As far as I understand for script enough time to call "return false" before the Ajax script will processed and return the response.

scott barnard1
Kilo Sage

Hi Alex

It depends on how much validation that you are doing. If it's a simple lookup check I tend to use an on load client script to populate the scratchpad via session storage.

 

so onload script example


@Alex150 wrote:

Hello community,

I'm wondering if there is the best practise to 'Reject/Abort' submit action on the Client side with GlideAjax. The main issue is observing when we have to validate smth on Submit action.

 

- We can't using methods the "getXmlWait()" anymore due to is not currently supported and does't work in Workspace/Portal. Previously it was working solution and there weren't any issues with it, but this method was deprecated.

 

- We can't using the getXML(function) method as we need to return "false" to abort submit action. Script runs function in the end of processing. All out of function will be processed first and it is not possible to make "return false". 

 

-Adding hidden fields/checkboxes etc to populate them to define if conditions met on change. I think this solution sound as "hardcode" and I avoid of using such approach and believe there is a more elegant solution.

 

- In native interface we can exchange the Submit Client Script with BR and g_scratchpad, but I was not able to find a nice solution for Service Catalog/Portal.

 

So, any ideas regarding this case?


function onLoad() {
   //Load the message record and pass it into session storage to access on submit
getMessage('TC1', function(msg){
		sessionStorage.message = msg;
	});
	
   
}

//In this case TC1 is the terms and conditions message on the message table

 

Then in the on submit script

 

 

    function onSubmit() {
     var message = sessionStorage.message;     // this picks up the data we put into the session storage in the on load
    getMessage('TC1', function(msg) {
      
    });
    if (typeof spModal != 'undefined' && !g_form._hasConfirmed) {

        spModal.open({
            message: 'T&C1' + message,
            title: 'Agree to Terms & Conditions',
            buttons: [{
                    label: '✘ Disagree',
                    cancel: true
                },
                {
                    label: '✔ I Accept',
                    primary: true
                }
            ]
        }).then(function(confirm) {
            if (confirm) {
                g_form._hasConfirmed = true;
                if (typeof g_form.orderNow != 'undefined') {
                    g_form.orderNow();
                } else {
                    g_form.submit();
                }
            }
        });

        return false;
    }
}

For simple validation it works fine, you don't need to bother with the modal etc. However if you are doing lots of queries then it will slow down client response