Check on CTASKS for completion before proceeding to next stage

Joel O
Mega Sage

Good day everyone, 

 

I have a use case for me to implement a check against CTASKS completion before the ticket can proceed to the next stage. The script provided below is being added to the Review UI Action on the change_request table and although the message does appear, it still proceeds to the Review state. 

 

Could I get a review of the code below and assist on what I am missing? As I'm still new to scripting, looking for your insights.

 

function moveToReview() {
    g_form.setValue("state", "0");
    gsftSubmit(null, g_form.getFormElement(), "state_model_move_to_review");

var gr = new GlideRecord("change_task");
     gr.addQuery("change_request", "=", g_form.getUniqueValue());
    gr.addQuery("state", "<", "3"); // anything less than 3 is not closed
    gr.setLimit(1);
    gr.query();

if (gr.next()) {
    g_form.addErrorMessage("Cannot move to Review state while Change Tasks are open.");
    return false;
}

}

if (typeof window == 'undefined')
   setRedirect();

function setRedirect() {
    current.update();
    action.setRedirectURL(current);
}

 

Much appreciated! 

 

2 ACCEPTED SOLUTIONS

Sujatha V M
Kilo Patron
Kilo Patron

@Joel O  You can update the existing "Review" action condition as below,

 

 

gs.hasRole('itil,sn_change_write') && new ChangeFormUI(current).isReviewAvailable()  && !(new ChangeRequest(current).hasOpenTasks())

 

 

By doing so, the "Review" button will be visible only after all the changes are closed/cancelled and make sure you the workflow modified accordingly so that it doesn't set the value of the state to "Review" automatically on closure of tasks. 

 

Instead of "Set Values" you can modify it to "Wait for Review State as Condition" and have the "Review" UI action functionality without any changes.  

 

SujathaVM_2-1716657889561.png

 

Reference : https://www.servicenow.com/community/itsm-forum/donot-close-change-request-unless-all-change-tasks-a...

 

Please mark this as helpful and accept it as a solution if this resolves your query.

Thanks,

Sujatha V.M.

 

 

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.

View solution in original post

Community Alums
Not applicable

Hi @Joel O ,

The issue with the above script is that it sets the state to "Review" before checking for open tasks.

 

Please try with the below code-

 

function moveToReview() {
    var gr = new GlideRecord("change_task");
    gr.addQuery("change_request", g_form.getUniqueValue());
    gr.addQuery("state", "<", "3"); // anything less than 3 is not closed
    gr.setLimit(1);
    gr.query();

    if (gr.next()) {
        g_form.addErrorMessage("Cannot move to Review state while Change Tasks are open.");
        return false;
    } else {
        g_form.setValue("state", "0");
        gsftSubmit(null, g_form.getFormElement(), "state_model_move_to_review");
    }
}

if (typeof window == 'undefined')
    setRedirect();

function setRedirect() {
    current.update();
    action.setRedirectURL(current);
}

 

The above script ensures that the state is only set to "Review" if there are no open change tasks, preventing the ticket from proceeding if tasks are still open.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

 

View solution in original post

4 REPLIES 4

Sujatha V M
Kilo Patron
Kilo Patron

@Joel O  You can update the existing "Review" action condition as below,

 

 

gs.hasRole('itil,sn_change_write') && new ChangeFormUI(current).isReviewAvailable()  && !(new ChangeRequest(current).hasOpenTasks())

 

 

By doing so, the "Review" button will be visible only after all the changes are closed/cancelled and make sure you the workflow modified accordingly so that it doesn't set the value of the state to "Review" automatically on closure of tasks. 

 

Instead of "Set Values" you can modify it to "Wait for Review State as Condition" and have the "Review" UI action functionality without any changes.  

 

SujathaVM_2-1716657889561.png

 

Reference : https://www.servicenow.com/community/itsm-forum/donot-close-change-request-unless-all-change-tasks-a...

 

Please mark this as helpful and accept it as a solution if this resolves your query.

Thanks,

Sujatha V.M.

 

 

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.

Thank you for your response. This has been tested and working as intended. 

Community Alums
Not applicable

Hi @Joel O ,

The issue with the above script is that it sets the state to "Review" before checking for open tasks.

 

Please try with the below code-

 

function moveToReview() {
    var gr = new GlideRecord("change_task");
    gr.addQuery("change_request", g_form.getUniqueValue());
    gr.addQuery("state", "<", "3"); // anything less than 3 is not closed
    gr.setLimit(1);
    gr.query();

    if (gr.next()) {
        g_form.addErrorMessage("Cannot move to Review state while Change Tasks are open.");
        return false;
    } else {
        g_form.setValue("state", "0");
        gsftSubmit(null, g_form.getFormElement(), "state_model_move_to_review");
    }
}

if (typeof window == 'undefined')
    setRedirect();

function setRedirect() {
    current.update();
    action.setRedirectURL(current);
}

 

The above script ensures that the state is only set to "Review" if there are no open change tasks, preventing the ticket from proceeding if tasks are still open.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

 

Thank you for your response. This too has been tested and working as intended.