How to stop duplicate names being entered even with excess whitespace

matthew_hughes
Kilo Sage

I'm working on a catalogue item and we've got a requirement where a user can't input the same name as an existing entry in the business application (cmdb_ci_business_app) table:

matthew_hughes_0-1765961855839.png

However, what we've noticed is that if the existing Business Application's name has whitespace before the current name itself, it doesn't produce an error message when a user enters the same name and doesn't clear the new name field:

matthew_hughes_1-1765961947032.png

The script include function that I'm using is:

checkDuplicates: function(newFieldValue, newFieldName) {

        //Setup parameter fields to be used in the client scripts
        var fieldValue = this.getParameter('sysparm_fieldValue') || newFieldValue;
        var fieldName = this.getParameter('sysparm_fieldName') || newFieldName;

        //Trim off any spaces before or after the 'fieldValue' parameter
        fieldValue = fieldValue.trim();

        //GlideAggregate the Business Application table to check if there are any existing entries by counting the number of records where the fieldsName has the value of fieldValue
        var gaBusinessApplicationName = new GlideAggregate('cmdb_ci_business_app');
        gaBusinessApplicationName.addAggregate('COUNT');
        gaBusinessApplicationName.addQuery(fieldName, fieldValue);
        //gaBusinessApplicationName.addQuery('u_active', true);
        gaBusinessApplicationName.query();
        gaBusinessApplicationName.next();

        //If the condition returns more than 0, return False. Otherwise, return True.
        if (gaBusinessApplicationName.getAggregate('COUNT') > 0) {
            return false;
        } else {
            return true;
        }
    },
 
The client script that I'm using is:
matthew_hughes_2-1765962176667.png
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    //This client script will check if the given value in the 'new_business_application_name' field aleady exists against the 'name' field in the Business Application table. If an entry already exists, it will clear out the field and show a field error message. Otherwise, it will allow the user to carryon with their request.
    var gaBusinessAppName = new GlideAjax('LBGBusinessAppCatItems');
    gaBusinessAppName.addParam("sysparm_name", "checkDuplicates");
    gaBusinessAppName.addParam("sysparm_fieldName", "name");
    gaBusinessAppName.addParam("sysparm_fieldValue", newValue);
    gaBusinessAppName.getXML(duplicateResult);

    function duplicateResult(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer === 'false') {
            g_form.clearValue('new_business_application_name');
            g_form.showFieldMsg('new_business_application_name', 'Duplicate found: A record already exists with this name.', 'error', true);
        } else {
            g_form.setValue('allow_submit', true);
        }
    }
}

 

I was just wondering if somebody has come across this issue before and what I can do to fix it.

 

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@matthew_hughes 

if your record in "cmdb_ci_business_app" table has space before the name then it's a data issue, please fix that

With this space in prefix did you try to use encodedQuery() with LIKE operator i.e. use contains

gaBusinessApplicationName.addEncodedQuery(fieldName + 'LIKE' + fieldValue);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Thanks @Ankur Bawiskar . That works for white spaces, but if a user has a business application with a name of '301 Redirect Service' and they want to change it to '301 Redirect', that should be allowed, but the new addEncodedQuery will stop the user from doing that.

@matthew_hughes 

so you are saying if already name "301 Redirect Service" exists and if user enters 301 Redirect then it will throw error.

Yes it will throw error as it will use contains and find that text

I already informed in my 1st response having leading white space in your name field on your table is a data issue.

You need to fix that without which you can't make your requirement work

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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