Task numbers are not auto incremented

Anandareddy
Tera Contributor

Hi,

I have created a business rule to auto create 5 incident tasks when certain conditions are met.

But 5 incident tasks have the task number.

Anandareddy_0-1696589746432.png

 Please check and advise how i can get different numbers for each incident task.

6 REPLIES 6

Anandareddy
Tera Contributor

Hi,

 

I have created After BR on Incident table with insert selected.

Script:

(function executeRule(current, previous /*null when async*/) {

    // Add your code here
 
    var incGr = new GlideRecord('incident_task');
    incGr.initialize();
    for (var i=0; i<5; i++){
    incGr.incident = current.sys_id;
    incGr.cmdb_ci = current.cmdb_ci;
    incGr.short_description = current.short_description;
    incGr.insert();
    }
 
})(current, previous);

The current script will create five records in the 'incident_task' table, but because 'incGr' is not created inside the loop, all of these records will have the same values. 
If you want each record to have different values, you make the following changes:

 

for (var i = 0; i < 5; i++) {
    var incGr = new GlideRecord('incident_task');
    incGr.initialize();
    incGr.incident = current.sys_id;
    incGr.cmdb_ci = current.cmdb_ci;
    incGr.short_description = current.short_description + " Task " + (i + 1); // Add a unique number to the short description
    incGr.insert();
}