onChange Client Script No Response from Script Include

appstorm
Tera Contributor

Need help identifying why my Catalog CS is not able to get a response from my Script Include (see below):

 

SI:

var LARFStudentInformation = Class.create();
LARFStudentInformation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    // Client callable function to fetch student data
    fetchStudentInfo: function() {
        // Get the c_num parameter passed from the client script
        var cnum = this.getParameter('c_num'); // Retrieve the c_num passed via GlideAjax
        
        try {
            gs.info('Script Include entered with c_num: ' + cnum);  // Log entry into the Script Include

            if (!cnum) {
                gs.error('Error: c_num is required');
                return null;
            }

            var restMessage = new sn_ws.RESTMessageV2();
            restMessage.setRestResourceName('DEFAULT');
            restMessage.setHttpMethod('GET');
            restMessage.setQueryParameter('c_num', cnum);

            var response = restMessage.execute();

            if (!response) {
                gs.error('No response received from REST API for c_num: ' + cnum);
                return null;
            }

            var responseBody = response.getBody();
            gs.info('API Response Body for c_num ' + cnum + ': ' + responseBody);

            var parsedResponse = JSON.parse(responseBody);
            if (!Array.isArray(parsedResponse) || parsedResponse.length === 0) {
                gs.error('No student data found for c_num: ' + cnum);
                return null;
            }

            var studentData = parsedResponse[0];
            gs.info('Student Data Found: ' + JSON.stringify(studentData));

            // Return student data as a JSON string to the client
            return JSON.stringify({
                pidm: studentData.pidm,
                cnumber: studentData.cnumber,
                stst_code: studentData.stst_code,
                gpa_ind: studentData.gpa_ind,
                guest_ind: studentData.guest_ind,
                hold_ind: studentData.hold_ind,
                hold_codes: studentData.hold_codes
            });

        } catch (e) {
            gs.error('Error fetching student data for c_num: ' + cnum + '. Error: ' + e.message);
            return null;
        }
    }
});

 

CS:

function onChange(control, oldValue, newValue, isLoading) {
    // Exit if the form is still loading
    if (isLoading) {
        return;
    }

    // Exit if the c_num field is empty or hasn't changed
    if (!newValue || newValue === oldValue) {
        return;
    }

    console.log('onChange triggered. New c_num: ' + newValue); // Log new c_num value

    // Call the Script Include function to fetch student information
    getStudentInfo(newValue);
}

function getStudentInfo(cnum) {
    var ga = new GlideAjax('LARFStudentInformation'); // Name of Script Include must match exactly
    ga.addParam('sysparm_name', 'fetchStudentInfo');  // Name of the method to call in the Script Include
    ga.addParam('c_num', cnum); // Pass c_num to the Script Include

    // Add a callback function to process the response
    ga.getXMLAnswer(function(response) {
        console.log('GlideAjax response received: ' + response);

        // Check if response is valid
        if (response) {
            var studentInfo = JSON.parse(response);
            if (studentInfo) {
                g_form.setValue('student_information', JSON.stringify(studentInfo));
            } else {
                g_form.setValue('student_information', 'No student data found.');
            }
        } else {
            console.error('No response from Script Include for c_num: ' + cnum);
            g_form.setValue('student_information', 'Error fetching student data.');
        }
    });
}

 

The idea here is there is a field named student_information (string) on the form.  When the c_num field changes, the client script should call the Script Include and pass c_num as an http parameter back to the REST API to return the matching string.  Maybe there is a better way to do this?  We can't use tables to store data, since student data is rather large and contains sensitive information.

1 ACCEPTED SOLUTION

Vishal Jaswal
Giga Sage

Hello @appstorm 

Please adhere the script include and client script settings shown below:

Here is when I tried with dummy response in my PDI:

Dummy Catalog Item:

vishal_jaswal_3-1741720511958.png

 

Script Include with Glide Ajax Enabled / Client Callable

