Tasks cant be created or modified in change state closed or cancelled

mamarusk
Tera Contributor

Hi everyone, Im trying to restrict possibility to create or modify tasks in change in state closed or cancelled, I tried to create some script or changing ui policie or create business rule but I didnt succeed so far, can you help me?

2 ACCEPTED SOLUTIONS

Community Alums
Not applicable

Hi @mamarusk ,

This requirement can be fulfilled in the below manner by creating a business rule.

Step 1:

SanjayG_0-1714689386578.png

 

Step 2:

SanjayG_1-1714689455481.png

 

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

    // Get the state of the parent change record
    var change = new GlideRecord('change_request');
    if (change.get(current.change_request)) { // Assumes 'change_request' is the field linking to the parent change
        var state = change.state.toString();

        // Define the states that are considered closed or cancelled, replace with actual state values in your instance
        var closedStates = ['3', '4']; // Example: 3 = Closed, 4 = Cancelled.

        if (closedStates.indexOf(state) !== -1) {
            // If the change is in a closed or cancelled state, prevent modification or creation
            gs.addErrorMessage('You cannot modify or add tasks to a change that is closed or cancelled.');
            current.setAbortAction(true);
        }
    }

})(current, previous);

 

This will restrict any change task being created or updated from a closed/canceled change_request.

 

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

Maddysunil
Kilo Sage

@mamarusk 

You can write before inser/update business rule, below is the sample script you can use it:

 

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

    // Check if the change request is in Closed or Cancelled state
    if (current.state == 'closed' || current.state == 'cancelled') {
        
        // Check if the operation is either insert or update
        if (current.operation() == 'insert' || current.operation() == 'update') {
            
            // Check if the user is trying to create or modify a task
            if (current.type == 'task') {
                
                // Cancel the current operation and display an error message
                gs.addErrorMessage('Tasks cannot be created or modified when the change request is in Closed or Cancelled state.');
                current.setAbortAction(true);
            }
        }
    }

})(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

 

View solution in original post

3 REPLIES 3

Community Alums
Not applicable

Hi @mamarusk ,

This requirement can be fulfilled in the below manner by creating a business rule.

Step 1:

SanjayG_0-1714689386578.png

 

Step 2:

SanjayG_1-1714689455481.png

 

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

    // Get the state of the parent change record
    var change = new GlideRecord('change_request');
    if (change.get(current.change_request)) { // Assumes 'change_request' is the field linking to the parent change
        var state = change.state.toString();

        // Define the states that are considered closed or cancelled, replace with actual state values in your instance
        var closedStates = ['3', '4']; // Example: 3 = Closed, 4 = Cancelled.

        if (closedStates.indexOf(state) !== -1) {
            // If the change is in a closed or cancelled state, prevent modification or creation
            gs.addErrorMessage('You cannot modify or add tasks to a change that is closed or cancelled.');
            current.setAbortAction(true);
        }
    }

})(current, previous);

 

This will restrict any change task being created or updated from a closed/canceled change_request.

 

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

Maddysunil
Kilo Sage

@mamarusk 

You can write before inser/update business rule, below is the sample script you can use it:

 

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

    // Check if the change request is in Closed or Cancelled state
    if (current.state == 'closed' || current.state == 'cancelled') {
        
        // Check if the operation is either insert or update
        if (current.operation() == 'insert' || current.operation() == 'update') {
            
            // Check if the user is trying to create or modify a task
            if (current.type == 'task') {
                
                // Cancel the current operation and display an error message
                gs.addErrorMessage('Tasks cannot be created or modified when the change request is in Closed or Cancelled state.');
                current.setAbortAction(true);
            }
        }
    }

})(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

 

it works! thank you a lot, now I need to learn from your solution why my solutions arent work 😄 😉 thx