If record exist in the table, the catalog form should not allow to submit and should display the alert message with Record already exist.

Suresh36
Tera Expert

If record exist in the table, the catalog form should not allow to submit and should display the alert message with Record already exist. If it is new record the form is allowed to submit and insert the record.

But when Iam trying to submit the form with duplicate record, alert message is populating but form is submitted with data. It should not be allowed to submit the form, if it is duplicate.  

Please find the below code, pls correct if anything wrong.

 if (g_form.getValue('oss_gov_package_registry') == 'npm' || g_form.getValue('oss_gov_package_registry') == 'nuget') {
        alert('inside npm :' + g_form.getValue('oss_gov_package_registry'));
        //var ga1=new GlideAjax('InsertLicenseRecord');
        ga1.addParam('sysparm_name', 'isRecordExistInOSSLibrary');
        ga1.addParam('sysparm_packageName', g_form.getValue('package_name'));
        ga1.addParam('sysparm_packageRegistry', g_form.getValue('oss_gov_package_registry'));
        ga1.addParam('sysparm_packageVersion', g_form.getValue('oss_gov_version_number'));
        ga1.getXML(validatePackage);     

    } 

    function validatePackage(response) {
        result = response.responseXML.documentElement.getAttribute('answer');
        alert("result" + result);

        if (result == 'true') {
            alert('Record is already exist so please submit the record with different value');
            }
    }

    return false;

}

Script Include :

var ValidateOSSLibraryRecord = Class.create();
ValidateOSSLibraryRecord.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isRecordExistInOSSLibrary: function() {
        gs.log('insertRecordOSSLibrary : npm : ');
        var retVal = 'false';
        var packageRegistry = this.getParameter('sysparm_packageRegistry');
        var gr = new GlideRecord('u_oss_library');

if (packageRegistry == 'npm' || packageRegistry == 'nuget') {
            var packageName = this.getParameter('sysparm_packageName');
            var packVersion = this.getParameter('sysparm_packageVersion');
            gs.info('Package Name : '+packageName);
            gs.info('Package Version : '+packVersion);
            
            //var gr = new GlideRecord('u_oss_library');
            gr.addQuery('u_library_name', packageName);
            //gr.addQuery('u_packagetype', packageRegistry);
            gr.addQuery('u_version', packVersion);
            gr.query();
            if (gr.next()) {
                 gs.info('NPM/nuget Package Name is already exist');
               retVal = 'true';
                
            }

return retVal;

},

1 ACCEPTED SOLUTION

Mahendra RC
Mega Sage

Hello Suresh,

Please change your onSubmit client script to use getXMLWait:

 if (g_form.getValue('oss_gov_package_registry') == 'npm' || g_form.getValue('oss_gov_package_registry') == 'nuget') {
        alert('inside npm :' + g_form.getValue('oss_gov_package_registry'));
        //var ga1=new GlideAjax('InsertLicenseRecord');
        ga1.addParam('sysparm_name', 'isRecordExistInOSSLibrary');
        ga1.addParam('sysparm_packageName', g_form.getValue('package_name'));
        ga1.addParam('sysparm_packageRegistry', g_form.getValue('oss_gov_package_registry'));
        ga1.addParam('sysparm_packageVersion', g_form.getValue('oss_gov_version_number'));
        ga1.getXMLWait(); 
        var recordExist =ga1.getAnswer();
        if (recordExist == "true") {
                alert('Record is already exist so please submit the record with different value');
                return false;
        }
}

Please mark my respsone as helpful/correct, if it answer your question.

Thanks

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you are using Asynchronous GlideAjax so by the time the script include function returns the data the form might get submitted.

Refer this link for alternate approach

How to limit the total quantity of a shopping cart for a specific service catalog item in New York v...

Refer these links for workaround/solution on how to use Synchronous GlideAjax in onSubmit

How To: Async GlideAjax in an onSubmit script

Asynchronous onSubmit Catalog/Client Scripts in ServiceNow

Regards
Ankur

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

@Suresh

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

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

@Suresh

getXMLWait() won't work in portal so it won't work.

You need to try the links I shared which has the workaround for this.

Did you check those?

Regards
Ankur

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

Mahendra RC
Mega Sage

Hello Suresh,

Please change your onSubmit client script to use getXMLWait:

 if (g_form.getValue('oss_gov_package_registry') == 'npm' || g_form.getValue('oss_gov_package_registry') == 'nuget') {
        alert('inside npm :' + g_form.getValue('oss_gov_package_registry'));
        //var ga1=new GlideAjax('InsertLicenseRecord');
        ga1.addParam('sysparm_name', 'isRecordExistInOSSLibrary');
        ga1.addParam('sysparm_packageName', g_form.getValue('package_name'));
        ga1.addParam('sysparm_packageRegistry', g_form.getValue('oss_gov_package_registry'));
        ga1.addParam('sysparm_packageVersion', g_form.getValue('oss_gov_version_number'));
        ga1.getXMLWait(); 
        var recordExist =ga1.getAnswer();
        if (recordExist == "true") {
                alert('Record is already exist so please submit the record with different value');
                return false;
        }
}

Please mark my respsone as helpful/correct, if it answer your question.

Thanks