Any chance of Auto click Ok (alert) for catalogs client script

String
Kilo Sage

Hi Team ,

Am using the below code in catalogs client script ,we are showing alert in onchange ,but user should not click Alert OK

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
alert('Please wait');//auto click OK 
var test1 = g_form.getValue('check);
var ga = new GlideAjax('Cus'); // Script include
ga.addParam('sysparm_name', 'gettest'); // Method
ga.addParam('consumable', test1);
ga.getXML(getResponse);
}function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('test', answer);
}
 
Please guide me 
7 REPLIES 7

Hi @String 

 

I could think of the below approach :

 

  • Make the Add Button Disabled whenever an asynchronous Ajax call is being made on the "on change" event for Field A. Before making the AJAX call, you can disable the Add button and once you get a response, you can enable it. This way, you could restrict users from clicking the add button until you have value for field B. Refer below posts to make this work :

https://www.servicenow.com/community/developer-articles/disable-buttons-in-multirow-variable-set/ta-...

https://www.servicenow.com/community/developer-forum/i-want-to-disable-or-make-add-button-non-clicka...

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @String , 

To address your requirement, you can introduce a delay without using an alert by utilizing JavaScript's setTimeout() function. This function allows you to delay the execution of a piece of code for a specified amount of time. Here's how you can modify your code to introduce a delay after the user clicks the Add button:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    
    // Assuming 'check' is the field being changed (Field A)
    var test1 = g_form.getValue('check');
    
    // Delay the execution of the AJAX call by 2 seconds (2000 milliseconds)
    setTimeout(function() {
        var ga = new GlideAjax('Cus'); // Script include
        ga.addParam('sysparm_name', 'gettest'); // Method
        ga.addParam('consumable', test1);
        ga.getXML(getResponse);
    }, 2000); // Adjust the delay time as needed
}

function getResponse(response) {
    var answer = response.responseXML.documentElement.getAttribute('answer');
    g_form.setValue('test', answer);
}

 

 

In this code:

  • We use setTimeout() to delay the execution of the AJAX call by 2000 milliseconds (2 seconds).
  • After the specified delay, the AJAX call is made to retrieve the value for Field B (test) based on the value of Field A (check).
  • Once the response is received, the getResponse() function is called to handle the response and set the value of Field B accordingly.

This approach should introduce a delay between when the user clicks the Add button and when the AJAX call is made, ensuring that the asynchronous call completes before proceeding with the form submission. Adjust the delay time (2000 milliseconds in this example) as needed to suit your requirements.

 

Please mark my answer as Accepted as Solution & hit helpful if it suffices your requirement !!! so that it will help other mates too !! 🙂

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

swathisarang98
Giga Sage
Giga Sage

Hi @String ,

 

you can try timeout session for alert which will delay the pop up, 

setTimeout(function(){

alert('Please wait');

}, 5000);
 
but with alert auto click ok easy not possible.
 

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

Regards,

Swathi Sarang