The CreatorCon Call for Content is officially open! Get started here.

scripts to check single line text against table

TMKAM
Tera Contributor

I have created Single line text in service catalog, and I have table 'trusted domain whitelisting".

Details as per below

 

Single line Text variable (u_domain_name)

table name = u_trusted_domain_whitelisting

Table Column 1 = u_active (True/False)

Table Column 2 = u_domain_whitelisting 

 

Here is scenario.

 

Scenario 1

When user enter a name in u_domain_name (variable), need to validate the domain name is existed or not in table "u_trusted_domain_whitelisting" and check if Table Column 1 = u_active (True) and it should check Table Column 2 = u_domain_whitelisting is existed if yes it need to show up an error message "Domain is available" and when user press "OK" it need to clear the u_domain_name (variable).

 

Scenario 2

When user enter a name in u_domain_name (variable), need to validate the domain name is existed or not in table "u_trusted_domain_whitelisting" and check if Table Column 1 = u_active (False) and it should skip check Table Column 2 = u_domain_whitelisting and nothing happen.

 

Please help me any one idea about this?

17 REPLIES 17

@TMKAM 

I have put some alert in the code please try let me know alert u getting

 

function onChange(control, oldValue, newValue, isLoading) {
    // Check if the control being changed is 'u_domain_name'
    if (control == 'u_domain_name' && !isLoading) {
        var domainName = g_form.getValue('u_domain_name');
       alert(domainName);
        // Check if the entered domain exists in the trusted domain whitelisting table
        var domainWhitelisting = new GlideRecord('u_trusted_domain_whitelisting');
        domainWhitelisting.addQuery('u_domain_whitelisting', domainName);
        domainWhitelisting.query();

        if (domainWhitelisting.next()) {
      alert("domain exist");
            // Domain exists in the table
            if (domainWhitelisting.u_active) {
         alert("domain exist and active");
                // Active domain, show error message
                g_form.showFieldMsg('u_domain_name', "Domain is not available.", 'error');
                g_form.clearValue('u_domain_name'); // Clear the entered domain
            }
            // If u_active is false, do nothing
        }
        // If the domain does not exist in the table, do nothing
    }
}

 

 

Kindly mark helpful/accepted if it helps you.

TMKAM
Tera Contributor

Dear Maddysunil,

 

I try the new scripts and there is no alert when the u_active is True and u_domain_whitelisting is exist.

@TMKAM 

Seems like we are not getting the data..Can u try using client script and script include, also put one more alert in the beginning inside client script to check if client script triggering or not

 

function onChange(control, oldValue, newValue, isLoading) {
    // Check if the control being changed is 'u_domain_name'
    if (control == 'u_domain_name' && !isLoading) {
        var domainName = g_form.getValue('u_domain_name');

        // Check if the entered domain exists in the trusted domain whitelisting table
        var gr = new GlideAjax('CheckDomainAvailabilityAjax');
        gr.addParam('sysparm_name', 'checkDomainAvailability');
        gr.addParam('sysparm_domain_name', domainName);
        gr.getXML(checkDomainCallback);
    }
}

function checkDomainCallback(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    
    if (answer === 'unavailable') {
        g_form.showFieldMsg('u_domain_name', "Domain is not available.", 'error');
        g_form.clearValue('u_domain_name'); // Clear the entered domain
    }
    // If the domain is available, do nothing
}
var CheckDomainAvailabilityAjax = Class.create();

CheckDomainAvailabilityAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkDomainAvailability: function() {
        var domainName = this.getParameter('sysparm_domain_name');

        // Check if the entered domain exists in the trusted domain whitelisting table
        var domainWhitelisting = new GlideRecord('u_trusted_domain_whitelisting');
        domainWhitelisting.addQuery('u_domain_whitelisting', domainName);
        domainWhitelisting.query();

        if (domainWhitelisting.next() && domainWhitelisting.u_active) {
            return 'unavailable';
        } else {
            return 'available';
        }
    }
});

 

Kindly mark helpful/accepted if it helps you.

TMKAM
Tera Contributor

dear maddysunil,

I have tried the new scripts u have provided and there is no error messages as well. i attach the scripts for your reference.

or can we try without checking the u_active is false or true to see whether it read the table?

what do you think

@TMKAM 

Lets debug from the beginning

Just update the client script and see what u getting in the first alert:

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

        var domainName = g_form.getValue('u_domain_name');
        alert(domainName);
        // Check if the entered domain exists in the trusted domain whitelisting table
        var gr = new GlideAjax('CheckDomainAvailabilityAjax');
        gr.addParam('sysparm_name', 'checkDomainAvailability');
        gr.addParam('sysparm_domain_name', domainName);
        gr.getXML(checkDomainCallback);
    }
}

function checkDomainCallback(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    
    if (answer === 'unavailable') {
        g_form.showFieldMsg('u_domain_name', "Domain is not available.", 'error');
        g_form.clearValue('u_domain_name'); // Clear the entered domain
    }
    // If the domain is available, do nothing
}