Copy all fields data including sys_id in another custom incident table

VeerenderG
Tera Contributor

Hi Experts,
I have a requirement like I need to copy all incident fields data in u_incident_copy table.
Issue: I can do copying all incident data using below background script but not able to copy same sys_id.

Requirement: I need to preserve same incident record sys_id in u_incident_copy table.

 

Background script:

// Get all records from the incident table
var incidentGR = new GlideRecord('incident');
incidentGR.query();

// Loop through each incident record
while (incidentGR.next()) {
// Create a new record in the u_incident_copy table
var incidentCopyGR = new GlideRecord('u_incident_copy');
incidentCopyGR.initialize();

// Map each field from the incident table to the u_incident_copy table
incidentCopyGR.short_description = incidentGR.short_description;
incidentCopyGR.description = incidentGR.description;
incidentCopyGR.category = incidentGR.category;
incidentCopyGR.subcategory = incidentGR.subcategory;
incidentCopyGR.state = incidentGR.state;
incidentCopyGR.cmdb_ci = incidentGR.cmdb_ci;
incidentCopyGR.contact_type = incidentGR.contact_type;
incidentCopyGR.impact = incidentGR.impact;
incidentCopyGR.urgency = incidentGR.urgency;
incidentCopyGR.priority = incidentGR.priority;
incidentCopyGR.assignment_group = incidentGR.assignment_group;
incidentCopyGR.assigned_to = incidentGR.assigned_to;

// continue mapping the remaining fields...

// Copy the sys_id from the incident table to the u_incident_copy table
incidentCopyGR.sys_id = incidentGR.sys_id;

// Get the incident number from the incident table
var incidentNumber = '';
if (incidentGR.getValue('number'))
incidentNumber = incidentGR.getValue('number');

// Copy the incident number to the u_incident_copy table
incidentCopyGR.number = incidentNumber;

// Insert the copied data into the u_incident_copy table
var incidentCopySysID = incidentCopyGR.insert();

gs.print("Copied incident " + incidentNumber + " to u_incident_copy with sys_id: " + incidentCopySysID);
}

gs.print("Data copying completed.");

0 REPLIES 0