Catalog Client Script help needed

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:  Error There is a JavaScript error in your browser console.  When looking at the console message. 
I get this:

Stephencroc_0-1744506337650.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

 
2 ACCEPTED SOLUTIONS

J Siva
Tera Sage

Hi @Stephencroc 

Modify your client script as below.

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.

Edited:

I missed the script include part. As Robert mentioned, correct your script include as well. Make sure to check the client callable check box in your script include and update your script include script with the below one.

 

 

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'

});

 

Hope this helps.

Regards,

Siva

View solution in original post

Robert H
Mega Sage

Hello @Stephencroc ,

 

There are two issues, one in the Script Include and one in the Client Script.

 

1) In order to be available for a GlideAjax call, the class in the Script Include needs to extend from 

"AbstractAjaxProcessor". Here is the updated Script Include:
 
var CheckCompanyExistence = Class.create();
CheckCompanyExistence.prototype = Object.extendsObject(AbstractAjaxProcessor, { // this is the changed line

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

 

2) When you use GlideAjax.getXMLAnswer() then the parameter passed to the callback function is already unwrapped from the XML response and contains the return value of the Script Include method. No need to do "response.responseXML.documentElement.getAttribute('answer');".

 

Here is the updated 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) { // the answer is already passed to the callback
        if (answer === 'true') {
            g_form.addInfoMessage('The company exists in the system.');
        } else {
            g_form.addErrorMessage('The company does not exist in the system.');
        }
    });
}

 

PS: and the background script fails because there is no "checkIfCompanyExists" method in your Script Include, it's called "checkCompany". And you are passing an argument to it but the function is defined without parameters.

 

Regards,

Robert

View solution in original post

4 REPLIES 4

J Siva
Tera Sage

Hi @Stephencroc 

Modify your client script as below.

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.

Edited:

I missed the script include part. As Robert mentioned, correct your script include as well. Make sure to check the client callable check box in your script include and update your script include script with the below one.

 

 

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'

});

 

Hope this helps.

Regards,

Siva

Robert H
Mega Sage

Hello @Stephencroc ,

 

There are two issues, one in the Script Include and one in the Client Script.

 

1) In order to be available for a GlideAjax call, the class in the Script Include needs to extend from 

"AbstractAjaxProcessor". Here is the updated Script Include:
 
var CheckCompanyExistence = Class.create();
CheckCompanyExistence.prototype = Object.extendsObject(AbstractAjaxProcessor, { // this is the changed line

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

 

2) When you use GlideAjax.getXMLAnswer() then the parameter passed to the callback function is already unwrapped from the XML response and contains the return value of the Script Include method. No need to do "response.responseXML.documentElement.getAttribute('answer');".

 

Here is the updated 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) { // the answer is already passed to the callback
        if (answer === 'true') {
            g_form.addInfoMessage('The company exists in the system.');
        } else {
            g_form.addErrorMessage('The company does not exist in the system.');
        }
    });
}

 

PS: and the background script fails because there is no "checkIfCompanyExists" method in your Script Include, it's called "checkCompany". And you are passing an argument to it but the function is defined without parameters.

 

Regards,

Robert

Ankur Bawiskar
Tera Patron
Tera Patron

@Stephencroc 

members have already shared the approach and it should work for you

share the feedback

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Sandeep Rajput
Tera Patron
Tera Patron

@Stephencroc It looks like you initially created a Server side script include and later on checked the Client Callable check box. Due to this your script include doesn't extend from 

Object.extendsObject(AbstractAjaxProcessor

 

A server side script include can't be called on the client side. I recommend you to either extend your script include with AbstractAjaxProcessor or create a client callable script include from scratch so that you wouldn't have to take care of the syntax. 

 

Also, in the client script update the getXMLAnswer call

ga.getXMLAnswer(function(response) {       
        if (response === 'true') {
            g_form.addInfoMessage('The company exists in the system.');
        } else {
            g_form.addErrorMessage('The company does not exist in the system.');
        }
    });