- 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 04:30 AM
Ah I see something wrong there 🙂
Sorry, not quickly able to test myself, because I don't have that setup.
if (current.parent) {
var parentTask = current.parent;
var parentTaskNumber = parent.number;
gs.log(parentTaskNumber + 'is the current task');
var childTaskGR = new GlideRecord('sc_task');
childTaskGR.addQuery('parent', parent.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 ' + parent.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 06:32 AM
Hi Peter,
Still it's not working, Script is not able to query the ParentTask.getUniqueValue() in second log. Parent filed is a reference field.
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 06:43 AM
Hi @Siddharth4,
Is the parent field a normal reference field, or a document_id field?
Is the field just parent or u_parent?
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:01 AM

- 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.