GlideRecord query: check for existing record before creating new

steve000
Kilo Explorer

Hey All,

I have a UI Action on the incident form that creates a new record and populates from incident fields.One of them being the CI. I am trying to query this custom table that the new record will be created in for an existing match of CI. I am a bit new to scripting so this could be entirely wrong. I am sure it is wrong since it is not working:

function createAssetExchange(){
       //Check for existing Asset Exchange
       var match;
       var existing = new GlideRecord('u_asset_exchange');
       existing.addQuery('u_defective_asset.asset_tag', g_form.getValue('cmdb_ci.asset_tag'));
       existing.query(checkForExisting);
       function checkForExisting(){
              if (existing.next()) {
                    match = 'Yes';
             } else {
                   match = 'No';
            }

            if (confirm('This will create a new Asset Exchange using the specified Location and Configuration item on this    Incident. Are you sure you want to proceed?')) {

                  if (match != 'No') {
                         alert("There is already an active Asset Exchange open for this CI");
                         return;
                      } else {...

 

1 REPLY 1

Andrew Saxton -
Tera Expert

Hey Steve,

 

Try this:

function createAssetExchange() {
    //Check for existing Asset Exchange
    var existing = new GlideRecord('u_asset_exchange');
    existing.addQuery('u_defective_asset.asset_tag', g_form.getValue('cmdb_ci.asset_tag'));
    existing.query();
    //Checking if we found any records
    if (existing.next()) {
    	//We found a record
        alert("There is already an active Asset Exchange open for this CI");
        return;
    } else {
    	//We did not find a record
        if (confirm('This will create a new Asset Exchange using the specified Location and Configuration item on this    Incident. Are you sure you want to proceed?')) {
        	//Do something
        }
    }
}