When mutiple tasks get created for a ritm then how to sync the state change between sctask and ritm

Priya Bonam
Tera Expert

I have requirement , when ritm gets created , then first sctask will get created and state should in sync betwen ritm and first sctask without no issues using business rule. Then  once first task gets closed then it trigger parallel sctasks . Here i am unable to sync the state change based on if condidtions ( no priorty between 2 parallel sctasks..we do not know which sctasks closes first . Also when last tasks gets close then only ritm should close(close Skipped/complete/imcomplete).

 

first state change sync is executing succesful between ritm and sctask.

Second condition set in code is not working and directly closeing the ritm based on second task( which means third condition set is working).

please suggest to modify if condition if any.

 

(function executeRule(current, previous /*null when async*/) {
//GlideRecord to get the parent RITM of the current sc_task
var gr_ritm = new GlideRecord('sc_req_item');
gr_ritm.addQuery("sys_id", current.request_item);
gr_ritm.query();

while(gr_ritm.next()) {

    var ga = new GlideAggregate('sc_task');
ga.addQuery('request_item', current.request_item);
ga.addAggregate('COUNT');
ga.query();
if (ga.next())
    var sctaskCount = ga.getAggregate('COUNT');
    gs.log(sctaskCount);
if(sctaskCount==1)
{
if (current.state == 1) {
            gr_ritm.state = 1;
        } else if (current.state == 2) {
            gr_ritm.state = 2;
        } else if (current.state == 8) {
            gr_ritm.state = 9;
        } else if (current.state == 9) {
            gr_ritm.state = 10;
        } else if (current.state == 10) {
            gr_ritm.state = 11;
           
        } else if (current.state == 11) {
            gr_ritm.state = 12;
        }
        else if (current.state == 3)
   {
gr_ritm.state = 'Implementation Complete';
   }
else if (current.state ==4)
{
    gr_ritm.state = 4;
}
else if (current.state ==7)
{
    gr_ritm.state = 7;
}
}
       
    if(sctaskCount==3 && gr_ritm.state =='Implementation Complete')
{
if (current.state == 1) {
            gr_ritm.state = 'Implementation Complete';
        } else if (current.state == 2) {
            gr_ritm.state = 'Implementation Complete';
        } else if (current.state == 8) {
            gr_ritm.state = 'Implementation Complete';
        } else if (current.state == 9) {
            gr_ritm.state = 'Implementation Complete';
        } else if (current.state == 10) {
            gr_ritm.state = 'Implementation Complete';
           
        } else if (current.state == 11) {
            gr_ritm.state = 'Implementation Complete';
        }
        else if (current.state == 3)   {
   
           gr_ritm.state = 'Implementation Complete';
        }
else if (current.state ==4)  {
gr_ritm.state = 4;
        }
else if (current.state ==7)
{
    gr_ritm.state = 7;
}
}  
        if((sctaskCount==3) && (gr_ritm.state =='Implementation Complete')&&(current.state!=('Closed Complete')||('Closed Incomplete')||('Closed Skipped')))
{
if (current.state == 1) {
            gr_ritm.state = 'Implementation Complete';
        } else if (current.state == 2) {
            gr_ritm.state = 'Implementation Complete';
        } else if (current.state == 8) {
            gr_ritm.state = 'Implementation Complete';
        } else if (current.state == 9) {
            gr_ritm.state = 'Implementation Complete';
        } else if (current.state == 10) {
            gr_ritm.state = 'Implementation Complete';
           
        } else if (current.state == 11) {
            gr_ritm.state = 'Implementation Complete';
        }
        else if (current.state == 3)
   {
gr_ritm.state = 3;
   }
else if (current.state ==4)
{
    gr_ritm.state = 4;
}
else if (current.state ==7)
{
    gr_ritm.state = 7;
}
}  
        /*  var ga = new GlideAggregate('sc_task');
ga.addQuery('request_item', current.request_item);
ga.addAggregate('COUNT');
ga.query();
if (ga.next())
    var sctaskCount = ga.getAggregate('COUNT');
    gs.log(sctaskCount); // Log the count for debugging.

       
            if(sctaskCount >0 || sctaskCount < 1)
            {
if (current.state == 3)
   {
gr_ritm.state = 'Implementation Complete';
   }
else if (current.state ==4)
{
    gr_ritm.state = 4;
}
else if (current.state ==7)
{
    gr_ritm.state = 7;
}
            }
           
            if(sctaskCount >1)

            {
                if (current.state == 3) {
            gr_ritm.state = 3;
        } else if (current.state == 7) {
            gr_ritm.state = 7;
        } else if (current.state == 4) {
 gr_ritm.state =4 ;
        }
        }  
        */    
gr_ritm.update();
}

})(current, previous);
4 REPLIES 4

JenniferRah
Mega Sage

That's not really how it works. If you close the RITM after the first task closes, then your workflow/Flow that creates the tasks will also stop. A better approach is to train your users to look at the Stage field and use that to explain what stage of fulfillment you are in.

created one state which is implementation complete . The ritm state remains same till last gets closed. Hence once first task completed then ritm updated as implementation complete. As first task closed it triggers 2 parallel tasks.

OlaN
Giga Sage
Giga Sage

Hi,

You shouldn't sync the state between sctasks and the ritm. That defeats the purpose of running the sctasks as independent processes from the ritm.

This should be kind of obvious in your second part, where 2 sctasks are created and running in parallel.

At any given time task2 could have one state, and task3 another. What state should the ritm be in then?

It makes no sense.

 

Please explain your use case for syncing the state.

Ankur Bawiskar
Tera Patron
Tera Patron

@Priya Bonam 

try this

(function executeRule(current, previous /*null when async*/) {
    // GlideRecord to get the parent RITM of the current sc_task
    var gr_ritm = new GlideRecord('sc_req_item');
    gr_ritm.addQuery("sys_id", current.request_item);
    gr_ritm.query();

    while (gr_ritm.next()) {
        var ga = new GlideAggregate('sc_task');
        ga.addQuery('request_item', current.request_item);
        ga.addAggregate('COUNT');
        ga.query();
        if (ga.next()) {
            var sctaskCount = ga.getAggregate('COUNT');
            gs.log(sctaskCount);

            // First state change sync
            if (sctaskCount == 1) {
                syncState(current, gr_ritm);
            }

            // Handle parallel SCTasks
            if (sctaskCount == 3 && gr_ritm.state == 'Implementation Complete') {
                syncState(current, gr_ritm);
            }

            // Final RITM closure
            if (sctaskCount == 3 && gr_ritm.state == 'Implementation Complete' && !isClosedState(current.state)) {
                syncState(current, gr_ritm);
            }

            gr_ritm.update();
        }
    }

    function syncState(current, gr_ritm) {
        if (current.state == 1) {
            gr_ritm.state = 1;
        } else if (current.state == 2) {
            gr_ritm.state = 2;
        } else if (current.state == šŸ˜Ž {
            gr_ritm.state = 9;
        } else if (current.state == 9) {
            gr_ritm.state = 10;
        } else if (current.state == 10) {
            gr_ritm.state = 11;
        } else if (current.state == 11) {
            gr_ritm.state = 12;
        } else if (current.state == 3) {
            gr_ritm.state = 'Implementation Complete';
        } else if (current.state == 4) {
            gr_ritm.state = 4;
        } else if (current.state == 7) {
            gr_ritm.state = 7;
        }
    }

    function isClosedState(state) {
        return state == 'Closed Complete' || state == 'Closed Incomplete' || state == 'Closed Skipped';
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader