Gliderecord not working in catalog client scripts

Jake Adams
Tera Contributor

Hi,

I have a single line text variable in a catalog. The variable question is 'which application'

In the question_choice table for the question 'which application' I have added 4 choices A, B, C and D.

 

Now when I enter a value in the single line text and that value matches anyone of this choice. I want to clear the single line field.

 

I am trying to write a onChange client script but it's not working

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//var ad = g_form.getValue('which_application');
var gr = new GlideRecord('question_choice');
gr.addEncodedQuery('question=103d1e4bdb3d9810c69960d444961966');
gr.query();
while (gr.next()) {
if (gr.value == g_form.getValue('which_application')) {
g_form.clearValue('which_application');
}

}

 

1 ACCEPTED SOLUTION

@Jake Adams 

 

In that case you have to use Script Include using GlideAjax() API where you can pass your 'which_application' value. In that Script Include you can query 'question_choice' table using GlideRecord and compare values and return true if both are same.

 

Then in your Catalog Client script you can check that if answer is true than g_form.clearValue('which_application');

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

View solution in original post

6 REPLIES 6

suvro
Mega Sage
Mega Sage

It is a bad practice to use GlideRecord in a client script. Use GlideAjax. Use below article on how to use GlideAjax

https://www.servicenow.com/community/it-service-management-articles/how-to-fetch-other-details-of-th...

AnubhavRitolia
Mega Sage
Mega Sage

@Jake Adams 

 

As you mentioned you have 1 variable only as single Line Text and also have there Choices defined, I guess you will get only those 4 choices and not text field to put any value.

 

Can you show the screenshot of your variable, how it looks?

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Hi,

Yes it's a single line text but the he choices I have added are not showing up in it. 

 

The thing is I don't want to allow that 4 values to be entered in that text variable.

 

I was using index of but my client was not satisfied with that, and want to store those values in question_choice table and query it and based on it need to restrict those values.

 

Is there any other best way?

@Jake Adams 

 

In that case you have to use Script Include using GlideAjax() API where you can pass your 'which_application' value. In that Script Include you can query 'question_choice' table using GlideRecord and compare values and return true if both are same.

 

Then in your Catalog Client script you can check that if answer is true than g_form.clearValue('which_application');

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023