want to add a incremental task number on a customized field when a new task get created

Siddharth4
Tera Contributor

Hello,

 

I have create a after insert BR to add a incremental task number in the customized field 'u_sr_number'. But the script is not working. 

When user create a new child task using UI action then I want to add the task number with incremental suffix on the custom field like SCTASK0122657_1, SCTASK0122657_2, SCTASK0122657_3 and so on for every child task. Kindly help by providing the correct script.

 

Siddharth4_0-1695894949816.png

 

if (current.parent) {

    var parentTaskNumber = current.parent.number;

    gs.log(parentTaskNumber + 'is the current task');

    var childTaskGR = new GlideRecord('sc_task');

    childTaskGR.addQuery('parent', current.parent);
	
    childTaskGR.addQuery('sys_id', '!=', current.sys_id); // Exclude the current task

    childTaskGR.orderByDesc('u_sr_number'); // Assuming the field is called 'customized_field'

    childTaskGR.query();
    gs.log('parent sys id ' + parentTaskNumber.sys_id);
    var suffix = 1;

    if (childTaskGR.next()) {

        var lastCustomizedField = childTaskGR.u_sr_number;

        gs.log(lastCustomizedField + 'last customized field');

        var lastSuffix = parseInt(lastCustomizedField.split('-')[1]);

        suffix = lastSuffix + 1;

    }
    var customizedString = parentTaskNumber + '-' + suffix;
    current.u_sr_number = customizedString;

}

 

Kindly help with the correct script.

Thanks 

1 ACCEPTED SOLUTION

Hi @Siddharth4,

 

I must be sleeping today.. Using variables I didn't define...

PeterBodelier_0-1695910164079.png

if (current.parent) {
var parentTask = current.parent.getRefRecord();
var parentTaskNumber = parentTask.number;

gs.log(parentTaskNumber + 'is the current task');

var childTaskGR = new GlideRecord('sc_task');

childTaskGR.addQuery('parent', parentTask.getUniqueValue());

childTaskGR.addQuery('sys_id', '!=', current.sys_id); // Exclude the current task

childTaskGR.orderByDesc('u_sr_number'); // Assuming the field is called 'customized_field'

childTaskGR.query();
gs.log('parent sys id ' + parentTask.getUniqueValue());
var suffix = 1;

if (childTaskGR.next()) {

    var lastCustomizedField = childTaskGR.u_sr_number;

    gs.log(lastCustomizedField + 'last customized field');

    var lastSuffix = parseInt(lastCustomizedField.split('-')[1]);

    suffix = lastSuffix + 1;

}
var customizedString = parentTaskNumber + '-' + suffix;
gs.log('customizedString ' + customizedString);
current.u_sr_number = customizedString;
current.update();
}

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

13 REPLIES 13

Peter Bodelier
Giga Sage

Hi @Siddharth4,

 

Why not use the OOTB Number Maintenance?

Add auto-numbering records in a table (servicenow.com)


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Hi Peter, 

 

Thanks for the quick reply but I want to add numbering in the customized field, I tried to achieve this with Number Maintenance but it's not working

 

Thanks

Hi @Siddharth4,

 

Try this:

if (current.parent) {
    var parentTaskNumber = current.parent.number;

    gs.log(parentTaskNumber + 'is the current task');

    var childTaskGR = new GlideRecord('sc_task');

    childTaskGR.addQuery('parent', current.parent);
	
    childTaskGR.addQuery('sys_id', '!=', current.sys_id); // Exclude the current task

    childTaskGR.orderByDesc('u_sr_number'); // Assuming the field is called 'customized_field'

    childTaskGR.query();
    gs.log('parent sys id ' + parentTaskNumber.sys_id);
    var suffix = 1;

    if (childTaskGR.next()) {

        var lastCustomizedField = childTaskGR.u_sr_number;

        gs.log(lastCustomizedField + 'last customized field');

        var lastSuffix = parseInt(lastCustomizedField.split('-')[1]);

        suffix = lastSuffix + 1;

    }
    var customizedString = parentTaskNumber + '-' + suffix;
    current.u_sr_number = customizedString;
    current.update();
}

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Hi Peter, 

 

Thanks for the quick reply but I check the script you provided but it's not working, 

 gs.log('parent sys id ' + parentTaskNumber.sys_id);

this log is not providing the parent sys_id. I'm new in Scripting kindly help by correcting the script. 

 

Thanks