Onchange Client script response

String
Kilo Sage

Hi All ,

Am using the below code in Catalog Client Scripts (variable set) onchange and using the below code 

 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// This is used for Consumable Ordering Portal
var cons = g_form.getValue('u_test');
// Call script include
var ga = new GlideAjax('test'); // Script include
ga.addParam('sysparm_name', 'getcount'); // Method
ga.addParam('test1', cons);
// Execute asynchronously
ga.getXML(getResponse);
}
function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('total', answer);
}
 
 
when Item field is changed ,than above on change script will trigger and get the value set to total field ,But I need to click anywhere on the screen .so value is set to total field
after changes the item value ,use directly click on submit than total field is empty 
 
Is there any way when Item filed changes than immediately it should trigger and set the value in total field 
5 REPLIES 5

Ademir Amaral1
Kilo Sage

Hi @String 

 

From what I understand, you're facing an issue where the "total" field is not updated immediately after the change in the "Item" field. This is because the onchange event is only triggered when the focus is lost on the field.

To solve this issue, you can try adding an additional event that triggers the onChange function when the "Item" field is changed. For example, you can add the "onkeyup" event to the "Item" field so that the onChange function is triggered whenever a key is pressed in the field.

Here's an example of how you can add the "onkeyup" event to the "Item" field:

// Add this to your existing code
var itemField = g_form.getControl('item_field'); // Replace "item_field" with the name of the "Item" field
itemField.onkeyup = function() {
onChange(itemField, '', itemField.value, false);
};

This should trigger the onChange function immediately after the change in the "Item" field. I hope this helps!

DYCM
Mega Sage

Hi @String ,

In onLoad event, register onkeyup event, and move your code from OnChange to onkeyup function.

 

function onLoad() {
   var field = g_form.getControl("name");
   field.onkeyup = function(){
	// Move your code from OnChange to here!
	alert("Put your logic here");
   };
}

 

 

Please refer to the screenshot for details:

2.png

on keyup:3.png

Amit Gujarathi
Giga Sage
Giga Sage

HI @String ,
I trust you are doing great.
Please find below reference code for the same :

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    // This is used for Consumable Ordering Portal
    var cons = g_form.getValue('u_test');
    // Call script include synchronously
    var ga = new GlideAjax('test'); // Script include
    ga.addParam('sysparm_name', 'getcount'); // Method
    ga.addParam('test1', cons);
    // Execute synchronously
    var response = ga.getXMLWait();
    getResponse(response);
}

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

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hi @Amit Gujarathi  thanks for your response ,but am getting the below Error 

GlideAjax.getXMLWait is no longer supported !