- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 09:19 AM - edited 04-17-2024 10:43 AM
Hello Team
I'm trying to write this getRowCount function in script include & write the catalog client Script.
This is my requirement:
if the count = 3 and the courses are active. Alert the user that he/she is not allowed to enroll anymore class.
if the count < 3 and the courses are inactive. Alert the user can enroll 3 more active classes.
I tested and it keeps come up javascript browser error. I'm not sure why. Please help me on this. Thanks for all your help.
These are 2 errors from the console:
getcoursecount: function() {
var student = this.getParameter('sysparm_student');
var courses = this.getParameter('sysparm_course');
var emails = this.getParameter('sysparm_email');
// Query the course table
var count = 0;
var ga = new GlideRecord('x_74571_kb_univ_ku_enrollment');
ga.addQuery('full_name', student);
// ga.addQuery('email', emails);
ga.query();
while (ga.next()) {
count++;
}
// Check if count = 3 and courses are active
if (count === 3) {
var activeCoursesCount = new GlideAggregate('x_74571_kb_univ_ku_enrollment');
activeCoursesCount.addQuery('full_name', student);
activeCoursesCount.addQuery('acive', 'true'); // Assuming 'course_status' is the field for course status
activeCoursesCount.addAggregate('COUNT');
activeCoursesCount.query();
if (activeCoursesCount.next()) {
var activeCount = parseInt(activeCoursesCount.getAggregate('COUNT'));
if (activeCount === 3) {
return 'active_count_equals_3';
}
}
}
// Check if count < 3 and courses are inactive
if (count < 3) {
var inactiveCoursesCount = new GlideAggregate('x_74571_kb_univ_ku_enrollment');
inactiveCoursesCount.addQuery('full_name', student);
inactiveCoursesCount.addQuery('active', 'false'); // Assuming 'course_status' is the field for course status
inactiveCoursesCount.addAggregate('COUNT');
inactiveCoursesCount.query();
if (inactiveCoursesCount.next()) {
var inactiveCount = parseInt(inactiveCoursesCount.getAggregate('COUNT'));
if (inactiveCount < 3) {
return 'inactive_count_less_than_3';
}
}
}
// Default return if none of the conditions match
return 'no_match';
},
type: 'getCourseValidation'
});
//Catalog Client Script:
var student = g_form.getValue('student_full_name');
var email = g_form.getValue('email');
var course = g_form.getValue('technology_courses');
var ga = new GlideAjax('x_74571_kb_univ.getcoursecount');
ga.addParam('sysparm_name', 'getcoursecount');
ga.addParam('sysparm_student', student);
ga.addParam('sysparm_course', course);
ga.addParam('sysparm_email', email);
ga.getXML(handleResponse); // Call the handleResponse function with the AJAX response
}
function handleResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'active_count_equals_3') {
alert('The student has already enrolled in 3 active courses.');
} else if (answer == 'inactive_count_less_than_3') {
alert('The student can enroll in more courses as they have fewer than 3 inactive courses.');
} else {
alert('No matching condition found.');
}
}}}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 02:34 PM
Modified the Script Include as below:
getcoursecount: function() {
var student = this.getParameter('sysparm_student');
var courses = this.getParameter('sysparm_course');
var emails = this.getParameter('sysparm_email');
// Query the course table
var count = 0;
var ga = new GlideRecord('x_74571_kb_univ_ku_enrollment');
ga.addAggregate('COUNT');
ga.addQuery('full_name', student);
ga.addQuery('active', true);
ga.query();
if (ga.next()) {
count = ga.getAggregate('COUNT');
if(count >= 3)
{
return 'active_count_equals_3';
}
else if(count > 0)
{
return 'inactive_count_less_than_3';
}
else
{
return 'no_match';
}
}
}
A few things to note
- Since you are not using the emails and the courses, consider removing them from the script
- I think you had typos for the 'active' field, it was spelled 'acive'
- Wasn't sure why you had two separate queries, it's consolidated into a single query
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 03:27 PM
I made the changes and you suggested. I still get the alert prompt "No matching condition found." . I feel like we get so close to fix this thing. Thanks for your effort and time into this.
Here is all scripts that i updated below so far.
Script include
getcoursecount: function() {
var student = this.getParameter('sysparm_student');
// Query the course table
var count = 0;
var ga = new GlideRecord('x_74571_kb_univ_ku_enrollment');
ga.addAggregate('COUNT');
ga.addQuery('full_name', student);
ga.addQuery('active', true);
ga.query();
if (ga.next()) {
count = ga.getAggregate('COUNT');
if(count >= 3)
{
return 'active_count_equals_3';
}
else if(count > 0)
{
return 'inactive_count_less_than_3';
}
else
{
return 'no_match';
}
}
},
Catalog Client Script
var student = g_form.getValue('student_full_name');
var ga = new GlideAjax('x_74571_kb_univ.getCourseValidation');
ga.addParam('sysparm_name', 'getcoursecount');
ga.addParam('sysparm_student', student);
ga.getXML(handleResponse); // Call the handleResponse function with the AJAX response
}
function handleResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'active_count_equals_3') {
alert('The student has already enrolled in 3 active courses.');
} else if (answer == 'inactive_count_less_than_3') {
alert('The student can enroll in more courses as they have fewer than 3 inactive courses.');
} else {
alert('No matching condition found.');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 03:38 PM
Ah, I made a mistake up there.
Instead of:
var ga = new GlideRecord('x_74571_kb_univ_ku_enrollment');
It should be the following:
var ga = new GlideAggregate('x_74571_kb_univ_ku_enrollment');
If it still doesn't work, can you make sure the field names are correct?
Try adding the filter on the list view and copying its encoded query.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 09:50 AM
What does the error say? That would help us more efficiently help you otherwise, we have to read all this code you've pasted on the forums.
As an additional note, I'd recommend using the "Insert/Edit code sample" button and pasting your code in that way so it's more organized and easier to read. Right now, it's like a wall of text.
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 10:46 AM
I updated the post. Thanks for let me know about that cool tools "Insert/Edit code". It looks much better now to read.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 01:52 PM
Hi @SnowDevOps,
Your Script Include's name is getCourseValidation but you are invoking a different name in the client script - x_74571_kb_univ.getcoursecount
I haven't gone through the entire code but let's see if that fixes it.
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 02:22 PM
That was great catch ! Thank you. The javascript error went away. But I got an alert prompt from Catalog client script. When i select the user name. It seems like the script is working but somewhere in the logic from the client script doesn't look right.
I'm currently testing a user who currently taking 3 courses & all 3 courses are active. Then i tried to register 1 more course.
This is what i'm getting from the alert prompt "No matching condition found".