If any task is went to Closed Complete state, Parent record should set to Closed Complete.

BhargavaKumar
Tera Expert

Hello,

 

I have a scenario i.e. there is a Custom Parent table and Task table. when any Parent Record created under that 4 tasks are creating itself. So whenever

1. if at least one task went to 'Closed Complete' state(i.e. 1 task= Closed Complete, 3 tasks= Closed Skipped) Parent record should set to 'Closed Complete'  or

2. if all Tasks(4) states are  'Canceled' (4 tasks = Closed Skipped) Parent Record state should set to 'Closed Skipped'.

I am using below After Business rule, It is working properly for 2nd Scenario but not working for 1st one.

Can anyone help me on this. Thanks in advance!😊 

Please see the below code and screen shots for your reference.

(function executeRule(current, previous /*null when async*/ ) {
    var count = 0;
    var count1 = 0;
    var count2 = 0;
    var tasks = new GlideRecord('x_1309288_hr_new_0_hr_task');
    tasks.addQuery('parent', current.parent);
    tasks.query();
    var taskcount = tasks.getRowCount();
    while (tasks.next()) {
        if (tasks.state == 3  ) //change as required
        {
            count++;
        }
        else if (tasks.state == 7 ) //change as required
        {
            count1++;
        }
        else if (tasks.active == false){
            count2++;
        }
    }
    if (count >= 0 && count2 == taskcount ) {
        var req = new GlideRecord('x_1309288_hr_new_0_hr_onboarding_request');
        req.addQuery('sys_id', current.parent);
        req.query();
        if (req.next()) {
            req.state = 3;
            req.update();
        }

    }
    else if (count1 == taskcount) {
        var req1 = new GlideRecord('x_1309288_hr_new_0_hr_onboarding_request');
        req1.addQuery('sys_id', current.parent);
        req1.query();
        if (req1.next()) {
            req1.state = 7;
            req1.update();
        }

    }

})(current, previous);
 

BhargavaKumar_0-1739033327424.png

BhargavaKumar_1-1739033546337.png

BhargavaKumar_2-1739033616460.png

 

 

 

2 ACCEPTED SOLUTIONS

Nishant8
Giga Sage

Hello @BhargavaKumar 2, It would have been easier to compare if you had shared the screenshot of working one too. Nevertheless, can you please try to change the line 21 of your BR (I'm referring the SS for the line) to below and share the outcome?

//removing the else from here

if (tasks.active == false){

            count2++;
}
 
Regards,
Nishant

 

View solution in original post

Hi @Nishant8 ,

Thanks for the suggestion. It's working as expected when i modified the code as below.

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here

    var count = 0;
    var count1 = 0;
    var count2 = 0;
    var tasks = new GlideRecord('x_1309288_hr_new_0_hr_task');
    tasks.addQuery('parent', current.parent);
    tasks.query();
    var taskcount = tasks.getRowCount();
    while (tasks.next()) {
        if (tasks.state == 3  ) //change as required
        {
            count++;
        }
         if (tasks.state == 7 ) //change as required
        {
            count1++;
        }
     if (tasks.active == false){
            count2++;
        }
    }
    if (count >= 1 && count2 == taskcount ) {
        var req = new GlideRecord('x_1309288_hr_new_0_hr_onboarding_request');
        req.addQuery('sys_id', current.parent);
        req.query();
        if (req.next()) {
            req.state = 3;
            req.update();
        }

    }
    else if (count1 == taskcount) {
        var req1 = new GlideRecord('x_1309288_hr_new_0_hr_onboarding_request');
        req1.addQuery('sys_id', current.parent);
        req1.query();
        if (req1.next()) {
            req1.state = 7;
            req1.update();
        }

    }

})(current, previous);

View solution in original post

4 REPLIES 4

HIROSHI SATOH
Mega Sage

 

Since it's one of these, I think the following is correct:

 if (count >= 0 && count2 == taskcount ) {

 if (count > 0 ) {

 

Nishant8
Giga Sage

Hello @BhargavaKumar 2, It would have been easier to compare if you had shared the screenshot of working one too. Nevertheless, can you please try to change the line 21 of your BR (I'm referring the SS for the line) to below and share the outcome?

//removing the else from here

if (tasks.active == false){

            count2++;
}
 
Regards,
Nishant

 

Hi @Nishant8 ,

Thanks for the suggestion. It's working as expected when i modified the code as below.

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here

    var count = 0;
    var count1 = 0;
    var count2 = 0;
    var tasks = new GlideRecord('x_1309288_hr_new_0_hr_task');
    tasks.addQuery('parent', current.parent);
    tasks.query();
    var taskcount = tasks.getRowCount();
    while (tasks.next()) {
        if (tasks.state == 3  ) //change as required
        {
            count++;
        }
         if (tasks.state == 7 ) //change as required
        {
            count1++;
        }
     if (tasks.active == false){
            count2++;
        }
    }
    if (count >= 1 && count2 == taskcount ) {
        var req = new GlideRecord('x_1309288_hr_new_0_hr_onboarding_request');
        req.addQuery('sys_id', current.parent);
        req.query();
        if (req.next()) {
            req.state = 3;
            req.update();
        }

    }
    else if (count1 == taskcount) {
        var req1 = new GlideRecord('x_1309288_hr_new_0_hr_onboarding_request');
        req1.addQuery('sys_id', current.parent);
        req1.query();
        if (req1.next()) {
            req1.state = 7;
            req1.update();
        }

    }

})(current, previous);

Hello @BhargavaKumar , Glad to hear that my suggestion helped you resolve the problem. If you don't mind then can you please accept my suggestion as solution too additionally so that in future if somebody faces the same problem, can refer the same and understand the problematic part. Thank you...

 

Regards,

Nishant