GlideRecord query: check for existing record before creating new
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-04-2018 06:18 AM
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 {...
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-04-2018 12:51 PM
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
}
}
}