State is not moving back until we refresh it.

Venkat141
Tera Contributor

Hello,

@Ankur Bawiskar 

@Saurav 

@shloke04 

@Anil Lande 

whenever we are trying to move the state then it will check if any open task records are there or not. if there are no-open tasks records then state will move to next state or else it will throw an error and remains on same state. 

 

here Issue is when we are trying to move the state from "perform post test activities" to close and if any open tasks are present then it's throwing an error but state will be close and if we refresh the page then it will go back to "perform post test activities" state.

 

Here in the below code it's working fine for all states expect for close.

 

Could anyone please help me what is the issue.

 

(function executeRule(current, previous /*null when async*/ ) {
    //To check active tasks in each valid state
    var checkTaskAgg = new GlideAggregate('x_nr_it_service_co_itscm_task');
    checkTaskAgg.addQuery('parent', current.sys_id);
    checkTaskAgg.addQuery('active', true);
    checkTaskAgg.addAggregate("COUNT");

    checkTaskAgg.query();
    if (checkTaskAgg.next())
        if (checkTaskAgg.getAggregate("COUNT") > 0) {
            gs.addInfoMessage(gs.getMessage('Please close open tasks from previous state to move to next state'));
            //gs.addInfoMessage('current :'+current.state+'----previous state : '+previous.state);
            current.state = previous.state;
            current.test_status = previous.test_status;
            current.closed_at = previous.closed_at;
            current.closed_by = previous.closed_by;
            //gs.addInfoMessage('final :'+current.state);
            //var url = gs.getProperty("glide.servlet.uri") + gs.action.getGlideURI();
           
            current.setAbortAction(true);
        //  action.setRedirectURL(current);
            //gs.setRedirect(url);
            //gs.addInfoMessage(url);
            gs.addInfoMessage('hello');
            //location.reload();
           
            reloadWindow(window);
            gs.addInfoMessage('hi');
           

        }

})(current, previous);
 
Best Regards,
Pavan

 

5 REPLIES 5

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Venkat141 

 

Even I am not developer, but still want to know. you are running BR before or after? Also OOTB you can check on change flow as well. 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Aniket Chavan
Tera Sage
Tera Sage

Hello @Venkat141 ,

Please give a try to the script below and see how it works for you.

(function executeRule(current, previous /*null when async*/) {
    // To check active tasks in each valid state
    var checkTaskAgg = new GlideAggregate('x_nr_it_service_co_itscm_task');
    checkTaskAgg.addQuery('parent', current.sys_id);
    checkTaskAgg.addQuery('active', true);
    checkTaskAgg.addAggregate("COUNT");

    checkTaskAgg.query();

    if (checkTaskAgg.next() && checkTaskAgg.getAggregate("COUNT") > 0) {
        gs.addErrorMessage(gs.getMessage('Please close open tasks from the previous state to move to the next state'));
        current.setAbortAction(true);
        // The state should not be changed here
    }
})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

Hi @Aniket Chavan ,

 

Still not working. Please find below snippet. its throwing an error and moves to close state but if we refresh the page state will go back to perform post test activities state.

 

MicrosoftTeams-image (17).png

Subhashis Ratna
Tera Guru

Hello @Venkat141 ,

I believe operation is setting only display value of state as closed but not backend value in data base operation as closed and that is the reason after refresh your value is resetting to current which you required.

Script optimization :

you don't have to set previous value to current one when you don't have to change the state and needs to throw error only.

Please remove all state transition code and keep only error code if active query ran successfully.

Second check : please check any client operation is working on your same condition for state field which setting the value as closed on form only.

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.

Warm Regards,
Subhashis r7