Assistance with a Catalog Client Script Please

Stephencroc
Kilo Guru

I'm trying to get a catalog client script that is an onChange script that will check for the existence of the company name.  
I have a very simple form to experiment with that is just a check box and the Company Name and Address.
What I am trying to do is if the Company name that is typed in the Company Name box when you click or go to the next field it will clear the checkbox and clear the company name if the company name is already in the core_company table.  
Here is the code for the Script Include.  I don't really see any problems but been wrestling with this for a while now:

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

    checkCompany: function() {
        var companyName = this.getParameter('sysparm_company_name');
        var gr = new GlideRecord('core_company'); // Table name
        gr.addQuery('name', companyName); // Assuming 'name' is the field for company name
        gr.setLimit(1);
        gr.query();
        return gr.hasNext().toString(); // Returns 'true' if a record is found, otherwise 'false'
    },

    type: 'CheckCompanyExistence'
};


It is set a client callable and active

The Catalog Script include is set to be on the Company Name field and here is the code for that:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
// check to see if the company exists and if it does just return the message that it exists or not
    var ga = new GlideAjax('CheckCompanyExistence'); // Script Include
    ga.addParam('sysparm_name', 'checkCompany'); // Function name in the Script Include
    ga.addParam('sysparm_company_name', newValue);  // Pass the company name entered by the user
    ga.getXMLAnswer(function(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        if (answer === 'true') {
            g_form.addInfoMessage('The company exists in the system.');
        } else {
            g_form.addErrorMessage('The company does not exist in the system.');
        }
    });
}

I'm unsure as to what is causing the problem.
Here is the background script:

// Define the company name you want to check
var companyNameToCheck = '3Com'; // Replace with the actual company name

// Create an instance of the Script Include
var companyChecker = new CheckCompanyExistence();

// Call the method to check if the company exists
var exists = companyChecker.checkIfCompanyExists(companyNameToCheck);

// Log the result
if (exists === 'true') {
    gs.info('The company "' + companyNameToCheck + '" exists in the core_company table.');
} else {
    gs.info('The company "' + companyNameToCheck + '" does not exist in the core_company table.');
}


 When I try to run this on the Catalog Item I get a message (red) that reads:  ErrorThere is a JavaScript error in your browser console.  When looking at the console message. 
I get this:
Screenshot 2025-04-12 at 7.57.47 PM.png

 

Does anyone know how to fix this so I can check if the Company exists or not and print a message saying as much in Employee Center.  I can take care of the rest (clearing the other fields needed).
Thanks in advance

4 REPLIES 4

J Siva
Kilo Patron
Kilo Patron

Hi @Stephencroc 

Modify your client script as below.

Edited:

I missed the script include part earlier. Make sure to check the client callable check box in your script include and update your script include script with the below one.

 

Script include:

 

var CheckCompanyExistence = Class.create();

CheckCompanyExistence.prototype = Object.extendsObject(AbstractAjaxProcessor, { 

    checkCompany: function() {

        var companyName = this.getParameter('sysparm_company_name');

        var gr = new GlideRecord('core_company'); // Table name

        gr.addQuery('name', companyName); // Assuming 'name' is the field for company name

        gr.setLimit(1);

        gr.query();

        return gr.hasNext().toString(); // Returns 'true' if a record is found, otherwise 'false'

    },

    type: 'CheckCompanyExistence'

});

 

 

Client script:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue === '') {
        return;
    }
// check to see if the company exists and if it does just return the message that it exists or not
    var ga = new GlideAjax('CheckCompanyExistence'); // Script Include
    ga.addParam('sysparm_name', 'checkCompany'); // Function name in the Script Include
    ga.addParam('sysparm_company_name', newValue); // Pass the company name entered by the user
    ga.getXMLAnswer(handleResponse);
function handleResponse (answer){
        if (answer === 'true') {
            g_form.addInfoMessage('The company exists in the system.');
        } else {
            g_form.addErrorMessage('The company does not exist in the system.');
        }
    }
}

For more info please check the below article.

https://www.servicenow.com/community/developer-articles/getxmlanswer-vs-getxml/ta-p/2307589

 

Regarding the background script error. Your script include is client callable and your are trying to execute that from the server (background script). That's why it's throwing that error.

 

Hope this helps.

Regards,

Siva

Nishant8
Giga Sage
Giga Sage

Hello @Stephencroc, Although your approach seems to be fine, there are a few minor problems, listed below, can you please correct them and share the feedback?

1. you no need to parse the answer again since you are already using 'getXMLAnswer' method. Also, in anonymous function call, use answer instead of response.  Here is modified Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    // check to see if the company exists and if it does just return the message that it exists or not
    var ga = new GlideAjax('CheckCompanyExistence'); // Script Include
    ga.addParam('sysparm_name', 'checkCompany'); // Function name in the Script Include
    ga.addParam('sysparm_company_name', newValue); // Pass the company name entered by the user
    ga.getXMLAnswer(function(answer) {
        if (answer === 'true') {
            g_form.addInfoMessage('The company exists in the system.');
        } else {
            g_form.addErrorMessage('The company does not exist in the system.');
        }
    });
}

2. Although above modified Client Script might solve your errors, you might not get expected output since your Script Include isn't extending 'AbstractAjaxProcessor'. Here is modified Script include:

var CheckCompanyExistence = Class.create();
CheckCompanyExistence.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    
    checkCompany: function() {
        var companyName = this.getParameter('sysparm_company_name');
        var gr = new GlideRecord('core_company'); // Table name
        gr.addQuery('name', companyName); // Assuming 'name' is the field for company name
        gr.setLimit(1);
        gr.query();
        return gr.hasNext().toString(); // Returns 'true' if a record is found, otherwise 'false'
    },

    type: 'CheckCompanyExistence'
});

 

Regards,

Nishant

Hello @Stephencroc, Thank you for marking my answer as helpful...If it helps you resolve your issue, can you please mark it as solution so that it can be helpful for the future reader of this thread.

 

Regards,

Nishant

Nishant8
Giga Sage
Giga Sage

Hello @Stephencroc, Hope you are doing well. did you try the modified code I shared? Can you please let me know whether it solved the problem or you'd need any further assistance?

 

Regards,

Nishant