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.

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

priyatam_pvp
Tera Guru

Can you check if the script include is client callable?

if yes, the SI first two lines should be like below

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

Please mark it helpful
Regards
Priyatam

Community Alums
Not applicable

Hi @appstorm 

 

Kindly update below line of code in your client script and check.

 

ga.addParam('sysparm_name, 'getCourses'); // The method inside your Script Include that returns courses

 

Note: The first call to addParam should be with the parameter sysparm_name and the name of the server-side method you want to call.

 

 

https://www.servicenow.com/docs/bundle/yokohama-api-reference/page/app-store/dev_portal/API_referenc...

 

Mark this helpful/ Accept the solution if it helps you

appstorm
Tera Contributor

Thank you for your reply!  I have made the change.  However, the result remains the same.

appstorm
Tera Contributor

Also getting this response in the console: "1 Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received"