- 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 07:32 AM
Thanks @Peter Bodelier , It's working fine now
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 07:47 AM
Hi @Peter Bodelier , I found one issue it was working fine when the parent field contain sctask number but in new sctask we have RITM number in parent and this is not working in new sctask
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 07:47 AM
Hi @Peter Bodelier , I found one issue it was working fine when the parent field contain sctask number but in new sctask we have RITM number in parent and this is not working in new sctask

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 07:52 AM
I'm not sure I fully understand the problem. As you can see in my screenshot, I tested it with RITM as parent.
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.