Unhandled Exception in GlideAjax

appstorm
Tera Contributor

Need some help debugging my Client Script/ Script Include.  The Client Script is supposed to call a Script Include to display JSON output from a REST message, in a form select box (drop-down) but it is throwing an Unhandled Exception in GlideAjax in the browser console.

 

CS:

function onLoad() {
    // Get the control for the 'course_information' field using its field name
    var courseSelect = g_form.getControl('course_information');  // Internal field name

    // Ensure the field exists
    if (courseSelect) {
        // Create GlideAjax instance to call the Script Include
        var ga = new GlideAjax('CourseInformation');  // Name of the Script Include
        ga.addParam('method', 'getCourses');  // The method inside your Script Include that returns courses

        // Fetch course data via GlideAjax and handle it asynchronously
        ga.getXMLAnswer(function(response) {
            var courseData = JSON.parse(response);  // Parse the response to handle course data
            
            // Clear any existing options in the dropdown
            courseSelect.innerHTML = '';

            // Add a default option to the dropdown
            var defaultOption = document.createElement('option');
            defaultOption.value = '';
            defaultOption.text = '-- Select a Course --';
            courseSelect.appendChild(defaultOption);

            // Populate dropdown with course data
            for (var i = 0; i < courseData.length; i++) {
                var course = courseData[i];
                var option = document.createElement('option');
                option.value = course.crn;  // Set CRN as the value
                option.text = course.subject_code + " " + course.course_number;  // Display subject_code + course_number
                courseSelect.appendChild(option);
            }
        });
    } else {
        console.error('Field "course_information" is not found or rendered on the form.');
    }
}

 

SI:

var CourseInformation = Class.create();
CourseInformation.prototype = {
    initialize: function() {},

    // Method to fetch course data from the LARF Banner Courses REST message
    getCourses: function() {
        var courseData = [];
        
        // Create an instance of the RESTMessageV2 object
        var restMessage = new sn_ws.RESTMessageV2();
        restMessage.setRestMessageName('LARF Banner Courses');  // Your REST message name
        var response = restMessage.execute();
        var responseBody = response.getBody();
        var responseData = JSON.parse(responseBody);  // Assuming JSON response format
        
        // Process and format the response data
        for (var i = 0; i < responseData.length; i++) {
            var course = responseData[i];
            courseData.push({
                crn: course.crn,
                term_code: course.term_code,
                part_of_term: course.part_of_term,
                subject_code: course.subject_code,
                course_number: course.course_number,
                sequence: course.sequence,
                faculty_cnumber: course.faculty_cnumber
            });
        }

        // Return the formatted course data
        return JSON.stringify(courseData);
    },
    
    type: 'CourseInformation'
};

 

6 REPLIES 6

Tai Vu
Kilo Patron
Kilo Patron

Hi @appstorm 

In your client script, change this line below from

ga.addParam('method', 'getCourses');

to 

ga.addParam('sysparm_name', 'getCourses');

 

And, your script include is not the Client callable one.

TaiVu_0-1741330362595.png

The class should be:

var SCUtilAJAX = Class.create();
SCUtilAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getCourses: function(){
		//TODO
	},

    type: 'SCUtilAJAX'
});

 

Cheers,

Tai Vu

appstorm
Tera Contributor

Thank you for the help!  I made the changes suggested, below:

 

var SCUtilAJAX = Class.create();
SCUtilAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getCourses: function() {
        var courseData = [];
        
        // Create an instance of the RESTMessageV2 object
        var restMessage = new sn_ws.RESTMessageV2();
        restMessage.setRestMessageName('LARF Banner Courses');  // Your REST message name
        var response = restMessage.execute();
        var responseBody = response.getBody();
        var responseData = JSON.parse(responseBody);  // Assuming JSON response format
        
        // Process and format the response data with additional filtering
        for (var i = 0; i < responseData.length; i++) {
            var course = responseData[i];
            
            // Filter courses where:
            // 1. part_of_term is 82x
            // 2. sequence ends with 'P'
            // 3. faculty_cnumber is not null or empty
            if (course.part_of_term === '82x' && 
                course.sequence.endsWith('P') &&
                course.faculty_cnumber &&  // This checks for non-null and non-empty faculty_cnumber
                course.faculty_cnumber.trim() !== '') {
                    
                courseData.push({
                    crn: course.crn,
                    term_code: course.term_code,
                    part_of_term: course.part_of_term,
                    subject_code: course.subject_code,
                    course_number: course.course_number,
                    sequence: course.sequence,
                    faculty_cnumber: course.faculty_cnumber
                });
            }
        }

        // Return the filtered course data as a JSON string
        return JSON.stringify(courseData);
    },

    type: 'SCUtilAJAX'
});

However, I am still getting a "sp_min.jsx?v=01-12-2025_1705:2762 Unhandled exception in GlideAjax. Cannot read properties of null (reading 'createElement')" console error.  I thought perhaps this was due to some courses returning 'null' for instructor.  However, removing those still returns no results.

 

Also, there is no Glide AJAX enabled checkbox on my Script Include.