On change client script is not working fine

anshul_jain25
Kilo Guru

Hi

I have written a code for onchange client script,

I want to populate different options for topic when differrent options are selected from category.

Every thing is working fine, its showing all the values correct, but after few seconds

for eg i have selected benefits in category and fastly i am selecting topic than its not showing any option, but when i have selected category and after 2-3 secs if i am selecting topic than i can see all the option,

So i believe this is some performance issue that my code is taking more time to populate option in topic field, you can see in below screenshots, first no options are coming , in second all option are coming when i selected topic after 2-3 secs

find_real_file.pngfind_real_file.png

Below is the code which i have written for this

I am getting a list from my table, using script include, than populating through add option function

now here '--None--' option is coming as soon as i am changing category value which is not in for loop, so i believe for loop is taking a bit of time to perform action

What should i do here, so that my code run fast.

find_real_file.png

1 ACCEPTED SOLUTION

satoshi_yamamur
Kilo Expert

Hi Anshul,


If you want to use the script include, use getXMLWait() instead of getXML().


getXML() is asynchronous so the function 'DoSomething()' runs after you get data from ServiceNow. Getting data takes 2-3 seconds.



http://wiki.servicenow.com/index.php?title=GlideAjax#Synchronous_Glide_Ajax


View solution in original post

15 REPLIES 15

lSurya 24
Giga Guru

Hello Anshul,



Instead of that code,



Try the below code with your variables:



var gp = new GlideRecord('sys_choice');


  gp.addQuery('dependent_value', newValue);


  gp.addQuery('element', 'u_subcategory');


  gp.query();


  if(!gp.hasNext()){


  g_form.clearOptions('u_subcategory');


  }


  if(gp.next()){


  g_form.addOption('u_subcategory', gp.value, gp.label);


  g_form.setValue('u_subcategory', gp.value);


  }


hi surya, we need to user client script not catalog client script,for client script we are bound to use glide record directly we have to use script include.



Please make me correct if i am wrong, the above will work with catalog client script i guess


The above will also work for client scripts. try the above code and check which is performing at optimum rates.


satoshi_yamamur
Kilo Expert

Hi Anshul,


If you want to use the script include, use getXMLWait() instead of getXML().


getXML() is asynchronous so the function 'DoSomething()' runs after you get data from ServiceNow. Getting data takes 2-3 seconds.



http://wiki.servicenow.com/index.php?title=GlideAjax#Synchronous_Glide_Ajax