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

Maddysunil
Kilo Sage

@TMKAM 

You can write a onsubmit client script..You can try the below code, validate the fields and table name in script .

 

function onSubmit() {
    var domainName = g_form.getValue('u_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()) {
        // Domain exists in the table
        if (domainWhitelisting.u_active) {
            // Active domain, show error message
            g_form.addErrorMessage("Domain is not available.");
            g_form.clearValue('u_domain_name'); // Clear the entered domain
            return false; // Prevent form submission
        } else {
            // Inactive domain, allow submission without further checks
            return true;
        }
    }

    // Domain does not exist in the table, allow submission
    return true;
}

 

 

Kindly mark helpful/accepted if it helps you.

TMKAM
Tera Contributor

Dear Maddysunil,

Thank you for your solution but I would like to have onChange scripts instead of onSubmit scripts,

 

Is scripts check if the u_active is true or false before checking u_domain_whitelisting because if u_active is true and it should be checking u_domain_whitelisting and if u_active is false it does not need to check u_domain_whitelisting anymore.

thank you.

@TMKAM 

You can change the script like below and try

 

function onChange(control, oldValue, newValue, isLoading) {
    // Check if the control being changed is 'u_domain_name'
    if (control == 'u_domain_name') {
        var domainName = g_form.getValue('u_domain_name');
        var domainWhitelisting = new GlideRecord('u_trusted_domain_whitelisting');
        domainWhitelisting.addQuery('u_domain_whitelisting', domainName);
        domainWhitelisting.query();

        if (domainWhitelisting.next()) {
            // Domain exists in the table
            if (domainWhitelisting.u_active) {
                // Active domain, show error message
                g_form.addErrorMessage("Domain is not available.");
                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.

Thanks

TMKAM
Tera Contributor

Dear Maddysunil,

i use the solution you have provided but there is no error message when the u_active is true and u_domain_whitelisting is exist.

I attach the table for your reference.