vishal_jaswal_1-1741720432898.png



 

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

    // Client callable function to fetch student data
    fetchStudentInfo: function() {
        // Get the c_num parameter passed from the client script
        var cnum = this.getParameter('c_num'); // Retrieve the c_num passed via GlideAjax

        try {
            gs.info('Script Include entered with c_num: ' + cnum); // Log entry into the Script Include

            if (!cnum) {
                gs.error('Error: c_num is required');
                return null;
            }

            // var restMessage = new sn_ws.RESTMessageV2();
            // restMessage.setRestResourceName('DEFAULT');
            // restMessage.setHttpMethod('GET');
            // restMessage.setQueryParameter('c_num', cnum);

            // var response = restMessage.execute();

            //dummy payload
            var response = {
                getBody: function() {
                    return JSON.stringify([{
                        pidm: "12345",
                        cnumber: "C12345",
                        stst_code: "VJ",
                        gpa_ind: "Y",
                        guest_ind: "N",
                        hold_ind: "Y",
                        hold_codes: ["HOLD1", "HOLD2"]
                    }]);
                },
                getStatusCode: function() {
                    return 200; 
                }
            };

            if (!response) {
                gs.error('No response received from REST API for c_num: ' + cnum);
                return null;
            }

            var responseBody = response.getBody();
            gs.info('API Response Body for c_num ' + cnum + ': ' + responseBody);

            var parsedResponse = JSON.parse(responseBody);
            if (!Array.isArray(parsedResponse) || parsedResponse.length === 0) {
                gs.error('No student data found for c_num: ' + cnum);
                return null;
            }

            var studentData = parsedResponse[0];
            gs.info('Student Data Found: ' + JSON.stringify(studentData));

            // Return student data as a JSON string to the client
            return JSON.stringify({
                pidm: studentData.pidm,
                cnumber: studentData.cnumber,
                stst_code: studentData.stst_code,
                gpa_ind: studentData.gpa_ind,
                guest_ind: studentData.guest_ind,
                hold_ind: studentData.hold_ind,
                hold_codes: studentData.hold_codes
            });

        } catch (e) {
            gs.error('Error fetching student data for c_num: ' + cnum + '. Error: ' + e.message);
            return null;
        }
    }
});

 

 Catalog Client Script

vishal_jaswal_2-1741720463020.png



 

function onChange(control, oldValue, newValue, isLoading) {
    // Exit if the form is still loading
    if (isLoading) {
        return;
    }
    // Exit if the c_num field is empty or hasn't changed
    if (!newValue || newValue === oldValue) {
        return;
    }
    console.log('onChange triggered. New c_num: ' + newValue); // Log new c_num value
    // Call the Script Include function to fetch student information
    getStudentInfo(newValue);
}

function getStudentInfo(cnum) {
    var ga = new GlideAjax('LARFStudentInformation'); // Name of Script Include must match exactly
    ga.addParam('sysparm_name', 'fetchStudentInfo'); // Name of the method to call in the Script Include
    ga.addParam('c_num', cnum); // Pass c_num to the Script Include
    // Add a callback function to process the response
    ga.getXMLAnswer(function(response) {
        console.log('GlideAjax response received: ' + response);
        // Check if response is valid
        if (response) {
            try {
                var studentInfo = JSON.parse(response);
                if (studentInfo) {
                    //displaying key:value pair formatting
                    var formattedInfo = '';
                    for (var key in studentInfo) {
                        if (studentInfo.hasOwnProperty(key)) {
                            formattedInfo += key + ': ' + studentInfo[key] + '\n';
                        }
                    }
                    g_form.setValue('student_information', formattedInfo.trim());
                } else {
                    g_form.setValue('student_information', 'No student data found.');
                }
            } catch (e) {
                console.error('Error parsing student information JSON:', e);
                g_form.setValue('student_information', 'Error parsing student data.');
            }
        } else {
            console.error('No response from Script Include for c_num: ' + cnum);
            g_form.setValue('student_information', 'Error fetching student data.');
        }
    });

}

 

Before c_num value change

vishal_jaswal_4-1741720543825.png



After c_num value change

vishal_jaswal_5-1741720563148.png


Hope it helps!




Hope that helps!

View solution in original post

11 REPLIES 11

Vishal Jaswal
Giga Sage

Hello @appstorm 

Please adhere the script include and client script settings shown below:

Here is when I tried with dummy response in my PDI:

Dummy Catalog Item:

vishal_jaswal_3-1741720511958.png

 

Script Include with Glide Ajax Enabled / Client Callable

vishal_jaswal_1-1741720432898.png



 

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

    // Client callable function to fetch student data
    fetchStudentInfo: function() {
        // Get the c_num parameter passed from the client script
        var cnum = this.getParameter('c_num'); // Retrieve the c_num passed via GlideAjax

        try {
            gs.info('Script Include entered with c_num: ' + cnum); // Log entry into the Script Include

            if (!cnum) {
                gs.error('Error: c_num is required');
                return null;
            }

            // var restMessage = new sn_ws.RESTMessageV2();
            // restMessage.setRestResourceName('DEFAULT');
            // restMessage.setHttpMethod('GET');
            // restMessage.setQueryParameter('c_num', cnum);

            // var response = restMessage.execute();

            //dummy payload
            var response = {
                getBody: function() {
                    return JSON.stringify([{
                        pidm: "12345",
                        cnumber: "C12345",
                        stst_code: "VJ",
                        gpa_ind: "Y",
                        guest_ind: "N",
                        hold_ind: "Y",
                        hold_codes: ["HOLD1", "HOLD2"]
                    }]);
                },
                getStatusCode: function() {
                    return 200; 
                }
            };

            if (!response) {
                gs.error('No response received from REST API for c_num: ' + cnum);
                return null;
            }

            var responseBody = response.getBody();
            gs.info('API Response Body for c_num ' + cnum + ': ' + responseBody);

            var parsedResponse = JSON.parse(responseBody);
            if (!Array.isArray(parsedResponse) || parsedResponse.length === 0) {
                gs.error('No student data found for c_num: ' + cnum);
                return null;
            }

            var studentData = parsedResponse[0];
            gs.info('Student Data Found: ' + JSON.stringify(studentData));

            // Return student data as a JSON string to the client
            return JSON.stringify({
                pidm: studentData.pidm,
                cnumber: studentData.cnumber,
                stst_code: studentData.stst_code,
                gpa_ind: studentData.gpa_ind,
                guest_ind: studentData.guest_ind,
                hold_ind: studentData.hold_ind,
                hold_codes: studentData.hold_codes
            });

        } catch (e) {
            gs.error('Error fetching student data for c_num: ' + cnum + '. Error: ' + e.message);
            return null;
        }
    }
});

 

 Catalog Client Script

vishal_jaswal_2-1741720463020.png



 

function onChange(control, oldValue, newValue, isLoading) {
    // Exit if the form is still loading
    if (isLoading) {
        return;
    }
    // Exit if the c_num field is empty or hasn't changed
    if (!newValue || newValue === oldValue) {
        return;
    }
    console.log('onChange triggered. New c_num: ' + newValue); // Log new c_num value
    // Call the Script Include function to fetch student information
    getStudentInfo(newValue);
}

function getStudentInfo(cnum) {
    var ga = new GlideAjax('LARFStudentInformation'); // Name of Script Include must match exactly
    ga.addParam('sysparm_name', 'fetchStudentInfo'); // Name of the method to call in the Script Include
    ga.addParam('c_num', cnum); // Pass c_num to the Script Include
    // Add a callback function to process the response
    ga.getXMLAnswer(function(response) {
        console.log('GlideAjax response received: ' + response);
        // Check if response is valid
        if (response) {
            try {
                var studentInfo = JSON.parse(response);
                if (studentInfo) {
                    //displaying key:value pair formatting
                    var formattedInfo = '';
                    for (var key in studentInfo) {
                        if (studentInfo.hasOwnProperty(key)) {
                            formattedInfo += key + ': ' + studentInfo[key] + '\n';
                        }
                    }
                    g_form.setValue('student_information', formattedInfo.trim());
                } else {
                    g_form.setValue('student_information', 'No student data found.');
                }
            } catch (e) {
                console.error('Error parsing student information JSON:', e);
                g_form.setValue('student_information', 'Error parsing student data.');
            }
        } else {
            console.error('No response from Script Include for c_num: ' + cnum);
            g_form.setValue('student_information', 'Error fetching student data.');
        }
    });

}

 

Before c_num value change

vishal_jaswal_4-1741720543825.png



After c_num value change

vishal_jaswal_5-1741720563148.png


Hope it helps!




Hope that helps!

This works - Thank you!  Could you identify the problem?  Also, I created both the Script Include and Client Script in the Global Scope. I'm not sure if this made a difference, seeings how the Universal Scope may have attributed to the issue.