Prevent closing a project that has open issues

This script prevents the closing of a project that has open issues.

Follow the steps below or download the solutions file, see Creating Solutions for details.

Setup

  1. Create two parameters, see Creating Parameters.

    Parameters list view in the OpenAir Scripting Center.

    The List source for the ProjectClosedStage Pick List parameter is “Project stage”.

    The List source for the IssueOpenStage Pick List parameter is “Stage”.

  2. Create a new Project form script deployment.

  3. Enter a Filename and click SAVE. The extension ‘.js’ is automatically appended if not supplied.

  4. Click on the script link to launch the Scripting Studio.

  5. (1) Copy the Program Listing below into the editor, (2) set the Before save event, and set test_prevent_project_close_with_open_issue as the Entrance Function.

    Scripting Studio tools and settings panel showing the form association, the user event association and the selected entrance function.

Program Listing

          // project_stage_id and issue_stage_id depend on account settings
function test_prevent_project_close_with_open_issue() {

    // return if new stage is not closed
    if (NSOA.form.getValue('project_stage_id') !=
        NSOA.context.getParameter('ProjectClosedStage'))
        return;

    // Load issue data
    var issue = new NSOA.record.oaIssue();
    issue.project_id = NSOA.form.getValue('id');
    issue.issue_stage_id = NSOA.context.getParameter('IssueOpenStage');

    var readRequest = {
        type: "Issue",
        fields: "id, date",
        method: "equal to",
        objects: [issue],
        attributes: [{
            name: "limit",
            value: "1"
        }]
    };

    var arrayOfreadResult = NSOA.wsapi.read(readRequest);

    if (!arrayOfreadResult || !arrayOfreadResult[0])
        NSOA.form.error('', "Internal error analyzing project issues.");

    else if (arrayOfreadResult[0].errors === null && arrayOfreadResult[0].objects)
        arrayOfreadResult[0].objects.forEach(
            function(o) {
                NSOA.form.error('', "Can't close project with open issues.");
            }
        );
}