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

Thanks @Peter Bodelier , It's working fine now 

 

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 

 

Siddharth4_1-1695912463404.png

 

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 

Siddharth4_0-1695912422676.png

 

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.