logo

NJP

ServiceNow Glide Record deleteRecord vs deleteMultiple method

Import · Dec 07, 2022 · article

Hi All,

Hope you are doing great.

In this article I will try to answer the most commonly asked question which is when and what should be used deleteRecord or deleteMultiple.

deleteRecord() and deleteMultiple

  • These two method are for the deletion of one or more records from database.
  • you should always make one thing clear in mind that deletion of records in ServiceNow should be always be a rare scenario where deactivation is not an option.
  • Wherever deactivation is option you should go for the same.
  • Non of this function ask for any arguments as both of the record acts on the GlideRecord object.
  • deleteRecord deletes single record wehereas deleteMultiple delete all the records for the passed query
  • You should not use deleteMultiple when your table contains currency field instead you should loop through the records one by one to support deleteRecord().

Performance:

  • The difference is mainly in performance.
  • This has to do with the underlying SQL operations.
  • In SQL, when you want to change a record (including delete operations), the database does some commit checks on every record you make. If you run the operation on multiple records in a sequence, SQL will perform the same commit check but save time only once.
    So if you need to delete 5-10 records, it doesn't matter, if you need to delete 20000 records, deleteMultiple can save you a lot of time.

deleteRecord() vs deleteMultiple()

Entity deleteRecord() deleteMultiple()
Part of API Gliderecord GlideRecord
Arguments No No
Supports One record Multiple record matching passed query
ddot walking support Yes No
Exclude criteria NA If table contains currency
prior calls to setWorkflow() No Yes
Returns Flag that indicates whether the record was successfully deleted. Possible values: true: Record was deleted. false: No record was found to delete. Method does not return a value

Examples

deleteMultiple()

function deleteIncidents() {
var callerSysId = '<user sys id>'
  var gr = new GlideRecord('sc_cart_item');
  gr .addQuery('caller_id', callerSysId );
  gr .query();
  gr .deleteMultiple();
}

deleteRecord()

var rec = new GlideRecord('incident');
rec.addQuery('active',false);
rec.query();
while (rec.next()) { 
 gs.print('Inactive incident ' + rec.number + ' deleted');
 rec.deleteRecord();
}

Please be sure to bookmark this article as well as mark it as Helpful if you thought it was helpful.

Regards,

Amit Gujarathi

Technomonk Youtube

Amit Gujarathi Linkedin

TheTechnomonk.com

ServiceNow Community Amit Gujarathi

View original source

https://www.servicenow.com/community/developer-articles/servicenow-glide-record-deleterecord-vs-deletemultiple-method/ta-p/2406064