
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2022 10:55 AM
Hey guys! I was typing up a client script to find how many tickets were open before the creation of the new ticket so that I can give our business a queue number. This number won't be changing or updating so I just need to let the users know how many tickets are in front of them once, during the opening of the ticket. So I used an onSubmit client script to find the count and then add that number to a field (so that I can use it in our email notifications to the user when they create a ticket).
function onSubmit() {
var count=0;
var queue = new GlideRecord('u_creative_services_ticketing');
queue.addEncodedQuery("active=true^stateNOT IN5,4");
queue.query();
while(queue.next()){
count++;
}
//gs.log(count);
g_form.setValue('u_original_q_pos',count,true);
}
However I ended up getting an error on the ticket creation due to the addEncodedQuery, I supposed that is server sided only and not runnable on client.
So I want to make a Script Include instead. I'm super new to script includes but this is what I've typed up;
var getCSTQueueAjax = Class.create();
getCSTQueueAjax.prototype = {
getCount: function() {
var count = 0;
var gr = new GlideRecord('u_creative_services_ticketing');
gr.addEncodedQuery("active=true^stateNOT IN5,4");
gr.query();
while(gr.next()){
count++;
}
},
type: 'getCSTQueueAjax'
};
From here, how do I GlideAjax that information over to my OnSubmit client script so that I can set that field value to the count it finds? This is what I have so far based off of the research I've been able to do, but I'm getting pretty confused and lost after this.
function onSubmit() {
//Script Include
var ga = new GlideAjax('CST_Queue_Position_Counter');
//Function calld upon
ga.addParam('sysparm_name', 'getCount');
}
I'm just needing to grab that count value I set in the Script Include and g_form.setValue it.
Thank you in advance for your assistance I greatly appreciate it.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2022 05:42 PM
Hi Leo,
Shouldn't calling ajax in onSubmit. If needed, use before business rule instead.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2022 09:54 AM
If I use a before business rule, how do I set the field values via the variable I set? Since I don't think I can g_form in a business rule correct?
So I can setup a script to encoded query and find the amount of records. But how do I take that variable into the actions tab to set the field value to that count?
I imagine it's some type of javascript entry here I have to do but don't know how.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2022 10:15 AM
Just do a current.setValue('u_original_q_pos', count);
that said, this will happen and the user will not see it unless they happened to do a Save instead of Update. They would have to reload the record if they clicked Update to see this field.
Aoife

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2022 10:33 AM
Ahh I see. I should of realized that. Forgot about current. Business rule is working as intended now.
Thank you again Aoife for all your help on the Ajax. I learned a lot from you and I greatly appreciate it.
Thank you Hitoshi for pointing out a business rule would do the trick as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2022 10:57 AM
You are quite welcome. There are times you might want to use Ajax (mostly on the portal pages), but typically client scripts, ui policies, and business rules are all you need for things like this on UI Forms.
Please be sure to mark responses as helpful and mark the response you feel is the most complete as the Correct response. It gives us points for answering questions.
Thanks,
Aoife