Check the if application name exists

SushmithaHemi
Tera Contributor

I was trying to check if the application already exists in cmdb_ci_appl table. If exist i was an alert message saying application already exist and which should work onchange of the variable. I have created a single line text field as new_asset_name and trying below code but i'm not getting any alert message even the application exist. Please help me with the code.

 

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
  return;
  }
  var vdr = new GlideRecord('cmdb_ci_appl');
  vdr.addQuery('name',newValue);
  vdr.query();
  if(vdr.next())
  {
  alert('Application already exists.');
  g_form.setValue('new_asset_name','');
  }
}
SushmithaHemi_0-1732843932676.png

 

1 REPLY 1

Bhavya11
Kilo Patron

hi @SushmithaHemi ,

 

 

 GlideRecord is not recommended on Client script.

GlideRecord is server side API if you want go get some data or validate from the server use GlideAjax API.

 

 

for example 

Script include:

Bhavya11_0-1733124408141.png

 

 

var getCILocal = Class.create();
getCILocal.prototype = Object.extendsObject(AbstractAjaxProcessor, {  
    getCIName: function() {
    var cis = this.getParameter('sysparam_ci');
    var vdr = new GlideRecord('cmdb_ci_appl');
    vdr.addQuery('name', cis);
    vdr.query();
    if (vdr.hasNext()) {
        return 'true'; // Return string 'true'
    } else {
        return 'false'; // Return string 'false'
    }
},

    type: 'getCILocal'
});

client script 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var att = new GlideAjax('getCILocal'); // Name of your Script Include
    att.addParam('sysparm_name', 'getCIName');
    att.addParam('sysparam_ci', newValue);
    att.getXMLAnswer(function(answer) { 
        if (answer === 'true') { 
            alert('Application already exists.');
            g_form.clearValue('assest_name'); // replace 'assest_name' with 'your_variable_name'
        }
    });


}

 

Please mark my solution as Accept, If you find it helpful.

 

Thanks,

BK.