Unable to Create Incident Using Scheduled Job

Hari S1
Tera Contributor

Hi, 

How can I create an incident weekly once using a scheduled job? I have tried, but I'm unable to create the incident from the scheduled job.  please help me?

 

 

Thanks

11 REPLIES 11

@Hari S1 

script looks good to me.

can you add try catch block to know if any exception is thrown? you didn't share the error from system logs

try to use this for line 5

incidentGR.setDisplayValue('assignment_group','Hardware');

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Hari S1 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Your script is mostly correct, but I see potential issues that might prevent the incident from being created successfully.
Little bit I Edit a script .

var incidentGR = new GlideRecord('incident');
 incidentGR.initialize(); 
incidentGR.short_description = 'Certificate Alert: test'; 
incidentGR.priority = 5; 
incidentGR.assignment_group.setDisplayValue('Hardware'); 
incidentGR.caller_id.setDisplayValue('System Administrator'); 
incidentGR.state = 1; 
 incidentGR.insert(); 
 gs.log('Ticket Created: ' + incidentGR.short_description);

SyedMahemoH
Tera Expert
Hi @Hari S1 
I Hope you are doing well!
Here is the step you can follow this .

Navigate to: System Definition → Scheduled Jobs → New Configure the Scheduled Job:
Name: Create Weekly Incident.
Run As: <Your User or System User>
Active:  (Checked)
Repeat Interval: Weekly.
Time: Choose the desired time.

Step 2: Add a Script to Create an Incident
In the Script field of the scheduled job, add the following script:


(function executeScheduleJob() {
    var inc = new GlideRecord('incident'); // Access the Incident table
    inc.initialize(); // Create a new record

    // Set the required fields
    inc.short_description = "Weekly Incident - Auto Created";
    inc.description = "This incident is created automatically by a scheduled job.";
    inc.impact = 2; // Set impact (1-High, 2-Medium, 3-Low)
    inc.urgency = 2; // Set urgency (1-High, 2-Medium, 3-Low)
    inc.priority = 3; // Set priority based on impact & urgency
    inc.caller_id = "System Administrator"; // Set caller (Modify as needed)
    inc.insert(); // Insert the new incident record
})();
Step 3: Save and Test
Save the Scheduled Job
Run it manually for testing:
Click on Execute Now to check if an incident is created.
Verify:
Go to Incident → All and check if the new incident appears.

Hi @SyedMahemoH .

 

I have made changes to the script using your suggestions, but the incident is still not being created in the Incident table. I can see an error in the logs.

 

Scheduled Job

HariS1_0-1740721784940.png

system logs - error

HariS1_1-1740721841004.png