How to check if application already exists in CMDB.

SushmithaHemi
Tera Contributor

I was trying to check if the application already exists in cmdb_ci_appl table. So i have written below mentioned client script on change of New asset name variable which is single line text.

 

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-1732882063949.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-1733123226422.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