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

Iraj Shaikh
Mega Sage
Mega Sage

Hi,

 

If you want to continue the incident task numbering that is already present in the system, you need to ensure that your business rule for auto-creating incident tasks is aware of the current task numbering sequence and continues from there. Here are the steps to achieve this:

  1. Identify the Current Highest Task Number: Determine the highest task number currently in your incident tasks table (usually the "Number" field). You can find this by running a query or report to identify the last used task number.

  2. Modify Your Business Rule Logic In your business rule script, you should increment this highest task number by 1 to generate the next task number. Here's an example in JavaScript for a business rule script:

 

var gr = new GlideRecord('task');
gr.addQuery('task_table', 'incident_task');
gr.orderByDesc('number');
gr.query();

if (gr.next()) {
    var currentMaxNumber = parseInt(gr.number);
    var nextNumber = currentMaxNumber + 1;

    // Set the next task number for the newly created incident task
    current.number = nextNumber.toString();
}

 

By following above steps, your business rule will ensure that the newly created incident tasks continue from the highest task number already present in the system. This way, you'll have a consistent sequence of task numbers without any duplicates.

If your problem is solved then please mark it helpful.

Christian_
Tera Guru

Hello,

 

This sounds like a potential bug in your logic.
An 'insert' into the 'incident_task' table should provide you with a unique number upon insertion.
Could you please share your code?

Hi Cristian,

 

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);

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

There is auto-number out-of-the-box on Incident Tasks. Is that also on your instance?

 

Can you also share details about your business rule? It might be scripting issue, for example when using autoSysFields(false).

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn