UI Action Delete Single Incident Record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2025 04:15 AM
Hi Devs,
I'm a beginner and learning ServiceNow, it's functions & Scripts.
I'm trying to create a UI Action button in PDI to delete a record but when I tried. It deleted all the incidents which was created earlier.
Can someone please help me on this?
The script I used to delete records & Screenshot of UI Action.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2025 05:40 PM - edited 03-15-2025 06:07 PM
As per your script, you are looping through the entire incident table and deleting the each record found under the incident table.
So in your script you need to use "if" statement instead of "while" loop to fetch a single record that matches your condition.
"If" statement picks the first matching record and on the other hand "while" loop picks all the matching records from the table.
Since you didn't put any filter query in your script, "while" loops picks all the record from the incident table.
To achieve your requirement, you need to add a filter query and include "if" statement in the place of "while" loop or else you can simply use "get" method to pick the current record.
Modified script:
var inc = new GlideRecord('incident');
inc.get(current.sys_id);
inc.deleteRecord();
OR
var inc = new GlideRecord('incident');
inc.addQuery('sys_id', current.sys_id); //pick up the current record
inc.query();
if(inc.next()){
inc.deleteRecord();
}
Note:
Avoid to define a variable name as current . Instead you can use some other meaningful names. current is a server side key-word.
Hope this helps.
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2025 06:16 PM - edited 03-15-2025 06:18 PM
It seem that your UI Action is missing a filter to delete only the selected record instead of all records. Below code is the correct way to write a UI Action in ServiceNow that deletes only the current record.
Most efficient way is to Create a Server-Side Script Include first and then create your UI action script with a warning message.
(DeleteRecordScript) (Name of the Script include)
var DeleteRecordScript = Class.create();
DeleteRecordScript.prototype = Object.extendsObject(AbstractAjaxProcessor, {
deleteRecord: function() {
var sysId = this.getParameter('sys_id');
if (sysId) {
var gr = new GlideRecord('incident');
if (gr.get(sysId)) {
gr.deleteRecord();
return 'success';
}
}
return 'error';
}
});
UI Action script :
function deleteRecord() {
var sysId = g_form.getUniqueValue(); // Get current record sys_id
if (confirm("Are you sure you want to delete this record?")) {
var ga = new GlideAjax('DeleteRecordScript');
ga.addParam('sys_id', sysId);
ga.getXMLAnswer(function(response) {
if (response === 'success') {
alert("Record deleted successfully!");
window.location = "incident_list.do"; // Redirect to list view
} else {
alert("Error deleting record!");
}
});
}
}
Hope this would be helpful
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2025 07:08 PM
HI @DanasekaranH ,
all you need is one line of code, replace the entire script with
current.deleteRecord();
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya