Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Release cannot move for approval unless Approved Change is attached

tindiz
Giga Guru

Hi, please help on this scenario:

 

Release record cannot be moved to 'Awaiting Approval' unless an approved Change Request is associated to it.

I have the below BR but it is not working.

 

tinadizon_0-1705936849631.pngtinadizon_1-1705936869734.png

 

3 REPLIES 3

tindiz
Giga Guru

tinadizon_0-1705937097038.png

I also used this script but did not work either. Please help. Thanks.

 

(function executeRule(current, previous /*null when async*/) {
    // Check if the state is changing to 'Awaiting Approval' or other relevant states
    if (current.state == 'awaiting_approval' || current.state == 'work_in_progress' || current.state == 'testing_qa' || current.state == 'deploy_launch') {
        // Check if there is a related Change Request
        var changeRequest = new GlideRecord('change_request');
        changeRequest.addQuery('release', current.sys_id);
        changeRequest.query();

        if (!changeRequest.hasNext()) {
            // No Change Request found, prevent state change
            current.setAbortAction(true);
            gs.addErrorMessage('Release cannot move to the selected state without a Change Request.');
        } else {
            // Change Request found, check if it is approved
            if (changeRequest.approval == 'approved') {
                // Change Request is approved, allow state change
                // Add any additional actions as needed
            } else {
                // Change Request not approved, prevent state change
                current.setAbortAction(true);
                gs.addErrorMessage('Release cannot be approved until the associated Change Request is approved.');
            }
        }
    }
})(current, previous);

(function executeRule(current, previous /*null when async*/) {
    // Log the start of the script
    gs.log('Business Rule Script Execution Started for Release: ' + current.number);

    // Check if there is a related Change Request
    var changeRequest = new GlideRecord("change_request");
    changeRequest.addQuery('release', current.sys_id);
    changeRequest.query();

    if (!changeRequest.hasNext()) {
        // No Change Request found, prevent state change
        current.setAbortAction(true);
        gs.log('Release cannot move to the selected state without a Change Request.');
        gs.addErrorMessage('Release cannot move to the selected state without a Change Request.');
    } else {
        // Change Request found, log and check if it is approved
        gs.log('Change Request found for Release: ' + current.number);
        
        if (changeRequest.approval == 'approved') {
            // Change Request is approved, allow state change
            // Add any additional actions as needed
            gs.log('Change Request is approved. Allowing state change for Release: ' + current.number);
        } else {
            // Change Request not approved, prevent state change
            current.setAbortAction(true);
            gs.log('Release cannot be approved until the associated Change Request is approved for Release: ' + current.number);
            gs.addErrorMessage('Release cannot be approved until the associated Change Request is approved.');
        }
    }

    // Log the end of the script
    gs.log('Business Rule Script Execution Completed for Release: ' + current.number);
})(current, previous);