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

Hi santoshi, i am getting null value with getXMLWait() function


Hello Anshul,



Can you share the code ? Also to mention xmlwait is not a best practise to use unless otherwise required. it will degrade the performance because of the wait function.


hi surya, you can check code for client script from my original post




find_real_file.png


below is script include script



find_real_file.png


In your script include, below line is wrong, you can not passing any sysparm_topic parameter from client script.


var topic = this.getParameter('sysparm_topic');



also, please use getValue() function in you if and else condition to get the value of sub_topic like : hr_topic.getValue('sub_topic');



Check of this helps. Also you please paste your script instead screen shot that would be easy for us to make correction in that.


oh that screen shot is for another function,


please check below code, this code is working fine when i am using getXML(), but the only thing is its taking 1-3 secs time to show topic list so i want to use getXMLWait so that till 1-3 secs that screen should stop and only after we get answer from server than only we should be able to select topic.



If there is any other way to hold screen for 2-3 secs please let me know



getTopicBasedOnCategory: function() {


var topic_list = '';


var cat = this.getParameter('sysparm_category');


var hr_cat = new GlideRecord('x_prgm_hr_employee_hr_employee_care_topic_subtopic');


hr_cat.addQuery('category',cat);


hr_cat.query();


while(hr_cat.next()){


if(topic_list == ''){


topic_list += hr_cat.topic;


}


else{


if(topic_list.indexOf(hr_cat.topic) == -1){


topic_list += ','+ hr_cat.topic;


}


}


}


var answer = topic_list;


return answer;


},