- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2023 10:48 PM
We have an option of "insert and stay" in the form context menu, which creates a same record as current record.
I want to stop this feature on Hardware table (alm_hardware) .
When we click on the "insert and stay" on the hardware form, it creates a duplicate record in the hardware table with same asset_tag and serial_number, which is something we don't want. How can I restrict the "insert and stay" feature on Hardware Table. (alm_hardware).
I have written a BR on Hardware table (alm_hardware) to avoid entries with duplicate asset_tag and serial_number and it working on manual creation of assets in the table, but not with "insert and stay" option.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-19-2023 06:57 AM
I achieved this by adding simple condition in "insert and stay" UI's condition.
1) open the "insert and stay" UI action.
2) add this to the condition: (current.getTableName() != 'alm_hardware')
What this does is it hides the insert and stay option from the context menu when we are in "alm_hardware" tables form view.
We can easily achieve this requirement for any other table also by adding the table name in place of "alm_hardware". ie.. (current.getTableName() != '<any_table_name>').
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2023 11:08 PM - edited ‎07-17-2023 11:18 PM
Hi,
Please try the below:
Client Script:
if (g_form.getActionName() === 'sysverb_insert_and_stay') {
var assetTag = g_form.getValue('asset_tag');
var serialNumber = g_form.getValue('serial_number');
var validation = new HardwareDuplicateValidation();
var isDuplicate = validation.checkForDuplicateRecord(assetTag, serialNumber);
if (isDuplicate) {
g_form.addErrorMessage("A record with the same Asset Tag and Serial Number already exists.");
return false;
}
}
return true;
Script Include:
checkForDuplicateRecord: function(assetTag, serialNumber) {
var duplicateHardware = new GlideRecord('alm_hardware');
duplicateHardware.addQuery('asset_tag', assetTag);
duplicateHardware.addQuery('serial_number', serialNumber);
duplicateHardware.query();
return duplicateHardware.hasNext();
},
Hope this helps,
Thanks
VS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2023 11:11 PM
Hi, Insert and stay functionality is only available to admin users, who should not be using this functionality for the scenario you have identified.
The easiest fix for this is training and where that doesn't work, removing the admin rights of any user not following process\policy as they are a risk to the business.
- I also though that there were existing OOB scripts to prevent Asset and cmdb_ci duplication.
Regarding your BR, I would think that a before insert\update BR to validate serial number and\or asset tag should work regardless of how the record insert is triggered.
Perhaps you could share your script\BR as xml or plain text so that it can be reviewed?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2023 11:27 PM
I have shared the BR below, it is working for manual insertions but not for "insert and stay".
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
// gs.addInfoMessage("this script is working 3");
var myQuery;
var grAst = new GlideRecord('alm_hardware');
if (current.asset_tag != "" || current.serial_number != "") {
if (current.asset_tag != "") {
myQuery = grAst.addQuery('asset_tag', current.asset_tag);
}
if (current.serial_number != "") {
// gs.addInfoMessage('we in the serial number');
myQuery.addOrCondition('serial_number', current.serial_number);
}
grAst.query();
// gs.addInfoMessage("totl results: "+ grAst.getRowCount());
if (grAst.next() && grAst.sys_id != current.sys_id) {
if (current.asset_tag == grAst.asset_tag) {
gs.addErrorMessage("Asset tag already exists");
current.setAbortAction(true);
}
if (current.serial_number == grAst.serial_number) {
gs.addErrorMessage("Serial Number already exists");
current.setAbortAction(true);
}
if (current.serial_number == grAst.serial_number && current.asset_tag == grAst.asset_tag) {
gs.addErrorMessage("Serial Number and Asset Tag already exists");
current.setAbortAction(true);
}
current.setAbortAction(true);
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-18-2023 12:59 AM
Hi, I think you have a number of syntax issues with your BR script and I don't believe that it would be functioning correctly regardless of trigger method\source.
This is untested but should be a suitable alternative (or at least give you a better idea of syntax).
var myQuery = '';
//If an insert the current sys_id should not exist so we don't need to exclude it.
if(current.operation() == 'update') {
myQuery = 'sys_id!=' + current.sys_id + '^';
}
if (current.asset_tag != '') {
myQuery = 'asset_tag=' + current.asset_tag + '^';
}
if (current.serial_number != '') {
myQuery = myQuery + 'serial_number=' + current.serial_number + '^';
}
var grAst = new GlideRecord('alm_hardware');
grAst.addEncodedQuery(myQuery);
grAst.query();
if(grAst.next()) {
gs.addErrorMessage("Insert aborted as Asset tag or serial number exists);
current.setAbortAction(true);
}