Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

getRowCount Help

SnowDevOps
Giga Guru

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:

SnowDevOps_0-1713375566508.png

 

SnowDevOps_1-1713375750726.png

 

 

 

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.');
    }
   
   
}}}

 

 
3 ACCEPTED SOLUTIONS

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

 

View solution in original post

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.');
    }
}

 

View solution in original post

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.

 

View solution in original post

8 REPLIES 8

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

 

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.');
    }
}

 

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.

 

It worked beautifully when I replaced it with a new GlideAggregate. Thanks You so much