diff --git a/Background Scripts/Bulk Update Tables/BG Script-BulkUpdateTable_Readme.md b/Background Scripts/Bulk Update Tables/BG Script-BulkUpdateTable_Readme.md new file mode 100644 index 0000000000..6113da041d --- /dev/null +++ b/Background Scripts/Bulk Update Tables/BG Script-BulkUpdateTable_Readme.md @@ -0,0 +1,8 @@ +This Background will help to bulk update the incident table where +Finds all incidents that are:Active +Have priority 3 +Have category = network +Updates each of those records: +Sets state = 2 (which usually means “In Progress”) +Adds a comment: "Updated via bulk update script" +Logs the number of records updated to the system logs. diff --git a/Background Scripts/Bulk Update Tables/BG Scripts - Bulk Update Tables.js b/Background Scripts/Bulk Update Tables/BG Scripts - Bulk Update Tables.js new file mode 100644 index 0000000000..738dc4f38a --- /dev/null +++ b/Background Scripts/Bulk Update Tables/BG Scripts - Bulk Update Tables.js @@ -0,0 +1,35 @@ +// Bulk update function to update records in any table +function bulkUpdate(table, query, data) { + if (!table || !query || typeof data !== 'object') { + gs.error("Invalid parameters provided to bulkUpdate function."); + return; + } + + var gr = new GlideRecord(table); + gr.addEncodedQuery(query); + gr.query(); + + var count = 0; + while (gr.next()) { + for (var field in data) { + if (data.hasOwnProperty(field)) { + try { + gr.setValue(field, data[field]); + } catch (e) { + gs.warn("Failed to set field '" + field + "' on record " + gr.getUniqueValue() + ": " + e.message); + } + } + } + gr.update(); + count++; + } + + gs.info("Bulk update completed. Total records updated: " + count); +} + +// Call the function to update incidents: +// Where active = true, priority = 3, and category = network +bulkUpdate('incident', 'active=true^priority=1^category=Hardware', { + state: 2, // Typically "In Progress" + comments: 'Updated via bulk update script' +});