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

tinadizon_0-1705859715399.pngtinadizon_1-1705859728404.png

 

Hello @tindiz ,

Please give a try to the script below and let me know, also I have added logs so we could get an idea like how much code is working,

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

tinadizon_0-1705887922105.png

I got this message when I try to move the Release record to 'Awaiting Approval' while Change Request has not been raised yet.

 

And when I try to raise a Change Request and gets it in 'Approved' state and tries to move the Release to 'Awaiting Approval' I am still getting the above message.