- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 03:04 AM
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.
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
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 07:09 AM
Hi @Siddharth4,
I must be sleeping today.. Using variables I didn't define...
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 03:11 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 03:31 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 03:34 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 04:26 AM
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