We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

existing record validation on String Field

_bhishek
Tera Guru

Hi All,

 

I have an catalog item to create business Application .Business application is inserted after approvals. There is field as application name in catalog item which maps to Name field field of Business application in service now.

I want when user enters any name in Application name field of catalog item which is operation and existing already in service now business application table then user should get an message like "This application already exist " and there should be a Click here Hyperlink which takes to the list view of business applications which should show the applications with  ' name contains what users has entered in Application name string field. '
I tried with Script include and catalog client script(using HTML message) but that message is coming for few seconds and automatically goes and if again enter same name in string field then no message comes .I want like if user enters same name again and again then that message/error should be there with respective Hyperlink and user should be able to close that message/error.

Any pointer would be helpful.

 

Thank you. 

6 REPLIES 6

Hello Ankur ,
I am using below script include and Catalog client script
Script include:

var BusinessAppNameValidator = Class.create();
BusinessAppNameValidator.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkName: function () {

        var appName = this.getParameter('sysparm_application_name');

        var result = {
            exactMatch: false,
            exactSysId: '',
            similarRecords: []
        };

        if (!appName)
            return JSON.stringify(result);

        var inputName = appName.trim().toLowerCase();

        var gr = new GlideRecord('cmdb_ci_business_app');
        gr.addQuery('operational_status', 1); 
        gr.addQuery('name', 'CONTAINS', appName);
        gr.orderBy('name');
        gr.query();

        while (gr.next()) {

            var existingName = gr.getValue('name');
            var existingSysId = gr.getUniqueValue();

            if (existingName.trim().toLowerCase() === inputName) {
                result.exactMatch = true;
                result.exactSysId = existingSysId;
            }

            result.similarRecords.push({
                name: existingName,
                sys_id: existingSysId
            });
        }

        return JSON.stringify(result);
    }

});

Catalog Client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) return;

    g_form.clearMessages();

    var ga = new GlideAjax('BusinessAppNameValidator');
    ga.addParam('sysparm_name', 'checkName');
    ga.addParam('sysparm_application_name', newValue);

    ga.getXMLAnswer(function(answer) {
        if (!answer) return;

        var result = JSON.parse(answer);
        if (!result || (result.similarRecords.length === 0 && !result.exactMatch)) return;

        var encodedQuery = "nameLIKE" + newValue + "^operational_status=1";
        var listUrl = "/cmdb_ci_business_app_list.do?sysparm_query=" + encodeURIComponent(encodedQuery);

        var message = "";
        message += "<div style='margin-bottom:8px;'>";
        message += "There are other similar records that already exist in CMDB. ";
        message += "Please view the existing records to see if it meets your requirement.";
        message += "</div>";

        message += "<div style='margin-bottom:8px;'><strong>List of Business Application Names:</strong></div>";
        message += "<ul style='margin-top:0;'>";

        // SHOW ALL records including duplicates
        result.similarRecords.forEach(function(rec) {
            message += "<li>" + rec.name + "</li>";
        });

        message += "</ul>";

        message += "<div style='margin-top:8px;'>";
        message += "<a href='" + listUrl + "' target='_blank'><strong>Click Here</strong></a> for more information.";
        message += "</div>";

        // Show error if exact match exists
        if (result.exactMatch) {
            g_form.addErrorMessage(message);
        } else {
            g_form.addInfoMessage(message);
        }
    });
}

I am getting below message on top of form  but it is staying just for 3 seconds and goes away and if i click again out side Application name field then no message as name is not changed .
how can i put this message till the time user does not close or if there is any other approach to do this .I want to show message with hyperlink of list view of those business application.If message is below field then it would be more than fine .

 
 

 

 
There are other similar records that already exist in CMDB. Please view the existing records to see if it meets your requirement.
List of Business Application Names:
  • C
  • C
  • C
CLICK HERE  for more information.

 

 

@_bhishek 

that's OOTB behavior, the message will stay for sometime and then go

You can use this workaround

1) create a URL type variable and hide it on form load

2) based on your validation, form the URL and paste in that URL variable and show that variable to user

3) user can click and then it opens the URL in new tab

💡 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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader