|
| 1 | +### Prompt for Creating a Scheduled Job to Archive Old Incidents |
| 2 | + |
| 3 | +**Prompt:** Develop a scheduled job that archives incidents older than one year into a separate archive table to keep the incident table manageable. The job should ensure that all relevant data is transferred, and the original incidents are deleted from the active incident table. |
| 4 | + |
| 5 | +**Structure:** Outline the steps to create the scheduled job, including the script logic for archiving incidents. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +### Expected Output: |
| 10 | + |
| 11 | +1. **Navigate to Scheduled Jobs:** |
| 12 | + - Go to **System Definition > Scheduled Jobs** in ServiceNow. |
| 13 | + - Click on **New** to create a new scheduled job. |
| 14 | + |
| 15 | +2. **Configure Scheduled Job Properties:** |
| 16 | + - **Name:** Archive Old Incidents |
| 17 | + - **Active:** True |
| 18 | + - **Run:** Periodically (set to run daily, weekly, etc. as per your organization’s requirements) |
| 19 | + |
| 20 | +3. **Set the Schedule:** |
| 21 | + - Use the **Run** field to specify when the job should execute (e.g., daily at midnight). |
| 22 | + |
| 23 | +4. **Script Logic:** |
| 24 | + - Add the following script in the **Script** field: |
| 25 | + |
| 26 | +```javascript |
| 27 | +// Create a GlideRecord for the incident table |
| 28 | +var grIncident = new GlideRecord('incident'); |
| 29 | +grIncident.addQuery('sys_created_on', '<=', gs.daysAgo(365)); // Incidents older than 1 year |
| 30 | +grIncident.query(); |
| 31 | + |
| 32 | +while (grIncident.next()) { |
| 33 | + // Create a new record in the archive table |
| 34 | + var grArchive = new GlideRecord('incident_archive'); // Assume this is your archive table |
| 35 | + grArchive.initialize(); |
| 36 | + grArchive.short_description = grIncident.short_description; |
| 37 | + grArchive.description = grIncident.description; |
| 38 | + grArchive.state = grIncident.state; |
| 39 | + grArchive.assigned_to = grIncident.assigned_to; |
| 40 | + grArchive.resolved_at = grIncident.resolved_at; |
| 41 | + // Add other necessary fields as required |
| 42 | + grArchive.insert(); // Insert the archived record |
| 43 | + |
| 44 | + // Delete the original incident |
| 45 | + grIncident.deleteRecord(); |
| 46 | + gs.info('Archived and deleted incident: ' + grIncident.number); |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +5. **Testing:** |
| 51 | + - Create several test incidents with different created dates. |
| 52 | + - Run the scheduled job manually to test the archiving process. |
| 53 | + - Verify that old incidents have been archived correctly and deleted from the active table. |
| 54 | + |
| 55 | +6. **Documentation:** |
| 56 | + - Document the scheduled job in the ServiceNow application documentation, including its purpose, schedule, and impact on incident management. |
| 57 | + |
| 58 | +--- |
0 commit comments