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.

Workflow or BR for Release to be moved to 'Awaiting Approval' when Change is raised/attached to it

tindiz
Giga Guru

Hi, I need help with creating a flow designer or business rule for this scenario:

 

Release record cannot be moved to 'Awaiting Approval' state unless a Change Request is attached or raised under it.

 

I've been trying to figure out how to do it using Flow Designer but it seems like I am just running around in circles.

 

7 REPLIES 7

tindiz
Giga Guru

And additional, once Change is raised/attached to the Release record, it has to be approved first before the Release can be approved by the Release admin approver.

tinadizon_0-1705855545331.pngtinadizon_1-1705855557714.png

So I created this BR, but even if the Change has been raised already it still cannot move to another State. Please help.

Aniket Chavan
Tera Sage
Tera Sage

Hello @tindiz ,

Please give a try to the script below and let me know how it works for you.

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

 

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

 

Thanks,

Aniket

It did not work. When I try to move the state to 'Awaiting Approval' OR 'Work In Progress' OR 'Testing/QA' OR 'Deploy/Launch it is not giving the error message even though a Change Request is not raised under the Release it just pushes through with the state change.